-
Notifications
You must be signed in to change notification settings - Fork 2
/
browser.js
44 lines (32 loc) · 1.15 KB
/
browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* eslint-env browser */
const base64Encode = require('fast-base64-encode')
function getImageData (image) {
if (!image.complete || image.naturalWidth === 0) return
const canvas = document.createElement('canvas')
canvas.width = image.naturalWidth || image.width
canvas.height = image.naturalHeight || image.height
const ctx = canvas.getContext('2d')
ctx.drawImage(image, 0, 0, canvas.width, canvas.height)
return ctx.getImageData(0, 0, canvas.width, canvas.height)
}
function imageFromBuffer (buffer) {
return imageFromDataURL('data:;base64,' + base64Encode(buffer))
}
function imageFromDataURL (dataURL) {
const img = new Image()
img.src = dataURL
return img.decode().then(() => img)
}
function imageFromImageData (imageData) {
const canvas = document.createElement('canvas')
canvas.width = imageData.width
canvas.height = imageData.height
const ctx = canvas.getContext('2d')
ctx.putImageData(imageData, 0, 0)
return imageFromDataURL(canvas.toDataURL())
}
exports.getImageData = getImageData
exports.imageFromBuffer = imageFromBuffer
exports.imageFromImageData = imageFromImageData
exports.Image = Image
exports.default = Image