-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions.js
145 lines (123 loc) · 3.55 KB
/
functions.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
const reset = onresize = () => {
scale = Math.min(innerWidth / width, innerHeight / height);
canvas.width = scale * width;
canvas.height = scale * height;
offsetX = (innerWidth - scale * width) / 2;
canvas.style.left = offsetX + 'px';
offsetY = (innerHeight - scale * height) / 2;
canvas.style.top = offsetY + 'px';
ctx.scale(scale, scale);
ctx.translate(width / 2, height / 2);
ctx.fillStyle = '#fff';
ctx.strokeStyle = '#fd9';
ctx.lineWidth = 2.5;
ctx.lineJoin = ctx.lineCap = 'round';
ctx.font = '30px Bookman Old Style, serif';
ctx.shadowColor = 'rgba(0,0,0,.5)';
ctx.shadowBlur = 2;
ctx.shadowOffsetX = ctx.shadowOffsetY = 2;
screens[currentScreen].render();
};
const drawLine = (x, y, parts) => {
ctx.save();
ctx.translate(x, y);
ctx.beginPath();
ctx.moveTo(parts[0], parts[1]);
for (var i = 2; i < parts.length; i += 2) {
ctx.lineTo(parts[i], parts[i + 1]);
}
ctx.stroke();
ctx.restore();
};
const drawPolygon = (x, y, parts) => {
const last = parts.length - 1;
ctx.save();
ctx.translate(x, y);
ctx.beginPath();
ctx.moveTo(parts[last - 1], parts[last]);
for (var i = 0; i < last; i += 2) {
ctx.lineTo(parts[i], parts[i + 1]);
}
ctx.stroke();
ctx.restore();
};
const fillPolygon = (x, y, parts) => {
const last = parts.length - 1;
ctx.save();
ctx.translate(x, y);
ctx.beginPath();
ctx.moveTo(parts[last - 1], parts[last]);
for (var i = 0; i < last; i += 2) {
ctx.lineTo(parts[i], parts[i + 1]);
}
ctx.fill();
ctx.restore();
};
const drawTriangle = (x, y) => {
drawPolygon(x, y, [-12, -14, -12, 14, 12, 0]);
};
const drawText = (text, align, x, y) => {
ctx.textAlign = align;
ctx.fillText(text, x, y);
};
const drawEllipse = (x, y, rx, ry=rx) => {
ctx.beginPath();
ctx.save();
ctx.scale(rx / ry, 1);
ctx.arc(x, y, ry, 0, 7, 0);
ctx.fill();
ctx.restore();
};
const drawRing = (x, y, r) => {
ctx.beginPath();
ctx.arc(x, y, r, 0, 7, 0);
ctx.stroke();
};
const drawMessage = text => {
ctx.fillStyle = '#000';
ctx.fillRect(-width / 2, -height / 2, width, height);
ctx.fillStyle = '#fff';
ctx.font = '18px Bookman Old Style, serif';
drawText(text, 'center', 0, 0);
const w = Math.max(100, ctx.measureText(text).width);
ctx.strokeStyle = '#999';
ctx.strokeRect(-w / 2 - 20, -30, w + 40, 50);
drawLine(-w / 2 - 25, -35, [w / 3, 0, -10, 0, -10, -10, 0, -10, 0, 20]);
drawLine(w / 2 + 25, -35, [-w / 3, 0, 10, 0, 10, -10, 0, -10, 0, 20]);
drawLine(-w / 2 - 25, 25, [w / 3, 0, -10, 0, -10, 10, 0, 10, 0, -20]);
drawLine(w / 2 + 25, 25, [-w / 3, 0, 10, 0, 10, 10, 0, 10, 0, -20]);
};
const getScreenPos = e => {
return {
x: (e.pageX - offsetX) / scale - width / 2,
y: (e.pageY - offsetY) / scale - height / 2
};
};
const clamp = pos => {
pos.x = Math.min(-20 + width / 2, Math.max(20 - width / 2, pos.x));
pos.y = Math.min(-20 + height / 2, Math.max(20 - height / 2, pos.y));
}
const wait = async (time) => {
return new Promise(resolve => {
setTimeout(resolve, time);
});
};
const transition = async () => {
transitioning = 1;
draggables = [];
dragging = false;
curtain.classList.toggle('black');
await wait(1000);
++currentScreen;
screens[currentScreen].init();
transitioning = 0;
reset();
curtain.classList.toggle('black');
await wait(1000);
document.location.hash = '#' + currentScreen;
};
const hasHitCircle = (e, a, b, r) => {
const {x, y} = getScreenPos(e);
const distance = Math.hypot(a - x, b - y);
return distance <= r;
};