-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path19_2_efficient_drawing.html
32 lines (28 loc) · 1.01 KB
/
19_2_efficient_drawing.html
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
<div></div>
<script>
// Change this method
PictureCanvas.prototype.syncState = function(picture) {
if (this.picture == picture) return;
const prevPicture = this.picture;
this.picture = picture;
drawPicture(this.picture, this.dom, scale, prevPicture);
};
// You may want to use or change this as well
function drawPicture(picture, canvas, scale, prevPicture) {
if (!prevPicture || prevPicture.width != picture.width || prevPicture.height != picture.height) {
canvas.width = picture.width * scale;
canvas.height = picture.height * scale;
prevPicture = null;
}
let cx = canvas.getContext('2d');
for (let y = 0; y < picture.height; y++) {
for (let x = 0; x < picture.width; x++) {
if (!prevPicture || picture.pixel(x, y) !== prevPicture.pixel(x, y)) {
cx.fillStyle = picture.pixel(x, y);
cx.fillRect(x * scale, y * scale, scale, scale);
}
}
}
}
document.querySelector('div').appendChild(startPixelEditor({}));
</script>