-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.js
48 lines (39 loc) · 1.4 KB
/
text.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
"use strict"
class CanvasText {
constructor(canvas, text, font, x, y, maxWidth, strokeColor, strokeWidth, fillColor) {
this.canvas = canvas;
this.ctx = this.canvas.getContext("2d");
this.text = text;
this.font = font;
this.x = x;
this.y = y;
this.maxWidth = maxWidth;
this.strokeColor = strokeColor;
this.strokeWidth = strokeWidth;
this.fillColor = fillColor;
}
draw() {
// Sets the font, if defined.
if(this.font != undefined) {
this.ctx.font = this.font;
}
// Makes a stroke-only text, if stroke color is defined.
if(this.strokeColor != undefined) {
// Sets the context's stroke color.
this.ctx.strokeStyle = this.strokeColor;
// Changes context's stroke width, if defined.
if(this.strokeWidth != undefined) {
this.ctx.lineWidth = this.strokeWidth;
}
// Creates the Text
this.ctx.strokeText(this.text, this.x, this.y, this.maxWidth);
}
// Makes a fill-only text, if fill color is defined.
if(this.fillColor != undefined) {
// Sets the context's fill color.
this.ctx.fillStyle = this.fillColor;
// Creates the Text
this.ctx.fillText(this.text, this.x, this.y, this.maxWidth);
}
}
}