1 |
|
2 | import CanvasDatapoint from './datapoint';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | class Canvas {
|
14 | constructor(el, options) {
|
15 |
|
16 | this.canvas = {
|
17 | element: el,
|
18 | context: el.getContext('2d'),
|
19 | };
|
20 |
|
21 |
|
22 | window.addEventListener('resize', () => this.resize());
|
23 | this.resize();
|
24 |
|
25 |
|
26 | this.options = {
|
27 | continuousClick: false,
|
28 | continuousClickInterval: 50,
|
29 | x1: -2.5,
|
30 | y1: -2.5,
|
31 | x2: 2.5,
|
32 | y2: 2.5,
|
33 | ...options,
|
34 | };
|
35 |
|
36 |
|
37 | this.listeners = new Map();
|
38 |
|
39 |
|
40 | this.elements = [];
|
41 |
|
42 |
|
43 | this.classesBoundaries = {};
|
44 |
|
45 |
|
46 | this.weights = null;
|
47 | this.multiWeights = null;
|
48 |
|
49 |
|
50 | this.handleMouseEvents();
|
51 |
|
52 |
|
53 | window.requestAnimationFrame(() => this.refresh());
|
54 |
|
55 |
|
56 | this.tmp = {};
|
57 | this.tmp.predFeatures = [];
|
58 | this.tmp.predLabels = [];
|
59 | }
|
60 |
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | addListener(label, callback) {
|
68 | if (!this.listeners.has(label)) {
|
69 | this.listeners.set(label, []);
|
70 | }
|
71 |
|
72 | this.listeners.get(label).push(callback);
|
73 | }
|
74 |
|
75 | |
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | removeListener(label, callback) {
|
82 | const listeners = this.listeners.get(label);
|
83 |
|
84 | if (listeners) {
|
85 | this.listeners.set(label, listeners.filter(
|
86 | x => !(typeof x === 'function' && x === callback)
|
87 | ));
|
88 | }
|
89 | }
|
90 |
|
91 | |
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | emit(label, ...args) {
|
99 | const listeners = this.listeners.get(label);
|
100 |
|
101 | if (listeners) {
|
102 | listeners.forEach((listener) => { listener(...args); });
|
103 | return true;
|
104 | }
|
105 |
|
106 | return false;
|
107 | }
|
108 |
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 | addDatapoint(datapoint) {
|
115 | this.elements.push(new CanvasDatapoint(this, datapoint));
|
116 | }
|
117 |
|
118 | |
119 |
|
120 |
|
121 | handleMouseEvents() {
|
122 | if (this.options.continuousClick) {
|
123 | this.mouseStatus = 0;
|
124 | this.mouseX = 0;
|
125 | this.mouseY = 0;
|
126 |
|
127 | this.canvas.element.addEventListener('mousedown', () => {
|
128 | this.mouseStatus = 1;
|
129 | this.continuousClickIntervalId = setInterval(
|
130 | () => this.click(),
|
131 | this.options.continuousClickInterval
|
132 | );
|
133 | });
|
134 |
|
135 | document.addEventListener('mouseup', () => {
|
136 | this.mouseStatus = 0;
|
137 | clearInterval(this.continuousClickIntervalId);
|
138 | });
|
139 |
|
140 | document.addEventListener('mousemove', (e) => {
|
141 | [this.mouseX, this.mouseY] =
|
142 | this.transformAbsolutePositionToRelativePosition(e.clientX, e.clientY);
|
143 | });
|
144 | }
|
145 |
|
146 | this.canvas.element.addEventListener('mousedown', (e) => {
|
147 | this.click(...this.transformAbsolutePositionToRelativePosition(e.clientX, e.clientY));
|
148 | });
|
149 | }
|
150 |
|
151 | |
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | transformAbsolutePositionToRelativePosition(x, y) {
|
160 |
|
161 | const el = this.canvas.element;
|
162 | const rect = el.getBoundingClientRect();
|
163 |
|
164 | return [x - rect.left, y - rect.top];
|
165 | }
|
166 |
|
167 | |
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | click(x = -1, y = -1) {
|
176 | let clickX = x;
|
177 | let clickY = y;
|
178 |
|
179 | if (x === -1) {
|
180 | clickX = this.mouseX;
|
181 | clickY = this.mouseY;
|
182 | }
|
183 |
|
184 |
|
185 | const [px, py] = this.convertCanvasCoordinatesToFeatures(clickX, clickY);
|
186 |
|
187 | this.emit('click', px, py);
|
188 | }
|
189 |
|
190 | |
191 |
|
192 |
|
193 | clear() {
|
194 | this.canvas.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
195 | }
|
196 |
|
197 | |
198 |
|
199 |
|
200 | resize() {
|
201 | this.canvas.element.style.width = '100%';
|
202 | this.canvas.element.style.height = '100%';
|
203 | this.canvas.element.width = this.canvas.element.offsetWidth * window.devicePixelRatio;
|
204 | this.canvas.element.height = this.canvas.element.offsetHeight * window.devicePixelRatio;
|
205 | this.canvas.width = this.canvas.element.offsetWidth;
|
206 | this.canvas.height = this.canvas.element.offsetHeight;
|
207 | this.canvas.context.scale(window.devicePixelRatio, window.devicePixelRatio);
|
208 | }
|
209 |
|
210 | redraw() {
|
211 |
|
212 | this.clear();
|
213 |
|
214 |
|
215 | this.drawGrid();
|
216 | this.drawAxes();
|
217 |
|
218 |
|
219 | this.elements.forEach((element) => {
|
220 | element.draw();
|
221 | });
|
222 |
|
223 |
|
224 | this.drawClassBoundaries();
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 | window.requestAnimationFrame(() => this.refresh());
|
250 | }
|
251 |
|
252 | |
253 |
|
254 |
|
255 | refresh() {
|
256 |
|
257 | this.elements.forEach((element) => {
|
258 | element.update();
|
259 | });
|
260 |
|
261 | this.redraw();
|
262 | }
|
263 |
|
264 | setWeightVector(weights) {
|
265 | this.weights = weights;
|
266 | }
|
267 |
|
268 | |
269 |
|
270 |
|
271 |
|
272 |
|
273 | setClassBoundaries(classesBoundaries) {
|
274 | this.classesBoundaries = classesBoundaries;
|
275 | }
|
276 |
|
277 | drawWeightVector(weights) {
|
278 | if (weights) {
|
279 | let fromX;
|
280 | let fromY;
|
281 | let toX;
|
282 | let toY;
|
283 |
|
284 |
|
285 |
|
286 | if (Math.abs(weights[1]) > Math.abs(weights[2])) {
|
287 | fromX = -1;
|
288 | fromY = -(weights[0] - weights[1]) / weights[2];
|
289 | toX = 1;
|
290 | toY = -(weights[0] + weights[1]) / weights[2];
|
291 | } else {
|
292 | fromY = -1;
|
293 | fromX = -(weights[0] - weights[2]) / weights[1];
|
294 | toY = 1;
|
295 | toX = -(weights[0] + weights[2]) / weights[1];
|
296 | }
|
297 |
|
298 | const canvas = this.canvas;
|
299 | const context = canvas.context;
|
300 |
|
301 | fromX = (fromX + 1) / 2;
|
302 | fromY = 1 - (fromY + 1) / 2;
|
303 | toX = (toX + 1) / 2;
|
304 | toY = 1 - (toY + 1) / 2;
|
305 |
|
306 | context.beginPath();
|
307 | context.moveTo(fromX * canvas.width, fromY * canvas.height);
|
308 | context.lineTo(toX * canvas.width, toY * canvas.height);
|
309 | context.lineWidth = 3;
|
310 |
|
311 | context.stroke();
|
312 | }
|
313 | }
|
314 |
|
315 | |
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 | convertCanvasCoordinatesToFeatures(x, y) {
|
326 |
|
327 | let f1 = x / this.canvas.width;
|
328 | let f2 = y / this.canvas.height;
|
329 |
|
330 |
|
331 | f1 = this.options.x1 + f1 * (this.options.x2 - this.options.x1);
|
332 | f2 = this.options.y1 + (1 - f2) * (this.options.y2 - this.options.y1);
|
333 |
|
334 | return [f1, f2];
|
335 | }
|
336 |
|
337 | |
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 | convertBoundaryCoordinatesToFeatures(bx, by) {
|
347 | const f1 = this.options.x1 + (bx + 1) / 2 * (this.options.x2 - this.options.x1);
|
348 | const f2 = this.options.y1 + (by + 1) / 2 * (this.options.y2 - this.options.y1);
|
349 |
|
350 | return [f1, f2];
|
351 | }
|
352 |
|
353 | |
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 | convertFeaturesToCanvasCoordinates(f1, f2) {
|
362 | const x = (f1 - this.options.x1) / (this.options.x2 - this.options.x1);
|
363 | const y = 1 - ((f2 - this.options.y1) / (this.options.y2 - this.options.y1));
|
364 |
|
365 | return [x * this.canvas.width, y * this.canvas.height];
|
366 | }
|
367 |
|
368 | |
369 |
|
370 |
|
371 | drawGrid() {
|
372 | const canvas = this.canvas;
|
373 | const context = canvas.context;
|
374 |
|
375 |
|
376 | for (let i = 1; i < 10; i += 1) {
|
377 |
|
378 | context.beginPath();
|
379 | context.moveTo(0, i / 10 * canvas.height);
|
380 | context.lineTo(canvas.width, i / 10 * canvas.height);
|
381 | context.lineWidth = 1;
|
382 | context.strokeStyle = '#EAEAEA';
|
383 | context.stroke();
|
384 |
|
385 |
|
386 | context.beginPath();
|
387 | context.moveTo(i / 10 * canvas.width, 0);
|
388 | context.lineTo(i / 10 * canvas.width, canvas.height);
|
389 | context.lineWidth = 1;
|
390 | context.strokeStyle = '#EAEAEA';
|
391 | context.stroke();
|
392 | }
|
393 | }
|
394 |
|
395 | |
396 |
|
397 |
|
398 | drawAxes() {
|
399 | const canvas = this.canvas;
|
400 | const context = canvas.context;
|
401 |
|
402 |
|
403 | const [originX, originY] = this.convertFeaturesToCanvasCoordinates(0, 0);
|
404 |
|
405 |
|
406 | context.beginPath();
|
407 | context.moveTo(0, originY);
|
408 | context.lineTo(canvas.width, originY);
|
409 | context.lineWidth = 2;
|
410 | context.strokeStyle = '#CCC';
|
411 | context.stroke();
|
412 |
|
413 |
|
414 | context.beginPath();
|
415 | context.moveTo(originX, 0);
|
416 | context.lineTo(originX, canvas.height);
|
417 | context.lineWidth = 2;
|
418 | context.strokeStyle = '#CCC';
|
419 | context.stroke();
|
420 | }
|
421 |
|
422 | |
423 |
|
424 |
|
425 | drawClassBoundaries() {
|
426 | const context = this.canvas.context;
|
427 |
|
428 | Object.keys(this.classesBoundaries).forEach((classLabel) => {
|
429 | const classBoundaries = this.classesBoundaries[classLabel];
|
430 |
|
431 |
|
432 | context.beginPath();
|
433 |
|
434 | classBoundaries.forEach((classBoundary) => {
|
435 | let firstpoint = true;
|
436 |
|
437 | classBoundary.forEach((boundaryPoint) => {
|
438 | const [xx, yy] = this.convertFeaturesToCanvasCoordinates(
|
439 | ...this.convertBoundaryCoordinatesToFeatures(boundaryPoint[0], boundaryPoint[1])
|
440 | );
|
441 |
|
442 | if (firstpoint) {
|
443 | firstpoint = false;
|
444 | context.moveTo(xx, yy);
|
445 | } else {
|
446 | context.lineTo(xx, yy);
|
447 | }
|
448 |
|
449 | if (Math.abs(boundaryPoint[0]) !== 1 && Math.abs(boundaryPoint[1]) !== 1) {
|
450 | context.fillStyle = this.getClassColor(classLabel);
|
451 | context.fillStyle = '#000';
|
452 | context.globalAlpha = 0.25;
|
453 |
|
454 | context.globalAlpha = 1;
|
455 | }
|
456 |
|
457 |
|
458 |
|
459 | });
|
460 |
|
461 | context.closePath();
|
462 | });
|
463 |
|
464 | context.fillStyle = '#5DA5DA';
|
465 | context.strokeStyle = '#5DA5DA';
|
466 | context.fillStyle = this.getClassColor(classLabel);
|
467 | context.strokeStyle = this.getClassColor(classLabel);
|
468 | context.globalAlpha = 0.5;
|
469 | context.fill();
|
470 | context.globalAlpha = 1;
|
471 |
|
472 | });
|
473 | }
|
474 |
|
475 | |
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 | getClassColor(classIndex) {
|
482 | const colors = this.getColors();
|
483 | return colors[Object.keys(colors)[parseInt(classIndex, 10)]];
|
484 | }
|
485 |
|
486 | |
487 |
|
488 |
|
489 |
|
490 |
|
491 | getColors() {
|
492 | return {
|
493 | blue: '#5DA5DA',
|
494 | orange: '#FAA43A',
|
495 | green: '#60BD68',
|
496 | pink: '#F17CB0',
|
497 | brown: '#B2912F',
|
498 | purple: '#B276B2',
|
499 | yellow: '#DECF3F',
|
500 | red: '#F15854',
|
501 | gray: '#4D4D4D',
|
502 | };
|
503 | }
|
504 | }
|
505 |
|
506 | export default Canvas;
|