-
Notifications
You must be signed in to change notification settings - Fork 0
/
cistercian.js
279 lines (264 loc) · 6.97 KB
/
cistercian.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
canvas.width = canvas.offsetWidth // * window.devicePixelRatio
canvas.height = canvas.offsetHeight// * window.devicePixelRatio
ctx.imageSmoothingEnabled = false;
ctx.lineWidth = 5
ctx.lineCap = "round"
function drawNumber(n) {
ctx.translate(0.5, 0.5);
// clear canvas
ctx.fillStyle = 'white'
ctx.fillRect(0, 0, canvas.width, canvas.height)
// always draw a vertical bar
var x = Math.floor(canvas.width / 2)
var y = Math.floor(canvas.height / 2)
var qt = Math.floor(y * 0.4) // quad top y start
var qb = Math.floor(y * 1.6) // quad bot y start
var qd = Math.floor((qb - qt) / 3) //quad dist
var t = 0 //temp
var given = n //for text drawing
ctx.beginPath()
ctx.moveTo(x, qt)
ctx.lineTo(x, qb)
ctx.closePath()
ctx.stroke()
// top right quadrant, 0 - 9
t = n % 10
n -= t
ctx.beginPath()
switch(t) {
case 1:
ctx.moveTo(x, qt)
ctx.lineTo(x+qd, qt)
break;
case 2:
ctx.moveTo(x, qt+qd)
ctx.lineTo(x+qd, qt+qd)
break;
case 3:
ctx.moveTo(x+qd, qt+qd)
ctx.lineTo(x, qt)
break;
case 4:
ctx.moveTo(x, qt+qd)
ctx.lineTo(x+qd, qt)
break;
case 5:
ctx.moveTo(x, qt+qd)
ctx.lineTo(x+qd, qt)
ctx.moveTo(x+qd, qt)
ctx.lineTo(x, qt)
break;
case 6:
ctx.moveTo(x+qd, qt)
ctx.lineTo(x+qd, qt+qd)
break;
case 7:
ctx.moveTo(x, qt)
ctx.lineTo(x+qd, qt)
ctx.moveTo(x+qd, qt)
ctx.lineTo(x+qd, qt+qd)
break;
case 8:
ctx.moveTo(x, qt+qd)
ctx.lineTo(x+qd, qt+qd)
ctx.moveTo(x+qd, qt+qd)
ctx.lineTo(x+qd, qt)
break;
case 9:
ctx.moveTo(x, qt+qd)
ctx.lineTo(x+qd, qt+qd)
ctx.moveTo(x+qd, qt+qd)
ctx.lineTo(x+qd, qt)
ctx.moveTo(x+qd, qt)
ctx.lineTo(x, qt)
break;
default: // 0 adds nothing to the figure
break;
}
ctx.closePath()
ctx.stroke()
// top left quadrant, 00 - 90
t = n % 100
n -= t
ctx.beginPath()
switch(t) {
case 10:
ctx.moveTo(x-qd, qt)
ctx.lineTo(x, qt)
break;
case 20:
ctx.moveTo(x-qd, qt+qd)
ctx.lineTo(x, qt+qd)
break;
case 30:
ctx.moveTo(x-qd, qt+qd)
ctx.lineTo(x, qt)
break;
case 40:
ctx.moveTo(x, qt+qd)
ctx.lineTo(x-qd, qt)
break;
case 50:
ctx.moveTo(x, qt+qd)
ctx.lineTo(x-qd, qt)
ctx.moveTo(x-qd, qt)
ctx.lineTo(x, qt)
break;
case 60:
ctx.moveTo(x-qd, qt)
ctx.lineTo(x-qd, qt+qd)
break;
case 70:
ctx.moveTo(x, qt)
ctx.lineTo(x-qd, qt)
ctx.moveTo(x-qd, qt)
ctx.lineTo(x-qd, qt+qd)
break;
case 80:
ctx.moveTo(x, qt+qd)
ctx.lineTo(x-qd, qt+qd)
ctx.moveTo(x-qd, qt+qd)
ctx.lineTo(x-qd, qt)
break;
case 90:
ctx.moveTo(x, qt+qd)
ctx.lineTo(x-qd, qt+qd)
ctx.moveTo(x-qd, qt+qd)
ctx.lineTo(x-qd, qt)
ctx.moveTo(x-qd, qt)
ctx.lineTo(x, qt)
break;
default: // 00 adds nothing to the figure
break;
}
ctx.closePath()
ctx.stroke()
// bottom right quadrant, 000 - 900
t = n % 1000
n -= t
ctx.beginPath()
switch(t) {
case 100:
ctx.moveTo(x+qd, qb)
ctx.lineTo(x, qb)
break;
case 200:
ctx.moveTo(x+qd, qb-qd)
ctx.lineTo(x, qb-qd)
break;
case 300:
ctx.moveTo(x+qd, qb-qd)
ctx.lineTo(x, qb)
break;
case 400:
ctx.moveTo(x, qb-qd)
ctx.lineTo(x+qd, qb)
break;
case 500:
ctx.moveTo(x, qb-qd)
ctx.lineTo(x+qd, qb)
ctx.moveTo(x+qd, qb)
ctx.lineTo(x, qb)
break;
case 600:
ctx.moveTo(x+qd, qb)
ctx.lineTo(x+qd, qb-qd)
break;
case 700:
ctx.moveTo(x, qb)
ctx.lineTo(x+qd, qb)
ctx.moveTo(x+qd, qb)
ctx.lineTo(x+qd, qb-qd)
break;
case 800:
ctx.moveTo(x, qb-qd)
ctx.lineTo(x+qd, qb-qd)
ctx.moveTo(x+qd, qb-qd)
ctx.lineTo(x+qd, qb)
break;
case 900:
ctx.moveTo(x, qb-qd)
ctx.lineTo(x+qd, qb-qd)
ctx.moveTo(x+qd, qb-qd)
ctx.lineTo(x+qd, qb)
ctx.moveTo(x+qd, qb)
ctx.lineTo(x, qb)
break;
default: // 000 adds nothing to the figure
break;
}
ctx.closePath()
ctx.stroke()
// bottom left quadrant, 0000 - 9000
t = n % 10000
n -= t
ctx.beginPath()
switch(t) {
case 1000:
ctx.moveTo(x-qd, qb)
ctx.lineTo(x, qb)
break;
case 2000:
ctx.moveTo(x-qd, qb-qd)
ctx.lineTo(x, qb-qd)
break;
case 3000:
ctx.moveTo(x-qd, qb-qd)
ctx.lineTo(x, qb)
break;
case 4000:
ctx.moveTo(x, qb-qd)
ctx.lineTo(x-qd, qb)
break;
case 5000:
ctx.moveTo(x, qb-qd)
ctx.lineTo(x-qd, qb)
ctx.moveTo(x-qd, qb)
ctx.lineTo(x, qb)
break;
case 6000:
ctx.moveTo(x-qd, qb)
ctx.lineTo(x-qd, qb-qd)
break;
case 7000:
ctx.moveTo(x, qb)
ctx.lineTo(x-qd, qb)
ctx.moveTo(x-qd, qb)
ctx.lineTo(x-qd, qb-qd)
break;
case 8000:
ctx.moveTo(x, qb-qd)
ctx.lineTo(x-qd, qb-qd)
ctx.moveTo(x-qd, qb-qd)
ctx.lineTo(x-qd, qb)
break;
case 9000:
ctx.moveTo(x, qb-qd)
ctx.lineTo(x-qd, qb-qd)
ctx.moveTo(x-qd, qb-qd)
ctx.lineTo(x-qd, qb)
ctx.moveTo(x-qd, qb)
ctx.lineTo(x, qb)
break;
default: // 0000 adds nothing to the figure
break;
}
ctx.closePath()
ctx.stroke()
ctx.translate(-0.5, -0.5);
ctx.fillStyle = 'black'
ctx.font = '48px serif'
ctx.fillText(given ? given : 1, x/5, y/3)
}
document.getElementById('generateNumber').addEventListener("click", function() {
var n = parseInt(document.getElementById('inputnumber').value)
if (0 > n < 10000) {
drawNumber(n)
} else {
// TODO error message
drawNumber(1)
}
});
drawNumber(1);