-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreditsPlayer.html
311 lines (208 loc) · 6.9 KB
/
CreditsPlayer.html
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
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Sover the Credits Player</title>
<meta charset="UTF-8">
<style>
html, body { height: 100%; margin: 0; }
body {
overflow: hidden;
background-color: #111111;
color: white;
}
</style>
</head>
<body>
<div id="setupDiv">
credits .txt file: <input id="textImportId" type="file" accept="text/*"></input><br>
images: <input id="imageImportId" type="file" accept="image/*" multiple="multiple"></input> <label id="imageLoadingId"></label><br>
<button onclick="startCredits()">Start Credits</button><br><br>
<br><br>
Usage:<br>
Write the credits in a text file.<br><br>
[this would appear bigger]<br>
[[[[this would be very big]]]]<br><br>
this would be replaced by the first loaded picture (alphabetically):<br>
#1<br><br>
this would be the second picture:<br>
#2<br><br>
(make sure to only have the # and the number in that line, it shouldn't have anything else in it)<br><br><br>
a line containing the word "pride" will be rainbow-y
</div>
<div id="canvasDiv" style="display: none;">
<canvas id="canvasId"></canvas>
</div>
<script> //this could use Lu's 2d automata thingy for cool colors in the back
var canvas = document.getElementById("canvasId");
var ctx = canvas.getContext("2d");
//canvas.oncontextmenu = function(){return false;}
canvas.height = window.innerHeight; //969
canvas.width = window.innerWidth; //1920
let fps = 60;
let fontSize = 32;
let textGap = 50;
let creditsWidthRatio = 0.55;
let imageHeight = 600;
let creditsText = "";
let texts = [];
let images = [];
let camera = {y: 100};
let time = 0;
</script>
<script> //Import Credits File
window.onload = function(){
let textInput = document.getElementById('textImportId');
textInput.addEventListener('change', handleTextFiles, false);
let imageInput = document.getElementById('imageImportId');
imageInput.addEventListener('change', handleImageFiles, false);
}
function handleTextFiles(e){ //https://stackoverflow.com/a/6776066/12777947
let f = e.target.files[0];
let reader = new FileReader();
reader.onload = function(event)
{
creditsText = event.target.result;
};
reader.readAsText(f); //what's this? ah, whatever...
}
function handleImageFiles(e){ //https://stackoverflow.com/a/6776066/12777947
images = [];
for (let i = 0; i < e.target.files.length; i++){
let url = URL.createObjectURL(e.target.files[i]);
let img = new Image();
img.src = url;
img.onload = function(){
let creditsWidth = canvas.width * creditsWidthRatio;
let pos = {
w: img.width,
h: img.height
}
if (pos.w > creditsWidth){
let ratio = creditsWidth / pos.w;
pos.w *= ratio;
pos.h *= ratio;
}
images[i] = {image: img, w: pos.w, h: pos.h};//w: img.width * (imageHeight / img.height), h: img.height};
URL.revokeObjectURL(img.src); //https://stackoverflow.com/a/6776055/12777947
document.getElementById("imageLoadingId").innerHTML = images.length + "/" + e.target.files.length;
}
}
}
</script>
<script> //Generate Texts Array
function generateTextsArray(){
texts = [];
let lines = creditsText.split('\n');
let y = 0;
for (let line of lines){
line = line.replaceAll('\r', '');
let isImage = false;
let imageNum = 0;
if (line[0] == "#"){
let num = Number(line.substring(1));
if (!isNaN(num)){
isImage = true;
imageNum = num - 1;
}
}
if (isImage){
texts.push({text: line, y: y, imageNum: imageNum});
let currentImageHeight = imageHeight;
if (images[imageNum] != undefined){
currentImageHeight = images[imageNum].h;
}
for (let i = textGap; i < currentImageHeight; i += textGap){
y += textGap;
}
} else{
texts.push({text: line, y: y});
}
y += textGap;
}
}
</script>
<script> //Start Credits
function startCredits(){
time = 0;
generateTextsArray();
document.getElementById("setupDiv").style.display = "none";
document.getElementById("canvasDiv").style.display = "inline-block";
camera.y = (canvas.height * 1.1);
draw();
}
</script>
<script> //Draw
function draw(){
canvas.width |= 0;
ctx.fillStyle = "#FFFFFF";
let creditsWidth = canvas.width * creditsWidthRatio;
for (let i in texts){
let currentY = texts[i].y + Math.round(camera.y);
if (texts[i].imageNum == undefined){
if (currentY > 0 && currentY - fontSize < canvas.height){
let currentText = texts[i].text;
let currentFontSize = fontSize;
while (currentText[0] == "[" && currentText[currentText.length - 1] == "]"){
currentText = currentText.substring(1, currentText.length - 1);
currentFontSize *= 1.35;
}
let length = currentText.length;
let width = (length * fontSize)/1.825;
if (width > creditsWidth){
currentFontSize = creditsWidth * 1.825 / length; //this was constructed with maths, am proud of it
}
let leftMargain = (creditsWidth - ((currentFontSize/1.825) * currentText.length)) / 2 + 10;
ctx.font = currentFontSize + "px Consolas";
for (let j = 0; j < currentText.length; j++){
if (currentText.toLowerCase().includes("pride")){
ctx.fillStyle = "hsl(" + ((j / currentText.length) * 255) + ",100%, 50%)";
ctx.fillText(currentText[j], j * (currentFontSize/1.825) + leftMargain, currentY);
} else{
if ((time - i - (j*2)) % 1200 > 900){
ctx.fillStyle = "#21b1ffB4";
} else if ((time - i - (j*2)) % 1200 > 600){
ctx.fillStyle = "#ffd800B4";
} else if ((time - i - (j*2)) % 1200 > 300){
ctx.fillStyle = "#ff218cB4";
} else{
ctx.fillStyle = "#000000B4";
}
let shiftAmount = Math.round(currentFontSize/20);
ctx.fillText(currentText[j], j * (currentFontSize/1.825) + leftMargain + shiftAmount, currentY + shiftAmount);
ctx.fillStyle = "#FFFFFF";
ctx.fillText(currentText[j], j * (currentFontSize/1.825) + leftMargain, currentY);
}
}
}
} else{
let image = images[texts[i].imageNum];
if (image != undefined){
let leftMargain = (creditsWidth - image.w) / 2 + 10;
ctx.drawImage(image.image, leftMargain, texts[i].y + Math.round(camera.y), image.w, image.h);
}
}
}
}
</script>
<script> //Next Frame
function nextFrame(){
time++;
camera.y -= 0.85 * 2;
draw();
setTimeout(nextFrame,1000/fps);
}
</script>
<script> //Listeners
//Resize
function onResize(){
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
draw();
}
window.addEventListener("resize",onResize);
</script>
<script> //Calls
nextFrame();
</script>
</body>
</html>