-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
634 lines (629 loc) · 21.1 KB
/
main.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
'use strict';
// HTML
$('input[type=range]').rangeslider({
polyfill: false,
onSlide: (self, val, pos) => {
let key = self.$element[0].getAttribute('key');
localStorage.setItem(key, val);
Musics[key].forEach(e => {
e.volume = val / 100;
})
}
});
// Canvas
/** @type {HTMLCanvasElement} */ // 宣告作業環境
const content = document.getElementById('content');
const canvas = document.getElementById('canvas'); // 取得畫布
const ctx = canvas.getContext('2d'); // 宣告2D畫布
// 物件導向
class Pixel {
constructor(args) {
let def = {
y: 0,
x: 0
}
Object.assign(def, args);
Object.assign(this, def);
}
draw() {
ctx.putImageData(this.img, this.x, this.y);
}
move() {
this.x -= this.mx;
this.y += this.my;
if (this.x + this.scale <= Gui.span || this.y + this.scale <= Gui.span || this.y >= (Gui.screen.y - 1) * Gui.span || this.x >= (Gui.screen.x - 1) * Gui.span) return true;
this.mx += this.vx;
this.my += this.vy;
}
}
class Player {
constructor(args) {
let def = {
y: 0,
x: 0,
tmpset: 0,
speed: 1000
}
Object.assign(def, args);
Object.assign(this, def);
this.Getblock();
}
Getblock() {
if (this.N_block === undefined) {
let Rand = rand(0, 6);
this.N_block = Block[Rand];
this.N_blockind = Rand;
}
let Rand = rand(0, 6);
this.Block = this.N_block;
this.Blockind = this.N_blockind;
this.x = Math.floor(Gui.screen.x / 2) - 1 - (this.Blockind === 6);
this.y = 1;
this.N_block = Block[Rand];
this.N_blockind = Rand;
this.tmpset = 0;
if (this.touch()) this.lose();
}
draw(forcibly = true) {
if (!start && forcibly) return;
// 陰影 & 自身邊框
for (let y = 0; y < Gui.screen.y; y++) {
if (this.touch(y + 1, 0)) {
this.border(y);
break;
}
}
// 方塊
let block = this.Block;
for (let y = 0; y < block.length; y++)
for (let x = 0; x < block[y].length; x++)
if (block[y][x])
decoration(y + this.y, x + this.x, Getdraw[block[y][x]], 1, 1, Gui.sqrt);
}
touch(y1 = 0, x1 = 0) {
while (this.Block === undefined) this.Getblock();
let block = this.Block;
for (let y = 0; y < block.length; y++)
for (let x = 0; x < block[y].length; x++)
if (block[y][x] && map[y + this.y + y1][x + this.x + x1]) return 1;
return 0;
}
down() {
let block = this.Block;
// 下降
if (this.touch(1, 0)) {
for (let y = 0; y < block.length; y++)
for (let x = 0; x < block[y].length; x++)
if (block[y][x]) map[y + this.y][x + this.x] = block[y][x];
this.Getblock();
}
else this.y++;
now = +new Date();
this.score();
}
score() {
let lines = 0;
for (let y = Gui.screen.y - 2; y > 0; y--) {
if (map[y].includes(0)) continue;
for (let y1 = y; y1 > 1; y1--) {
map[y1] = map[y1 - 1].slice();
map[1] = line.slice();
M_dele.currentTime = 0;
M_dele.play();
} y++;
lines++;
}
this.speed = Math.max(130, this.speed - lines * 11);
Gui.score += [0, 40, 100, 300, 1200][lines];
}
move(y = move.y, x = move.x) {
movetime = +new Date();
if ((y | x) === 0) return;
else if (y === 1) this.down();
else if (y === -1) this.transform();
else if (y | x && !this.touch(y, x)) {
this.y += y;
this.x += x;
}
M_move.play();
}
transform() {
move.y = 0;
if (this.Blockind === 1) return;
else {
let orig_block = this.Block;
let new_block = [];
for (let x = 0; x < orig_block[0].length; x++) {
new_block.push([]);
for (let y = 0; y < orig_block.length; y++)
new_block[x].push(orig_block[orig_block.length - y - 1][x]);
}
let x = [0, -1, 1, (this.Blockind == 6) ? -2 : 0].find(e => {
for (let y = 0; y < new_block.length; y++)
for (let x = 0; x < new_block[y].length; x++)
if (new_block[y][x] && map[y + this.y][x + this.x + e])
return false;
return true;
})
if (x === undefined) return;
player.x += x;
this.Block = new_block;
}
}
bottom() {
for (let y = 0; y < Gui.screen.y; y++) {
if (this.touch(y + 1, 0)) {
this.y += y;
this.down();
M_bottom.currentTime = 0;
M_bottom.play();
return;
}
}
}
border(y1 = 0) {
let block = this.Block;
ctx.beginPath();
ctx.save();
ctx.lineWidth = 2;
for (let y = 0; y < block.length; y++) {
for (let x = 0; x < block[y].length; x++) {
if (block[y][x]) {
let color = Getdraw[block[y][x]];
ctx.strokeStyle = `rgb(${color[0]},${color[1]},${color[2]})`;
ctx.fillStyle = `rgba(${color[0]},${color[1]},${color[2]},.4)`;
ctx.rect((x + this.x) * Gui.span, (y + this.y + y1) * Gui.span, Gui.span, Gui.span);
ctx.fillRect((x + this.x) * Gui.span, (y + this.y + y1) * Gui.span, Gui.span, Gui.span);
ctx.stroke();
}
}
}
ctx.restore();
ctx.closePath();
}
lose() {
draw_all = true;
if (start) play_bt.click();
Gui.draw();
Buttons.forEach(e => e.draw());
let px = [];
let span = Gui.span;
map.forEach((arr, y) => {
arr.forEach((val, x) => {
if (val % 8 === 0) return;
let scale = 3;
for (let i = 0; i < Math.pow(span / scale, 2); i++) {
let sign = { x: [-1, 1][rand(0, 1)], y: [-1, 1][rand(0, 1)] };
px.push(new Pixel({
x: span * x + i * scale % span,
y: span * y + Math.floor(i * scale / span) * scale,
vx: 0.1 * sign.x,
vy: 0.1 * sign.y,
mx: rand(100, 200) / 100 * sign.x,
my: rand(100, 200) / 100 * sign.y,
scale: scale,
img: ctx.getImageData(span * x + i * scale % span, span * y + Math.floor(i * scale / span) * scale, scale, scale)
}))
}
})
})
px.forEach(e => e.draw());
function move() {
ctx.fillStyle = 'rgb(65,65,65,0.2)';
ctx.fillRect(Gui.span, Gui.span, (Gui.screen.x - 2) * Gui.span, (Gui.screen.y - 2) * Gui.span);
px.forEach((e, i) => {
if (e.move()) {
px.splice(i, 1);
return;
}
e.draw();
})
for (let y = 0; y < Gui.screen.y; y++) {
decoration(y, 0, [160, 160, 160], 1, 1, Gui.sqrt);
decoration(y, Gui.screen.x - 1, [160, 160, 160], 1, 1, Gui.sqrt);
}
for (let x = 0; x < Gui.screen.x; x++) {
decoration(0, x, [160, 160, 160], 1, 1, Gui.sqrt);
decoration(Gui.screen.y - 1, x, [160, 160, 160], 1, 1, Gui.sqrt);
}
if (px.length) requestAnimationFrame(move);
else {
init();
Gui.text = 'U LOSE';
if (start) play_bt.click();
draw_all = false;
}
}
requestAnimationFrame(move)
}
set() {
if (this.tmpset) return;
else if (this.tmp_blockind === undefined) {
this.tmp_blockind = this.Blockind;
this.Getblock();
this.tmpset = 1;
M_move.play();
}
else {
this.Block = Block[this.tmp_blockind];
this.Blockind = [this.tmp_blockind, this.tmp_blockind = this.Blockind][0];
this.x = Math.floor(Gui.screen.x / 2) - 1 - (this.Blockind === 6);
this.y = 1;
this.tmpset = 1;
M_move.play();
}
}
}
class Button {
constructor(args) {
let def = {
font: '28px Arial',
textAlign: 'center',
textBaseline: 'middle',
color: 'rgb(222,222,222)',
bgcolor: 'rgba(0,0,0,0)',
x: Gui.screen.x * Gui.span,
y: 0,
sqrt: 8,
w: 4,
h: 2,
onhover: false,
hover: {}
}
Object.assign(def, SA(args));
Object.assign(def.hover, Dele(SA(def), ['x', 'y', 'onhover', 'hover', 'w', 'h', 'name']));
Object.assign(def.hover, args.hover);
Object.assign(this, def);
}
draw() {
ctx.save();
ctx.translate(this.x, this.y);
if (this.onhover) {
ctx.fillStyle = this.hover.bgcolor;
decoration(0, 0, this.hover.bgcolor.split(/[()]/)[1].split(',').map(e => { return parseInt(e) }), this.w, this.h, this.sqrt);
ctx.font = this.hover.font;
ctx.textAlign = this.hover.textAlign;
ctx.fillStyle = this.hover.color;
ctx.textBaseline = this.hover.textBaseline;
}
else {
ctx.fillStyle = this.bgcolor;
decoration(0, 0, this.bgcolor.split(/[()]/)[1].split(',').map(e => { return parseInt(e) }), this.w, this.h, this.hover.sqrt);
ctx.font = this.font;
ctx.textAlign = this.textAlign;
ctx.fillStyle = this.color;
ctx.textBaseline = this.textBaseline;
}
ctx.fillText(this.name, Gui.span * this.w / 2, Gui.span * this.h / 2 + 2);
ctx.restore();
}
click() { }
}
class GUI {
constructor(args) {
let def = {
x: 5,
y: 22,
score: 0,
sqrt: 7,
bc: [0, 254, 254],
span: 30,
screen: { x: 12, y: 22 },
text: 'S T O P',
}
Object.assign(def, args);
Object.assign(this, def);
}
init() {
this.score = 0;
}
draw() {
// 遊玩畫面(左)
ctx.fillStyle = 'gray';
ctx.fillRect(0, 0, ww, wh);
for (let y = 0; y < this.screen.y; y++) { // 畫方塊
for (let x = 0; x < this.screen.x; x++) {
let color = Getdraw[map[y][x]];
if (map[y][x]) decoration(y, x, color, 1, 1, this.sqrt);
else {
ctx.fillStyle = `rgb(${color[0]},${color[1]},${color[2]})`;
ctx.fillRect(x * this.span, y * this.span, this.span, this.span);
}
}
}
// 遊玩介面(右)
ctx.save();
ctx.translate(this.screen.x * this.span, 0);
for (let y = 0; y < this.y; y++)
for (let x = 0; x < this.x; x++)
decoration(y, x, Getdraw[8], 1, 1, this.sqrt);
let block = Block[player.N_blockind];
for (let y = 0; y < 2; y++) {
if (block[y] === undefined) block[y] = [];
for (let x = 0; x < 4; x++) {
if (block[y][x] !== undefined && block[y][x]) decoration(y + 1, x, Getdraw[block[y][x]], 1, 1, this.sqrt);
else {
ctx.clearRect(x * this.span, (y + 1) * this.span, this.span, this.span);
decoration(y + 1, x, [50, 50, 50, .08], 1, 1, this.sqrt);
}
}
}
this.tmp();
this.show_score();
ctx.restore();
if (!start && !draw_all) {
ctx.save();
player.draw(false);
ctx.fillStyle = 'rgba(80,80,80,0.9)';
ctx.fillRect(this.span, this.span, this.span * (this.screen.x - 2), this.span * (this.screen.y - 2));
ctx.font = '75px Arial';
ctx.shadowColor = 'rgb(225, 20, 225,0.8)';
ctx.shadowBlur = 15;
ctx.fillStyle = 'rgb(255, 50, 255,0.8)';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(this.text, Gui.screen.x * Gui.span / 2, Gui.screen.y * Gui.span / 2);
ctx.restore();
}
}
tmp() {
let block = Block[player.tmp_blockind];
for (let y = 0; y < 2; y++) {
for (let x = 0; x < 4; x++) {
ctx.save();
ctx.translate(x * this.span, (y + 19) * this.span);
if (block !== undefined && block[y][x]) decoration(0, 0, Getdraw[block[y][x]], 1, 1, this.sqrt);
else {
ctx.clearRect(0, 0, this.span, this.span);
decoration(0, 0, [50, 50, 50, .08], 1, 1, this.sqrt)
}
ctx.restore();
}
}
}
show_score() {
ctx.save();
// 區塊
ctx.translate(0, this.span * 4);
ctx.fillStyle = 'rgb(96, 226, 225)';
ctx.fillRect(0, 0, this.span * 4, this.span * 2);
// 文字
ctx.font = '26px Arial';
ctx.fillStyle = 'rgb(0, 50, 255)';
ctx.textAlign = 'right';
ctx.textBaseline = 'middle';
ctx.fillText(this.score.toString().padStart(8, '0'), this.span * 4, this.span + 3);
ctx.restore();
}
}
let Block = [
[[0, 1, 0], [1, 1, 1]],
[[2, 2], [2, 2]],
[[0, 0, 3], [3, 3, 3]],
[[4, 4, 0], [0, 4, 4]],
[[0, 5, 5], [5, 5, 0]],
[[6, 0, 0], [6, 6, 6]],
[[7, 7, 7, 7]]
];
let player;
let map = [];
let Gui = new GUI();
let start = false;
let draw_all = false;
let draw_player = false;
let Getdraw = [[64, 64, 64], [145, 0, 188], [220, 190, 25], [255, 165, 0], [255, 80, 80], [140, 190, 0], [0, 85, 254], [30, 220, 220], [160, 160, 160]];
let ww = (Gui.screen.x + Gui.x) * Gui.span;
let wh = Gui.screen.y * Gui.span;
canvas.width = ww;
canvas.height = wh;
content.style.height = `${wh}px`;
content.style.transform = `translate(-${ww * 0.6}px,-50%)`;
let now;
let movetime;
let line = [];
let move = { x: 0, y: 0 };
let keydown = 1;
let light = [190, -140, -220, 100]; // 亮度變化
// ================Button==================
let Buttons = [];
let play_bt = new Button({
name: 'Play',
y: 7 * Gui.span,
color: 'rgb(222,222,222)',
bgcolor: 'rgb(55,154,255)',
sqrt: 11,
hover: {
bgcolor: 'rgb(0, 102, 205)',
color: 'rgb(200,200,200)'
}
});
play_bt.click = () => {
start = !start;
Gui.text = 'S T O P';
this.name = (start) ? 'Pause' : 'Play';
if (start) {
M_bgm.play();
if (!draw_player) {
draw_player = true;
now = +new Date();
}
}
else M_bgm.pause();
}
let Update_bt = new Button({
name: 'Update',
y: 10 * Gui.span,
color: 'rgb(222,222,222)',
bgcolor: 'rgb(55,154,255)',
sqrt: 11,
hover: {
bgcolor: 'rgb(0, 102, 205)',
color: 'rgb(200,200,200)'
}
});
Update_bt.click = () => {
let Dom = document.getElementById('Update');
if (Dom.getAttribute('show') === 'true')
$('.show').attr('show', 'false');
else {
$('.show:not(#Update)').attr('show', 'false');
Dom.setAttribute('show', 'true');
}
}
let Control_bt = new Button({
name: 'Control',
y: 13 * Gui.span,
color: 'rgb(222,222,222)',
bgcolor: 'rgb(55,154,255)',
sqrt: 11,
hover: {
bgcolor: 'rgb(0, 102, 205)',
color: 'rgb(200,200,200)'
}
})
Control_bt.click = () => {
let Dom = document.getElementById('Control');
if (Dom.getAttribute('show') === 'true')
$('.show').attr('show', 'false');
else {
$('.show:not(#Control)').attr('show', 'false');
Dom.setAttribute('show', 'true');
}
}
let Shop_bt = new Button({
name: 'Shop',
y: 16 * Gui.span,
color: 'rgb(222,222,222)',
bgcolor: 'rgb(55,154,255)',
sqrt: 11,
hover: {
bgcolor: 'rgb(0, 102, 205)',
color: 'rgb(200,200,200)'
}
})
Shop_bt.click = () => {
let Dom = document.getElementById('Shop');
if (Dom.getAttribute('show') === 'true')
$('.show').attr('show', 'false');
else {
$('.show:not(#Shop)').attr('show', 'false');
Dom.setAttribute('show', 'true');
}
}
Buttons.push(play_bt, Update_bt, Control_bt, Shop_bt);
// ================Music===================
let M_bgm = document.createElement('audio');
M_bgm.src = 'music/bgm.mp3';
M_bgm.loop = true;
let M_move = document.createElement('audio');
M_move.src = 'music/move.mp3';
let M_bottom = document.createElement('audio');
M_bottom.src = 'music/bottom.mp3';
let M_dele = new Audio('music/dele.wav');
let Musics = {
'BackGround Music': [M_bgm],
'Sound Effect': [M_move, M_bottom, M_dele]
};
// ========================================
['BackGround Music', 'Sound Effect'].forEach(Element => {
if (localStorage.getItem(Element) === null) localStorage.setItem(Element, 20);
$(`[key='${Element}']`).val(localStorage.getItem(Element)).change();
})
function init() { // 初始化
draw_player = false;
M_bgm.currentTime = 0;
for (let y = 0; y < Gui.screen.y; y++) {
map[y] = [];
for (let x = 0; x < Gui.screen.x; x++) {
if (y && y + 1 != Gui.screen.y && x && x + 1 != Gui.screen.x) map[y][x] = 0;
else map[y][x] = 8;
}
}
now = +new Date();
movetime = +new Date();
player = new Player();
Gui.init();
line = map[1].slice();
}
window.addEventListener('keydown', e => {
if (!start) return;
let t = e.key;
if (t === ' ') player.bottom();
else if (t === 'Shift') player.set();
move = {
y: (['w', 'ArrowUp', 'W'].includes(t)) ? -1 : (['s', 'ArrowDown', 'S'].includes(t)) ? 1 : 0,
x: (['a', 'ArrowLeft', 'A'].includes(t)) ? -1 : (['d', 'ArrowRight', 'D'].includes(t)) ? 1 : 0
}
if (keydown) {
player.move();
movetime += 100;
}
keydown = 0;
})
window.addEventListener('keyup', e => {
let t = e.key;
move = {
y: (['w', 'ArrowUp', 'W', 's', 'ArrowDown', 'S'].includes(t)) ? 0 : move.y,
x: (['a', 'ArrowLeft', 'A', 'd', 'ArrowRight', 'D'].includes(t)) ? 0 : move.x
}
keydown = 1;
})
canvas.addEventListener('mousemove', mouse => {
let pos = { y: mouse.offsetY, x: mouse.offsetX };
if (Buttons.find(e => { if (pos.y >= e.y && pos.x > e.x && pos.y <= e.y + e.h * Gui.span && pos.x <= e.x + e.w * Gui.span) return true }) !== undefined) canvas.style.cursor = 'pointer';
else canvas.style.cursor = '';
Buttons.forEach(e => {
if (pos.y >= e.y && pos.x > e.x && pos.y <= e.y + e.h * Gui.span && pos.x <= e.x + e.w * Gui.span) e.onhover = true;
else e.onhover = false;
})
});
canvas.addEventListener('click', mouse => {
let pos = { y: mouse.offsetY, x: mouse.offsetX };
Buttons.forEach(e => {
if (pos.y >= e.y && pos.x > e.x && pos.y <= e.y + e.h * Gui.span && pos.x <= e.x + e.w * Gui.span) e.click();
});
});
function draw() {
if (draw_all) { requestAnimationFrame(draw); return };
Gui.draw();
player.draw();
Buttons.forEach(e => e.draw());
if (!start) { requestAnimationFrame(draw); return };
let now_time = +new Date();
if (now_time - movetime >= 60) { // 移動更新
player.move();
movetime = +new Date();
}
if (now_time - now >= player.speed) { // 更新
player.down();
now = +new Date();
}
requestAnimationFrame(draw);
}
function Dele(Obj, Arr) {
Arr.forEach(e => delete Obj[e]);
return Obj;
}
function SA(i) { return JSON.parse(JSON.stringify(i)) };
function rand(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min }; // 隨機整數,含最大值、最小值
function decoration(y = 0, x = 0, color, w = 1, h = 1, sqrt = 1) {
ctx.fillStyle = `rgba(${color[0]},${color[1]},${color[2]},${(color[3] === undefined) ? 1 : color[3]})`;
let span = Gui.span;
ctx.fillRect(x * span, y * span, span * w, span * h);
for (let i = 0; i < 4; i++) {
ctx.save();
ctx.beginPath();
ctx.translate(span * x + ((i && i < 3) ? span * w : 0), y * span + ((i >= 2) ? span * h : 0));
ctx.rotate(Math.PI / 2 * i);
ctx.moveTo(0, 0);
ctx.lineTo(sqrt, sqrt);
ctx.lineTo(span * ((i % 2) ? h : w) - sqrt, sqrt);
ctx.lineTo(span * ((i % 2) ? h : w), 0);
ctx.fillStyle = (light[i] <= 0) ? `rgba(${color[0] + color[0] / 255 * light[i]},${color[1] + color[1] / 255 * light[i]},${color[2] + color[2] / 255 * light[i]},${(color[3] === undefined) ? 1 : color[3]})` : `rgba(${color[0] + (255 - color[0]) / 255 * light[i]},${color[1] + (255 - color[1]) / 255 * light[i]},${color[2] + (255 - color[2]) / 255 * light[i]},${(color[3] === undefined) ? 1 : color[3]})`; // 亮度變化
ctx.fill();
ctx.closePath();
ctx.restore();
}
}
init();
requestAnimationFrame(draw);