-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcathanvas.js
72 lines (61 loc) · 1.83 KB
/
cathanvas.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
class Cathanvas {
/**
* @param containerID
* @param options Available properties: width, height, id, bgStyle
*/
constructor(containerID, options = {}) {
this.container = document.getElementById(containerID);
this.width = options.width || 800;
this.height = options.height || 600;
this.center = {x: this.width / 2, y: this.height / 2};
this.id = options.id || 'cathanvas';
this.canvas = document.createElement('canvas');
this.canvas.width = this.width;
this.canvas.height = this.height;
this.canvas.id = this.id;
this.context = this.canvas.getContext('2d');
this.container.appendChild(this.canvas);
this.bgStyle = options.bgStyle;
this.fillBg(options.bgStyle);
}
clearCanvas() {
this.context.clearRect(0, 0, this.width, this.height);
this.fillBg(this.bgStyle);
}
fillBg(style = '#000000') {
this.context.fillStyle = style;
this.context.fillRect(0, 0, this.width, this.height);
}
drawDot(coords, style = '#FFFFFF') {
this.context.fillStyle = style;
if (coords.x && coords.y) {
this.context.fillRect(coords.x, coords.y, 1, 1);
return;
}
this.context.fillRect(coords[0], coords[1], 1, 1);
}
drawLineFrom(source = [this.center.x, this.center.y], target, style) {
this.context.strokeStyle = style || '#FFFFFF';
this.context.moveTo(source[0], source[1]);
this.context.lineTo(target[0], target[1]);
this.context.stroke();
}
drawLineFromCenter(coords, style) {
this.context.strokeStyle = style || '#FFFFFF';
this.context.moveTo(this.center.x, this.center.y);
this.context.lineTo(coords.x, coords.y);
this.context.stroke();
}
/**
* Draws a set of lines
* @param coordArray [from, to, from, to, from, to, from to]
*/
drawLines(coordArray) {
}
animate() {
// clear canvas
// save canvas state
// draw animated shapes
// restore canvas state
}
}