Skip to content

Commit 2c54039

Browse files
committed
(feat): add support for alternate canvas implementations
- e.g. node-canvas - so this doesn't depend on being run in a browser/DOM context
1 parent f79266a commit 2c54039

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

index.es6

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export default function trimCanvas (canvas, options = {}) {
22
const {clone} = options
3+
let {createCanvas = createCanvasDOM} = options // support node-canvas, etc
4+
35
const context = canvas.getContext('2d')
46

57
const imgWidth = canvas.width
@@ -24,7 +26,7 @@ export default function trimCanvas (canvas, options = {}) {
2426

2527
// early return if cloning
2628
if (clone) {
27-
const copy = document.createElement('canvas')
29+
const copy = createCanvas()
2830
copy.width = cropXDiff
2931
copy.height = cropYDiff
3032
copy.getContext('2d').putImageData(trimmedData, 0, 0)
@@ -95,3 +97,7 @@ function scanX (fromLeft, imgWidth, imgHeight, imgData) {
9597
// the whole image is white already
9698
return null
9799
}
100+
101+
function createCanvasDOM () {
102+
return document.createElement('canvas')
103+
}

0 commit comments

Comments
 (0)