-
Notifications
You must be signed in to change notification settings - Fork 17
/
canvas.js
104 lines (87 loc) · 2.12 KB
/
canvas.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
// jsSyntaxTree - A syntax tree graph generator
// (c)2019 Andre Eisenbach <andre@ironcreek.net>
'use strict';
export default class Canvas {
constructor(c) {
this.canvas = c;
this.font = 'sans-serif';
this.fontsize = 16;
this.context = c.getContext('2d');
}
resize(w, h) {
this.canvas.width = w;
this.canvas.height = h;
this.clear();
}
textWidth(t) {
this.context.font = this.fontsize + 'px ' + this.font;
return this.context.measureText(t).width;
}
clear() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.context.setTransform(1, 0, 0, 1, 0, 0);
this.context.textAlign = 'center';
this.context.textBaseline = 'top';
}
translate(x, y) {
this.context.translate(x, y);
}
text(t, x, y) {
this.context.font = this.fontsize + 'px ' + this.font;
this.context.fillText(t, x, y);
}
setFont(f) {
this.font = f;
}
setFontSize(s) {
this.fontsize = s;
}
setFillStyle(s) {
this.context.fillStyle = s;
}
setStrokeStyle(s) {
this.context.strokeStyle = s;
}
setLineWidth(w) {
this.context.lineWidth = w;
}
line(x1, y1, x2, y2) {
const ctx = this.context;
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}
triangle(x1, y1, x2, y2, x3, y3, fill = false) {
const ctx = this.context;
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.lineTo(x3, y3);
ctx.lineTo(x1, y1);
if (fill) ctx.fill();
ctx.stroke();
}
rect(x, y, w, h) {
const ctx = this.context;
ctx.beginPath();
ctx.rect(x, y, w, h);
ctx.stroke();
}
curve(x1, y1, x2, y2, cx1, cy1, cx2, cy2) {
const ctx = this.context;
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.bezierCurveTo(cx1, cy1, cx2, cy2, x2, y2);
ctx.stroke();
}
download(fn) {
const image = this.canvas.toDataURL('image/png')
.replace('image/png', 'image/octet-stream');
const link = document.createElement('a');
link.setAttribute('href', image);
link.setAttribute('download', fn);
link.click();
link.remove();
}
}