-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandwrittenimage.js
47 lines (32 loc) · 1.18 KB
/
handwrittenimage.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
class HandWrittenImage {
constructor(){
this.canvas = document.getElementById('canvas');
this.text = document.getElementById('answer');
this.canvasWidth = this.canvas.width;
this.canvasHeight = this.canvas.height;
this.ctx = canvas.getContext('2d');
this.imageData = this.ctx.getImageData(0, 0, this.canvasWidth, this.canvasHeight);
this.pixels = this.imageData.data;
}
draw(gues = '_'){
this.text.textContent = "hmm... I'm thinking its a: " + gues + "?";
for (let y = 0; y < this.canvasHeight; ++y) {
for (let x = 0; x < this.canvasWidth; ++x) {
let index = (y * this.canvasWidth + x) * 4;
this.pixels[index] = this.data[x * this.canvasWidth + y]; // red
this.pixels[++index] = this.data[x * this.canvasWidth + y]; // green
this.pixels[++index] = this.data[x * this.canvasWidth + y]; // blue
this.pixels[++index] = 255; // alpha
}
}
this.ctx.putImageData(this.imageData, 0, 0);
}
refresh(imagejson){
this.answer = Object.keys(imagejson)[0];
this.data = imagejson[this.answer];
this.draw();
}
getAnswer(){
return parseInt(this.answer);
}
}