-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw_pixels.js
130 lines (78 loc) · 2.91 KB
/
draw_pixels.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
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
119
120
121
122
123
124
125
126
127
128
129
130
class Canvas {
_canvas;
_context;
_pix_dim;
_plot;
_mouse_is_down;
_mouse_is_inside;
constructor(candiv_id, can_dim, pix_dim) {
let canvasDiv = document.getElementById(candiv_id);
this._canvas = document.createElement('canvas');
this._canvas.style.width = can_dim + 'px';
this._canvas.style.height = can_dim + 'px';
this._canvas.width = can_dim;
this._canvas.height = can_dim;
this._canvas.style.border = '1px solid black';
canvasDiv.appendChild(this._canvas);
this._context = this._canvas.getContext('2d');
this._pix_dim = pix_dim;
this._plot = [];
this._draw();
this._canvas.addEventListener('mouseleave', () => {
this._mouse_is_inside = false;
});
this._canvas.addEventListener('mouseenter', () => {
this._mouse_is_inside = true;
});
this._canvas.addEventListener('mouseup', () => {
this._mouse_is_down = false;
});
this._canvas.addEventListener('mousedown', (e) => {
this._mouse_is_down = true;
this._handle_drawing(e);
});
this._canvas.addEventListener('mousemove', (e) => {
this._handle_drawing(e);
});
}
_handle_drawing(e) {
if(this._mouse_is_down && this._mouse_is_inside) {
let pix_size = this._canvas.width / this._pix_dim;
let can_pos = this._canvas.getBoundingClientRect();
let x = Math.floor((e.clientX - can_pos.left)/pix_size);
let y = Math.floor((e.clientY - can_pos.top)/pix_size);
this._plot.push(x);
this._plot.push(y);
this._draw();
}
}
_draw() {
this._context.clearRect(0, 0, this._canvas.width, this._canvas.height);
let pix_x, pix_y;
for(let i = 1; i < this._pix_dim; i++) {
// Vertical Bars
pix_x = this._canvas.width/this._pix_dim * i;
pix_y = this._canvas.height;
this._context.beginPath();
this._context.moveTo(pix_x, 0);
this._context.lineTo(pix_x, pix_y);
this._context.stroke();
// Horizonal Bars
pix_x = this._canvas.width;
pix_y = this._canvas.height/this._pix_dim * i;
this._context.beginPath();
this._context.moveTo(0, pix_y);
this._context.lineTo(pix_x, pix_y);
this._context.stroke();
}
let pixel_width = this._canvas.width/this._pix_dim;
let pixel_height = this._canvas.height/this._pix_dim;
for(let i = 0; i < this._plot.length/2; i++) {
this._context.fillRect(this._plot[2*i] * pixel_width, this._plot[2*i+1] * pixel_height, pixel_width, pixel_height);
}
}
}
let canvas;
window.onload = function() {
canvas = new Canvas('canvas-div', 400, 30);
}