-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
57 lines (37 loc) · 1.09 KB
/
utils.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
function add_alfa(data) {
for (let i = 0; i < data.length; i++) {
if ((i + 1) % 4 == 0) data[i] = 255;
}
return data
}
// function remove_alfa(data) {
// data = data.filter((_, index) => {
// if ((index + 1) % 4 == 0) return false;
// return true;
// })
// return data
// }
function draw(canvas, file) {
const img = new Image()
const context = canvas.getContext('2d');
img.src = URL.createObjectURL(file);
img.onload = () => {
canvas.height = img.naturalHeight
canvas.width = img.naturalWidth
context.drawImage(img, 0, 0)
after_draw();
}
}
function get_data(canvas) {
let context = canvas.getContext("2d");
let data = context.getImageData(0, 0, canvas.width, canvas.height).data;
// data = remove_alfa(data)
return data
}
function put_data(canvas, new_data) {
new_data = add_alfa(new_data)
let context = canvas.getContext("2d");
let data = context.getImageData(0, 0, canvas.width, canvas.height);
data.data.set(new_data)
context.putImageData(data, 0, 0);
}