Skip to content

Commit f79266a

Browse files
committed
(feat): add built-in option to clone the canvas
- this is almost always needed by developers, so figured I should add it into the library itself (well again, but w/o a node-canvas dep) - while it should probably be the default, will introduce it as opt-in so it's not a breaking change
1 parent e009bc5 commit f79266a

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

index.es6

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
export default function trimCanvas (canvas) {
1+
export default function trimCanvas (canvas, options = {}) {
2+
const {clone} = options
23
const context = canvas.getContext('2d')
34

45
const imgWidth = canvas.width
@@ -21,6 +22,15 @@ export default function trimCanvas (canvas) {
2122
const trimmedData = context.getImageData(cropLeft, cropTop, cropXDiff,
2223
cropYDiff)
2324

25+
// early return if cloning
26+
if (clone) {
27+
const copy = document.createElement('canvas')
28+
copy.width = cropXDiff
29+
copy.height = cropYDiff
30+
copy.getContext('2d').putImageData(trimmedData, 0, 0)
31+
return copy
32+
}
33+
2434
// set the trimmed width and height
2535
canvas.width = cropXDiff
2636
canvas.height = cropYDiff

0 commit comments

Comments
 (0)