This repository was archived by the owner on Dec 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSlideIllustrated.js
More file actions
119 lines (90 loc) · 3.53 KB
/
Copy pathSlideIllustrated.js
File metadata and controls
119 lines (90 loc) · 3.53 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import Slide from './Slide.js';
import COLORS from './COLORS.js';
import UTILS from './UTILS.js';
export default class SlideIllustrated extends Slide {
constructor(canvas) {
super(canvas);
}
drawImage(x, y, id, filter) {
const image = document.getElementById(id);
switch (typeof filter) {
case 'string':
this.renderImageWithStandardFilter(x, y, image, filter);
break;
case 'object':
this.renderImageWithMatrixFilter(x, y, image, filter);
break;
default:
this.renderImageWithoutFilter(x, y, image);
break;
}
}
drawImageWithConvolveMatrixFilter(x, y, id, filter, factor) {
const image = document.getElementById(id);
this.renderImageWithConvolveMatrixFilter(x, y, image, filter, factor);
}
renderImageWithoutFilter(x, y, image) {
this.context.drawImage(image, x, y, 100, 100);
}
renderImageWithStandardFilter(x, y, image, filter) {
this.context.filter = filter;
this.context.drawImage(image, x, y, 100, 100);
this.context.filter = 'none';
}
renderImageWithMatrixFilter(x, y, image, filter) {
this.context.drawImage(image, x, y, 100, 100);
const imageData = this.context.getImageData(x, y, 100, 100);
const data = imageData.data;
for (let i = 0; i < data.length; i += 4) {
const color = UTILS.multiplyMatrices(filter,
[
[data[i]],
[data[i + 1]],
[data[i + 2]],
[data[i + 3]]
]
);
data[i] = color[0];
data[i + 1] = color[1];
data[i + 2] = color[2];
data[i + 3] = color[3];
}
this.context.putImageData(imageData, x, y);
}
renderImageWithConvolveMatrixFilter(positionX, positionY, image, filter, factor) {
this.context.drawImage(image, positionX, positionY, 100, 100);
const imageData = this.context.getImageData(positionX, positionY, 100, 100);
const data = imageData.data;
const newImageData = this.context.createImageData(imageData);
const newData = newImageData.data;
const delta = (filter[0].length - 1) / 2;
for (let pixel = 0; pixel < data.length; pixel += 4) {
let y = Math.floor(pixel / 100);
let x = pixel % 100;
let R = 0;
let G = 0;
let B = 0;
let A = 255;
for (let i = -delta; i < delta; i++) {
for (let j = -delta; j < delta; j++) {
const partPixel = (y + j*4) * 100 + x + i*4;
const multiplier = filter[j + delta][i + delta];
if (data[partPixel]) {
R += data[partPixel] * multiplier;
G += data[partPixel + 1] * multiplier;
B += data[partPixel + 2] * multiplier;
} else {
R += data[pixel] * multiplier;
G += data[pixel + 1] * multiplier;
B += data[pixel + 2] * multiplier;
}
}
}
newData[pixel] = R * factor;
newData[pixel + 1] = G * factor;
newData[pixel + 2] = B * factor;
newData[pixel + 3] = A;
}
this.context.putImageData(newImageData, positionX, positionY);
}
}