-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.js
64 lines (58 loc) · 1.81 KB
/
hooks.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import {useState, useEffect} from 'react';
const loadImageData = (img, scale, filter = '') => {
const cnv = document.createElement('canvas');
cnv.width = img.naturalWidth * scale;
cnv.height = img.naturalHeight * scale;
const ctx = cnv.getContext('2d');
ctx.filter = filter;
ctx.drawImage(img, 0, 0, cnv.width, cnv.height);
return {
imageData: ctx.getImageData(0, 0, cnv.width, cnv.height),
width: cnv.width,
height: cnv.height,
};
};
export const loadImage = (src, scale = 1, filter = '') => {
const [data, setData] = useState({pixels: [], width: 0, height: 0, ready: false});
useEffect(
function renderImage() {
const img = new Image();
img.src = src;
img.crossOrigin = '';
img.onload = () => {
const {imageData, width, height} = loadImageData(img, scale, filter);
if (imageData.data.length > 0 && imageData.data.some(x => x > 0)) {
setData({imageData, width, height, ready: true});
}
};
},
[src],
);
return data;
};
const loadPixels = (img, scale, filter = 'grayscale()') => {
const {imageData, width, height} = loadImageData(img, scale, filter);
const pixels = [];
for (let i = 0; i < imageData.data.length; i += 4) {
pixels.push(imageData.data[i]);
}
return {pixels, width, height};
};
export const loadGrayImage = (src, scale = 1) => {
const [data, setData] = useState({pixels: [], width: 0, height: 0, ready: false});
useEffect(
function reportRedChannel() {
const img = new Image();
img.src = src;
img.crossOrigin = '';
img.onload = () => {
const {pixels, width, height} = loadPixels(img, scale);
if (pixels.length > 0 && pixels.some(x => x > 0)) {
setData({pixels, width, height, ready: true});
}
};
},
[src],
);
return data;
};