-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9e2ef8
commit 73879fa
Showing
3 changed files
with
91 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
function supportsImageData () { | ||
try { | ||
const img = new window.ImageData(1, 1) | ||
return img.width === 1 && img.height === 1 | ||
} catch (err) { | ||
return false | ||
} | ||
} | ||
|
||
function createImageDataSimple (width, height) { | ||
return new ImageData(width, height) | ||
} | ||
|
||
function createImageDataCanvas (canvas, width, height) { | ||
return canvas.getContext('2d') | ||
.createImageData(width, height) | ||
} | ||
|
||
function createImageDataObject (width, height) { | ||
return { | ||
data: new Uint8ClampedArray(width * height * 4).fill(0), | ||
width, | ||
height | ||
} | ||
} | ||
|
||
const hasWindow = typeof window !== 'undefined' | ||
if (hasWindow && supportsImageData()) { | ||
module.exports = createImageDataSimple | ||
} else if (hasWindow && typeof document !== 'undefined' && document.createElement) { | ||
const canvas = document.createElement('canvas') | ||
module.exports = (width, height) => | ||
createImageDataCanvas(canvas, width, height) | ||
} else { | ||
module.exports = createImageDataObject | ||
} |