-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
282 lines (245 loc) · 10.8 KB
/
script.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
document.addEventListener("DOMContentLoaded", function () {
const displayCanvas = document.getElementById('demoCanvas');
const displayCtx = displayCanvas.getContext('2d');
const offscreenCanvas = document.createElement('canvas');
const offscreenCtx = offscreenCanvas.getContext('2d');
offscreenCanvas.width = 800; // Fixed dimension
offscreenCanvas.height = 600; // Fixed dimension
function resizeDisplayCanvas() {
displayCanvas.width = window.innerWidth;
displayCanvas.height = window.innerHeight;
}
window.addEventListener('resize', resizeDisplayCanvas);
resizeDisplayCanvas(); // Initialize canvas size on start
const Directions = {
NONE: 0,
TOP: 1,
BOTTOM: 2,
LEFT: 3,
RIGHT: 4
};
let text = "Hello, this is a classic Amiga-style sine text scroller with copper bars!";
let instructions = " Keys/click: Left/Right for scroller, Up/Down for copper. ";
let time = 0;
let scrollerSpeed = 60.0;
let copperSpeed = 2.0;
let flashOpacity = 0;
let activeDirection = Directions.NONE;
let font_width, font_height, baseline_offset;
const fontSize = 48; // Consistent font size
const letter_spacing = 4; // Configurable spacing between characters
const frequency = 0.05;
const amplitude = 50;
let y_offset;
let characterBitmap = {}; // Define the characterBitmap object here
function x_to_index(x) {
const total_width = offscreenCanvas.width + 2 * font_width;
let index = Math.round(((x % total_width) + total_width) % total_width);
return index;
}
function recalculateYOffset() {
y_offset = new Array(offscreenCanvas.width + 2 * font_width); // Initialize y_offset array here
for (let x = -font_width; x < offscreenCanvas.width + font_width; x++) {
const index = x_to_index(x);
y_offset[index] = Math.sin(x * frequency * (scrollerSpeed / 100)) * amplitude;
}
}
function calculateFontDimensions() {
let unionTop = 0;
let unionBottom = 0;
let unionRight = 0;
const uniqueChars = [...new Set(text.split(''))];
const tempCanvas = document.createElement('canvas');
const tempCtx = tempCanvas.getContext('2d');
tempCtx.font = `${fontSize}px 'Press Start 2P'`;
uniqueChars.forEach(char => {
const metrics = tempCtx.measureText(char);
const boundingBox = {
top: -metrics.actualBoundingBoxAscent,
bottom: metrics.actualBoundingBoxDescent,
right: Math.ceil(metrics.width)
};
unionTop = Math.max(unionTop, Math.abs(boundingBox.top));
unionBottom = Math.max(unionBottom, boundingBox.bottom);
unionRight = Math.max(unionRight, boundingBox.right);
});
font_width = unionRight + letter_spacing;
font_height = unionTop + unionBottom;
baseline_offset = unionTop;
}
function initializeCharacterBitmaps() {
const uniqueChars = [...new Set(text.split(''))];
uniqueChars.forEach(char => {
const charCanvas = document.createElement('canvas');
charCanvas.width = font_width;
charCanvas.height = font_height;
const charCtx = charCanvas.getContext('2d');
charCtx.font = `${fontSize}px 'Press Start 2P'`;
charCtx.fillStyle = "white";
const textWidth = charCtx.measureText(char).width;
const centeredX = (font_width - textWidth) / 2;
charCtx.fillText(char, centeredX, baseline_offset);
characterBitmap[char] = charCanvas;
});
}
function restartDemo() {
calculateFontDimensions();
initializeCharacterBitmaps();
recalculateYOffset();
}
function update() {
time += 0.05;
if (flashOpacity > 0) {
flashOpacity -= 0.01;
} else {
activeDirection = Directions.NONE;
}
}
function render() {
update();
offscreenCtx.clearRect(0, 0, offscreenCanvas.width, offscreenCanvas.height);
drawCopperBars();
drawScrollingText();
drawInstructions();
displayCtx.clearRect(0, 0, displayCanvas.width, displayCanvas.height);
displayCtx.drawImage(offscreenCanvas, 0, 0, displayCanvas.width, displayCanvas.height);
drawActiveTriangle();
requestAnimationFrame(render);
}
function drawScrollingText() {
let baseX = offscreenCanvas.width - ((time * scrollerSpeed) % (text.length * (font_width + letter_spacing) + offscreenCanvas.width));
for (let i = 0; i < text.length; i++) {
let x = baseX + i * (font_width + letter_spacing);
drawCharacterWithSineWave(x, text[i]);
}
}
function drawCharacterWithSineWave(x, character) {
for (let p = 0; p < font_width; p++) {
let index = x_to_index(x + p);
let yOffset = y_offset[index];
offscreenCtx.drawImage(
characterBitmap[character],
p, 0, 1, font_height,
x + p, (offscreenCanvas.height / 2) + yOffset - baseline_offset,
1, font_height
);
}
}
function drawInstructions() {
offscreenCtx.font = "16px 'Press Start 2P'";
offscreenCtx.fillStyle = "white";
let amplitude = (offscreenCanvas.width - offscreenCtx.measureText(instructions).width) / 2;
let instructionX = (offscreenCanvas.width / 2) + Math.sin(time * 0.5) * amplitude;
offscreenCtx.fillText(instructions, instructionX - offscreenCtx.measureText(instructions).width / 2, offscreenCanvas.height - 30);
}
function drawCopperBars() {
let barHeight = 15;
let numBars = 20;
let barSpacing = 5;
let barFrequency = 0.2;
let barAmplitude = 40;
let centerY = offscreenCanvas.height / 2 - (numBars * (barHeight + barSpacing) / 2);
for (let i = 0; i < numBars; i++) {
let yOffset = Math.sin((time + i * 0.2) * barFrequency + time * copperSpeed) * barAmplitude;
let yPosition = centerY + i * (barHeight + barSpacing) + yOffset;
let hue = (time * 10 + i * 5) % 360;
let gradient = offscreenCtx.createLinearGradient(0, yPosition, 0, yPosition + barHeight);
gradient.addColorStop(0, `hsl(${hue}, 100%, 50%)`);
gradient.addColorStop(0.5, `hsl(${hue}, 100%, 20%)`);
gradient.addColorStop(1, `hsl(${hue}, 100%, 0%)`);
offscreenCtx.fillStyle = gradient;
offscreenCtx.fillRect(0, yPosition, offscreenCanvas.width, barHeight);
}
}
function drawActiveTriangle() {
if (flashOpacity > 0 && activeDirection !== Directions.NONE) {
const cx = displayCanvas.width / 2;
const cy = displayCanvas.height / 2;
displayCtx.fillStyle = `rgba(255, 255, 255, ${flashOpacity})`;
displayCtx.beginPath();
switch (activeDirection) {
case Directions.TOP:
displayCtx.moveTo(cx, cy);
displayCtx.lineTo(0, 0);
displayCtx.lineTo(displayCanvas.width, 0);
break;
case Directions.BOTTOM:
displayCtx.moveTo(cx, cy);
displayCtx.lineTo(0, displayCanvas.height);
displayCtx.lineTo(displayCanvas.width, displayCanvas.height);
break;
case Directions.LEFT:
displayCtx.moveTo(cx, cy);
displayCtx.lineTo(0, 0);
displayCtx.lineTo(0, displayCanvas.height);
break;
case Directions.RIGHT:
displayCtx.moveTo(cx, cy);
displayCtx.lineTo(displayCanvas.width, 0);
displayCtx.lineTo(displayCanvas.width, displayCanvas.height);
break;
default:
break;
}
displayCtx.closePath();
displayCtx.fill();
}
}
function handleInteraction(x, y) {
const cx = displayCanvas.width / 2;
const cy = displayCanvas.height / 2;
if (isPointInTriangle(x, y, cx, cy, 0, 0, displayCanvas.width, 0)) {
activeDirection = Directions.TOP;
copperSpeed += 0.1;
} else if (isPointInTriangle(x, y, cx, cy, 0, displayCanvas.height, displayCanvas.width, displayCanvas.height)) {
activeDirection = Directions.BOTTOM;
copperSpeed = Math.max(0.1, copperSpeed - 0.1);
} else if (isPointInTriangle(x, y, cx, cy, 0, 0, 0, displayCanvas.height)) {
activeDirection = Directions.LEFT;
scrollerSpeed = Math.max(10, scrollerSpeed + 10);
recalculateYOffset();
} else if (isPointInTriangle(x, y, cx, cy, displayCanvas.width, 0, displayCanvas.width, displayCanvas.height)) {
activeDirection = Directions.RIGHT;
scrollerSpeed = Math.max(10, scrollerSpeed - 10);
recalculateYOffset();
} else {
activeDirection = Directions.NONE;
}
flashOpacity = 0.5;
}
displayCanvas.addEventListener('click', function (event) {
const rect = displayCanvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
handleInteraction(x, y);
});
window.addEventListener('keydown', function (event) {
const cx = displayCanvas.width / 2;
const cy = displayCanvas.height / 2;
switch (event.key) {
case 'ArrowLeft':
handleInteraction(1, cy);
break;
case 'ArrowRight':
handleInteraction(displayCanvas.width - 1, cy);
break;
case 'ArrowUp':
handleInteraction(cx, 1);
break;
case 'ArrowDown':
handleInteraction(cx, displayCanvas.height - 1);
break;
default:
return;
}
});
// Use FontFaceObserver to ensure the font is fully loaded
const fontObserver = new FontFaceObserver('Press Start 2P');
fontObserver.load().then(function () {
restartDemo(); // Ensure the demo only starts after the font is loaded
render(); // Start the render loop
}).catch(function (error) {
console.error('Font failed to load:', error);
// Optionally handle fallback or retry logic here
});
});