/
htmldraw.js
231 lines (207 loc) · 7.95 KB
/
htmldraw.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
function tripleToRGBString(colour) {
s = "rgb(";
for (let i = 0; i < 3; ++i) {
s += colour[i].toString();
if (i < 2)
s += ",";
}
s += ")";
return s;
}
function tripleToRGBAString(colour, alpha) {
s = "rgba(";
for (let i = 0; i < 3; ++i) {
s += colour[i].toString() + ", ";
}
s += alpha.toString() + ")";
return s;
}
/* Scan "src" and replace every run of consecutive occurrences of the character
* "seek" with a <span style="visibility: hidden"></span> tag, whose contents
* is that same number of occurrences of the character "repl" and return the
* result. */
function replaceWithInvisible(src, seek, repl) {
let dest = "";
let srcPos = 0;
while (srcPos < src.length) {
if (src.charAt(srcPos) == seek) {
dest += "<span style=\"visibility: hidden\">"
while (srcPos < src.length && src.charAt(srcPos) == seek) {
dest += repl;
srcPos++;
}
dest += "</span>";
}
else {
dest += src.charAt(srcPos++);
}
}
return dest;
}
class HTMLBoxClockDesign extends ClockDesign {
constructor(container, uniqueClassName) {
super();
this.container = container;
this.div = document.createElement("DIV");
this.div.classList.add(uniqueClassName);
this.div.style.position = "absolute";
this.div.style.fontFamily = [ this.fontFamily, "sans-serif" ];
this.div.style.fontVariantNumeric = "tabular-nums";
this.div.innerHTML = "0";
this.container.appendChild(this.div);
this.showBorder = true;
this.clockBackgroundColour = [ 0, 0, 0 ];
this.clockBackgroundAlpha = 0.7;
this.styleChanged = true;
this.previousStringLength = 0;
this.applyStyle();
this.div.style.display = "none"; // don't display anything yet
}
setOutline(outlineColour, outlineSize) {
if (outlineSize > 0)
outlineSize = 0.5;
else
outlineSize = 0;
super.setOutline(outlineColour, outlineSize);
}
setClockBackgroundColour(clockBackgroundColour, clockBackgroundAlpha) {
this.clockBackgroundColour = clockBackgroundColour;
this.clockBackgroundAlpha = clockBackgroundAlpha;
this.styleChanged = true;
}
supportsClockBackground() {
return true;
}
supportsFonts() {
return true;
}
applyStyle() {
let charHeightPx = (this.autoMaximise ? 1 : this.scaleFactor) * this.container.clientHeight / 15;
let lengthUnitPx = charHeightPx / 40;
this.div.style.fontSize = charHeightPx.toString() + "px";
this.div.style.color = tripleToRGBString(this.textColour);
this.div.style.display = "block";
this.div.style.paddingLeft = "0.25em";
this.div.style.paddingRight = "0.25em";
this.div.style.fontFamily = [ this.fontFamily, "sans-serif" ];
if (this.fontFamily == "'Press Start 2P'") {
/* Press Start 2P has zero padding on top; fix that */
this.div.style.paddingTop = (charHeightPx / 8).toString() + "px";
}
else {
this.div.style.paddingTop = null;
}
if (this.showBorder) {
this.div.style.backgroundColor = tripleToRGBAString(this.clockBackgroundColour, this.clockBackgroundAlpha);
}
else {
this.div.style.backgroundColor = null;
}
let shadows = [];
if (this.outlineSize > 0) {
/* We abuse shadows to make an outline, so we don't allow an
* outline size of greater than 0.5 or it looks awful. */
for (let d = 0; d < 8; ++d) {
let shadowPx = this.outlineSize * lengthUnitPx;
shadows.push((directionToXY[d][0] * shadowPx).toString() + "px " +
(directionToXY[d][1] * shadowPx).toString() + "px " +
shadowPx.toString() + "px " +
tripleToRGBString(this.outlineColour));
}
}
if (this.shadowLength > 0) {
let horizShadow = directionToXY[this.shadowDirection][0] * this.shadowLength * lengthUnitPx;
let vertShadow = directionToXY[this.shadowDirection][1] * this.shadowLength * lengthUnitPx;
let blurRadius = (Math.abs(horizShadow) > Math.abs(vertShadow) ? Math.abs(horizShadow) : Math.abs(vertShadow));
shadows.push(horizShadow.toString() + "px " +
vertShadow.toString() + "px " +
blurRadius.toString() + "px black");
}
let shadowString = "";
if (shadows.length == 0) {
shadowString = "none";
}
else {
for (let i = 0; i < shadows.length; ++i) {
if (i > 0)
shadowString += ", ";
shadowString += shadows[i];
}
}
this.div.style.textShadow = shadowString;
this.div.style.transform = null;
if (this.autoCentre) {
let canvasWidth = this.container.clientWidth;
let canvasHeight = this.container.clientHeight;
this.div.style.left = (canvasWidth / 2).toString() + "px";
this.div.style.top = (canvasHeight / 2).toString() + "px";
this.div.style.right = null;
this.div.style.bottom = null;
this.div.style.transform = "translate(-50%, -50%)";
if (this.autoMaximise) {
let xMax = this.maximiseRatio * canvasWidth / this.div.clientWidth;
let yMax = this.maximiseRatio * canvasHeight / this.div.clientHeight;
let scaleFactor = Math.min(xMax, yMax);
this.div.style.transform += " scale(" + scaleFactor.toString() + ")";
}
}
else {
if (this.xPosRightEdge) {
this.div.style.right = (100 - this.xPosPercent).toString() + "%";
this.div.style.left = null;
}
else {
this.div.style.left = this.xPosPercent.toString() + "%";
this.div.style.right = null;
}
if (this.yPosBottomEdge) {
this.div.style.top = null;
this.div.style.bottom = (100 - this.yPosPercent).toString() + "%";
}
else {
this.div.style.top = this.yPosPercent.toString() + "%";
this.div.style.bottom = null;
}
}
this.styleChanged = false;
}
drawClock(string) {
if (string.length != this.previousStringLength) {
/* If the length of the contents changes, we may need to resize
* the box, so redo the style. */
this.styleChanged = true;
}
this.previousStringLength = string.length;
if (this.fontFamily != "'Press Start 2P'") {
/* Replace - with &minus so it looks a bit more minusy, except
* don't do that with Press Start 2P because it doesn't have a
* specific − character. */
string = string.replace(/-/g, "−");
}
/* Replace every run of spaces with the same number of invisible digits.
* We can't use   because that turns out not always to be exactly
* the same width as a digit. When " 9" becomes "10" we don't want the
* size of the div to change. */
string = replaceWithInvisible(string, " ", "0");
this.div.innerHTML = string;
if (this.styleChanged) {
this.applyStyle();
}
}
clearClock() {
this.div.style.display = "none";
this.styleChanged = true;
}
getDefaultOptionValues() {
return {
"showborder" : true,
"outlinesize" : 0,
"shadowlength" : 2,
"shadowdir" : "3",
"clockbgcolor" : [0, 0, 0],
"clockbgalpha" : 70,
"fgcolor" : [255, 255, 255],
"outlinecolor" : [0, 0, 0]
};
}
}