-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.html
709 lines (652 loc) · 29.3 KB
/
index.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
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Forkphorus is a JavaScript compiler for Scratch 3, 2, and 1 projects.">
<link rel="canonical" href="https://forkphorus.github.io/" />
<title>forkphorus - phosphorus for Scratch 3</title>
<script>
// Apply local dark mode preference very early to avoid a flash of white
(function() {
try {
if (localStorage.getItem('userPrefersDark') === 'true') {
document.documentElement.classList.add('dark');
}
} catch (e) {
console.error('error loading theme choice', e);
}
}());
</script>
<link rel="stylesheet" href="phosphorus.css">
<link rel="stylesheet" href="studioview/studioview.css">
<link rel="stylesheet" href="index.css">
<meta name="google-site-verification" content="Re7zb-nm555twSGK216lVPDW-7v7ob1vQHYGQT3fBhE" /></head>
<body>
<div class="theme-select">
<a class="switch-theme">switch theme</a>
</div>
<div id="app">
<div class="area" id="title-area">
<h1 class="title">forkphorus</h1>
<p>forkphorus runs Scratch 3, 2, and 1 projects really fast by compiling them to JavaScript. Try it out by pasting a project ID or URL into the field below, <a class="upload-button"><input id="upload-file-input" type="file" autocomplete="off" accept=".sb,.sb2,.sb3">selecting a project file</a>, dropping a file on the page, or <a id="examples">exploring the examples studio</a>.</p>
</div>
<div id="examples-container" class="area">
<div id="examples-view"></div>
<div id="examples-footer"></div>
</div>
<div class="area" id="player-area"></div>
<div class="title">
<input class="url" value="https://scratch.mit.edu/projects/" spellcheck="false">
<a target="_blank" class="project-link" title="View project on Scratch" rel="noopener" href="https://scratch.mit.edu/"></a>
</div>
<div class="area" id="unshared-projects">
<h1>Unshared projects are no longer visible</h1>
<p>
Unshared projects are no longer visible due to Scratch API changes.
</p>
<p>
For more information and workarounds, visit: <a href="https://docs.turbowarp.org/unshared-projects" target="_blank" rel="noopener noreferrer">https://docs.turbowarp.org/unshared-projects</a>
</p>
</div>
<div class="area" id="project-area">
<section>
<h1 data-i18n="index.report">Report a problem</h1>
<p>forkphorus is still in development. <a id="bug-link" target="_blank" rel="noopener" href="https://github.com/forkphorus/forkphorus/issues/new">Click here</a> to report a problem with this project. You can also comment problems <a href="https://scratch.mit.edu/projects/366001364/" target="_blank" rel="noopener">here</a> if you don't have a GitHub account.</p>
</section>
<section class=package>
<h1 data-i18n="index.embed">Embed this project</h1>
<p data-i18n="index.embed.description">Include the forkphorus player in your web site.</p>
<p class="embed-options">
<input readonly id="embed-code">
<input type="checkbox" id="embed-auto-start" checked>
<label for="embed-auto-start" data-i18n="index.embed.autostart">Start automatically</label>
<input type="checkbox" id="embed-light-content">
<label for="embed-light-content" data-i18n="index.embed.lightControls">Light controls</label>
<input type="checkbox" id="embed-hide-controls">
<label for="embed-hide-controls" data-i18n="index.embed.hideUI">Hide UI</label>
<span>
<!-- we re-use the i18n for the packager for this -->
<input type="radio" name="embed-resolution" id="embed-480" checked>
<label for="embed-480" data-i18n="index.package.480">480x360</label>
<input type="radio" name="embed-resolution" id="embed-other">
<label for="embed-other" data-i18n="index.package.custom">Custom:</label>
<input id="embed-other-width" type="number" value="960" disabled min="1">
<span class="package-other-divider" data-i18n="index.package.divider">×</span>
<input id="embed-other-height" type="number" value="720" disabled min="1">
</span>
</p>
</section>
<section class=package>
<h1 data-i18n="index.package">Package this project</h1>
<p>Get a link to a web page that automatically runs your project. To package to HTML, EXE, APK, or other formats, use the <a href="packager/" id="html-packager">HTML packager</a>.</p>
<p class="package-options">
<a href="#" target="_blank" id="package-link" data-i18n="index.package.button">Package</a>
<input type="checkbox" id="package-turbo">
<label for="package-turbo" data-i18n="index.package.turbo">Turbo mode</label>
<input type="checkbox" id="package-controls">
<label for="package-controls" data-i18n="index.package.controls">Controls</label>
<span>
<input type="radio" name="package-resolution" id="package-full-screen" checked>
<label for="package-full-screen" data-i18n="index.package.fullscreen">Full screen</label>
<input type="radio" name="package-resolution" id="package-480">
<label for="package-480" data-i18n="index.package.480">480x360</label>
<input type="radio" name="package-resolution" id="package-other">
<label for="package-other" data-i18n="index.package.custom">Custom:</label>
<input id="package-other-width" type="number" value="960" disabled min="1">
<span class="package-other-divider" data-i18n="index.package.divider">×</span>
<input id="package-other-height" type="number" value="720" disabled min="1">
</span>
</p>
</section>
<h1 data-i18n="index.settings">Settings</h1>
<table id="settings">
<tr>
<td class="name" title="Forkphorus will **attempt** to run at this framerate."><label for="settings-fps">Framerate (FPS)</label></td>
<td class="value"><input id="settings-fps" value="0" placeholder="30" type="number" step="any" min="0" required autocomplete="off"></td>
</tr>
<tr>
<td class="name" title="The value of the "username" block. Changing this may cause issues in some projects."><label for="settings-username">Username</label></td>
<td class="value"><input id="settings-username" type="text" placeholder="(nothing)" autocomplete="off"></td>
</tr>
<tr>
<td class="name" title="Controls whether the renderer tries to make images look smoother."><label for="settings-image-smoothing">Image Smoothing</label></td>
<td class="value"><input id="settings-image-smoothing" type="checkbox" autocomplete="off"></td>
</tr>
<tr>
<td class="name" title="Prevents sprites from moving offscreen."><label for="settings-sprite-fencing">Sprite Fencing</label></td>
<td class="value"><input id="settings-sprite-fencing" type="checkbox" autocomplete="off"></td>
</tr>
<tr>
<td class="name" title="Removes some limits."><label for="settings-remove-limits">Remove some limits</label></td>
<td class="value"><input id="settings-remove-limits" type="checkbox" autocomplete="off"></td>
</tr>
</table>
<p>Changes are applied automatically.</p>
<h1 data-i18n="index.advanced">Advanced Settings</h1>
<p>Help test unfinished features. These require a refresh to apply.</b> <a href="https://github.com/forkphorus/forkphorus/wiki/Advanced-Features" target="_blank">Please read this first</a>.</p>
<div class="advanced">
<label><input id="advanced-webgl" class="advanced-feature" type="checkbox"> WebGL Renderer</label>
</div>
</div>
<section>
<h1>Try out TurboWarp</h1>
<p style="margin-bottom: 0;"><a id="turbowarp" href="https://turbowarp.org/" target="_black" rel="noopener">TurboWarp</a> typically has better performance and compatibility than forkphorus and has an editor. Give it a try, you might like it.</p>
<h1 data-i18n="index.credits">Credits</h1>
<p>forkphorus is maintained by <a href="https://scratch.mit.edu/users/GarboMuffin/">GarboMuffin</a> and <a href="https://github.com/forkphorus/forkphorus/graphs/contributors">other contributors</a>. It is a fork of <a href="https://github.com/nathan">Nathan</a>'s <a href="https://phosphorus.github.io/">phosphorus</a>. Its CPS-style compilation was inspired by <a href="https://github.com/RHY3756547">Rhys</a>'s <a href="https://code.google.com/p/sb2-js/">sb2.js</a>. The <a href="https://stuk.github.io/jszip/">JSZip</a> library is used to read <code>.sb2</code> and <code>.sb3</code> files. The <a href="https://github.com/canvg/canvg">canvg</a> library is used to render SVGs for Scratch 2 projects. The <a href="https://github.com/LLK/scratch-sb1-converter">scratch-sb1-converter</a> library is used to support Scratch 1 projects.</p>
<h1 data-i18n="index.code">Code</h1>
<p>The source code for forkphorus is available <a href=https://github.com/forkphorus/forkphorus>on GitHub</a>.</p>
<footer>
<a data-i18n="index.privacy" href="privacy">Privacy Policy</a>
- Version 41b2748 (2025-01-09)</footer>
</section>
</div>
<script src="lib/scratch-sb1-converter.js"></script>
<script src="lib/jszip.min.js"></script>
<script src="lib/fontfaceobserver.standalone.js"></script>
<script src="lib/canvg.min.js"></script>
<script src="lib/purify.min.js"></script>
<script src="phosphorus.dist.js"></script>
<script src="common.js"></script>
<script src="studioview/studioview.js"></script>
<script>
P.i18n.addTranslations('en', {
'studioview.authorAttribution': 'by $author',
'studioview.projectHoverText': '$title by $author',
'index.document.title': 'forkphorus - phosphorus for Scratch 3',
'index.report': 'Report a problem',
'index.embed': 'Embed this project',
'index.package': 'Package this project',
'index.settings': 'Settings',
'index.credits': 'Credits',
'index.code': 'Code',
'index.studio.view': 'View studio on Scratch.',
'index.package.button': 'Package',
'index.package.turbo': 'Turbo mode',
'index.package.controls': 'Controls',
'index.package.fullscreen': 'Full screen',
'index.package.480': '480\u00D7360',
'index.package.custom': 'Other:',
'index.package.divider': '\u00D7',
'index.embed.description': 'Include the forkphorus player in your web site.',
'index.embed.autostart': 'Start automatically',
'index.embed.lightControls': 'Light controls',
'index.embed.hideUI': 'Hide UI',
'index.advanced': 'Advanced Settings',
'index.advanced.confirmation': 'This will refresh this page, resetting the project. Continue?',
'index.privacy': 'Privacy Policy',
});
</script>
<script>
if (!window.P || !window.P.io) {
document.getElementById('title-area').innerHTML += '<p><b>This browser is not supported.</b> Please upgrade to Mozilla Firefox, Google Chrome, or Microsoft Edge.</p>'
}
</script>
<script>
(function() {
'use strict';
/** Utilities **/
// document.querySelector but it throws if your selector failed
function querySelector(selector) {
var el = document.querySelector(selector);
if (!el) {
throw new Error('Selector returned nothing: ' + selector);
}
return el;
}
/** i18n */
document.title = P.i18n.translate('index.document.title');
StudioView.AUTHOR_ATTRIBUTION = P.i18n.translate('studioview.authorAttribution');
StudioView.PROJECT_HOVER_TEXT = P.i18n.translate('studioview.projectHoverText');
P.i18n.translateElement(document.body);
/** Constants **/
var PROJECT_PREFIX = 'https://scratch.mit.edu/projects/';
var STUDIO = '15926401';
/** Core Player Logic **/
var titleArea = querySelector('#title-area');
var playerArea = querySelector('#player-area');
var projectArea = querySelector('#project-area');
var unsharedProjectArea = querySelector('#unshared-projects');
var player = new P.player.Player();
player.addControls();
new P.player.ProgressBar(player, {
position: querySelector('.url').parentNode,
});
var errorHandler = new P.player.ErrorHandler(player);
player.onerror.subscribe(function(err) {
if (err instanceof P.player.PlayerError) {
// Handled by the player's ErrorHandler.
return;
}
err = '' + err;
var el = document.createElement('div');
el.innerHTML = `Alternatively, <a href="https://scratch.mit.edu/projects/366001364/" target="_blank">post a Scratch comment</a> and include: "<span style="font-family: monospace; user-select: all">${err.replace(/<>/g, '')} (${player.getProjectMeta().getId()})</span>"`;
errorHandler.errorEl.appendChild(el);
if (errorHandler.generatedErrorLink) {
bugLink.href = errorHandler.generatedErrorLink;
}
});
playerArea.appendChild(player.root);
// expose player on the global object
window.player = player;
console.log('forkphorus is open source https://github.com/forkphorus/forkphorus');
console.log('The project player is exposed as `player`');
console.log('The project is exposed as `player.stage`');
console.log('Sprites are exposed in `player.stage.children`');
var initialId = Common.projectId || '';
player.setOptions(Common.playerOptions);
player.setOptions({
autoplayPolicy: 'never'
});
playerArea.style.height = projectArea.style.height = 'auto';
var titleAreaHeight = titleArea.offsetHeight;
var playerAreaHeight = playerArea.offsetHeight;
var projectAreaHeight = projectArea.offsetHeight;
var unsharedProjectAreaHeight = unsharedProjectArea.offsetHeight;
var studioViewAreaHeight = 330;
playerArea.style.height = projectArea.style.height = 0;
var urlInput = querySelector('.url');
urlInput.value = PROJECT_PREFIX + initialId;
var projectLink = querySelector('.project-link');
var bugLink = querySelector('#bug-link');
var htmlPackagerLink = querySelector('#html-packager');
var timeout;
urlInput.addEventListener('input', function() {
var url = urlInput.value;
var pid = player.hasProjectMeta() ? player.getProjectMeta().getId() : -1;
var id = url.match(/\d+/g) || [''];
id = id[0].replace(/^0+/, '');
urlInput.value = url = PROJECT_PREFIX + id;
clearTimeout(timeout);
if (pid !== id) {
timeout = setTimeout(function() {
location.hash = '#' + id;
}, 300);
}
});
urlInput.addEventListener('focus', function() {
setTimeout(function() {
if (/\d/.test(urlInput.value)) {
urlInput.select();
}
});
});
var ignoreNextHashChange = false;
window.addEventListener('hashchange', function() {
if (ignoreNextHashChange) {
ignoreNextHashChange = false;
return;
}
var id = location.hash.substr(1);
if (/^\d+$/.test(id)) {
urlInput.value = PROJECT_PREFIX + id;
loadProjectId(id);
} else {
hidePlayerArea();
}
});
// Change location.hash w/o firing the hashchange event (or at least make it cancel)
// Include the leading # in the provided hash
function silentChangeHash(hash) {
if (location.hash === hash) return;
ignoreNextHashChange = true;
location.hash = hash;
}
function hidePlayerArea() {
titleArea.style.height = titleAreaHeight + 'px';
playerArea.style.height = '0px';
projectArea.style.height = '0px';
urlInput.focus();
player.cleanup();
if (studioView && studioView.visible) {
examplesContainer.style.height = studioViewAreaHeight + 'px';
examplesViewContainer.appendChild(studioView.root);
studioView.projectList.scrollTop = studioView.scrollTop;
}
unsharedProjectArea.style.height = unsharedProjectAreaHeight + 'px';
resetTitle();
}
function showPlayerArea() {
titleArea.style.height = '0px';
playerArea.style.height = playerAreaHeight + 'px';
projectArea.style.height = projectAreaHeight + 'px';
hideStudioView();
examplesContainer.style.height = '0px';
unsharedProjectArea.style.height = '0px';
}
function loadProjectId(id) {
showPlayerArea();
player.loadProjectById(id);
updateLinks();
setTitleToProjectTitle();
}
function loadProjectFile(file) {
var extension = file.name.split('.').pop();
if (!['sb', 'sb2', 'sb3'].includes(extension)) {
return;
}
silentChangeHash('#' + file.name);
showPlayerArea();
player.loadProjectFromFile(file);
updateLinks();
setTitleToProjectTitle();
}
function updateLinks() {
bugLink.href = errorHandler.createBugReportLink();
if (player.getProjectMeta().isFromScratch()) {
projectLink.href = 'https://scratch.mit.edu/projects/' + player.getProjectMeta().getId();
htmlPackagerLink.href = 'packager/#' + player.getProjectMeta().getId();
} else {
projectLink.href = 'https://scratch.mit.edu/';
htmlPackagerLink.href = 'packager';
}
updatePackageLink();
updateEmbedCode();
if (player.getProjectMeta().isFromScratch()) {
document.getElementById('turbowarp').href = 'https://turbowarp.org/' + player.getProjectMeta().getId();
} else {
document.getElementById('turbowarp').href = 'https://turbowarp.org/';
}
}
var initialTitle = document.title;
function resetTitle() {
document.title = initialTitle;
}
function setTitleToProjectTitle() {
var stageId = player.stageId;
player.getProjectMeta().load()
.then(function(meta) {
if (player.stageId === stageId) {
if (meta.isUnshared()) {
unsharedProjectArea.style.height = unsharedProjectAreaHeight + 'px';
}
var title = meta.getTitle();
if (title) {
player.projectTitle = title;
document.title = title + ' - forkphorus';
updateLinks();
} else {
resetTitle();
}
}
});
}
/** Packaging **/
var packageLink = querySelector('#package-link');
var packageTurbo = querySelector('#package-turbo');
var packageFullScreen = querySelector('#package-full-screen');
var package480x360 = querySelector('#package-480');
var packageOther = querySelector('#package-other');
var packageOtherWidth = querySelector('#package-other-width');
var packageOtherHeight = querySelector('#package-other-height');
var packageControls = querySelector('#package-controls');
// Bind two inputs together so that changes to one reflect a change in the other.
function bindInputs(first, other, multiplier) {
var fun1 = function() { other.value = Math.round(first.value * multiplier); };
var fun2 = function() { first.value = Math.round(other.value * (1 / multiplier)); };
first.addEventListener('input', fun1);
first.addEventListener('change', fun1);
other.addEventListener('input', fun2);
other.addEventListener('change', fun2);
}
function updatePackageLink() {
packageOtherWidth.disabled = packageOtherHeight.disabled = !packageOther.checked;
if (player.getProjectMeta().isFromScratch()) {
var url = location.origin + location.pathname + 'app.html?id=' + player.getProjectMeta().getId();
if (packageTurbo.checked) url += '&turbo';
if (packageFullScreen.checked) {
// fullscreen is the default
} else if (package480x360.checked) {
url += '&w=480';
} else {
url += '&w=' + packageOtherWidth.value;
}
if (packageControls.checked) {
url += '&controls';
}
packageLink.href = url;
} else {
packageLink.href = 'about:blank';
}
}
bindInputs(packageOtherHeight, packageOtherWidth, 4 / 3);
packageTurbo.addEventListener('change', updatePackageLink);
packageFullScreen.addEventListener('change', updatePackageLink);
package480x360.addEventListener('change', updatePackageLink);
packageOther.addEventListener('change', updatePackageLink);
packageOtherHeight.addEventListener('change', updatePackageLink);
packageOtherWidth.addEventListener('change', updatePackageLink);
packageControls.addEventListener('change', updatePackageLink);
/** Embedding **/
var embedCode = querySelector('#embed-code');
var embedAutoStart = querySelector('#embed-auto-start');
var embedLightContent = querySelector('#embed-light-content');
var embedHideControls = querySelector('#embed-hide-controls');
var embed480 = querySelector('#embed-480');
var embedOther = querySelector('#embed-other');
var embedOtherWidth = querySelector('#embed-other-width');
var embedOtherHeight = querySelector('#embed-other-height');
function updateEmbedCode(e) {
// Hide UI replaces other options.
embedAutoStart.disabled = embedHideControls.checked;
embedLightContent.disabled = embedHideControls.checked;
if (embedHideControls.checked) {
embedAutoStart.checked = true;
embedLightContent.checked = false;
}
embedOtherWidth.disabled = embedOtherHeight.disabled = !embedOther.checked;
if (player.getProjectMeta().isFromScratch()) {
var source = location.protocol + '//' + location.host + location.pathname + 'embed.html?id=' + player.getProjectMeta().getId();
if (embedHideControls.checked) {
source += '&ui=false';
} else {
source += '&auto-start=' + embedAutoStart.checked;
source += '&light-content=' + embedLightContent.checked;
}
var width = 480;
var height = 360;
if (embedOther.checked) {
width = +embedOtherWidth.value;
height = +embedOtherHeight.value;
}
if (!embedHideControls.checked) {
// Space for controls and player border
width += 2;
height += 33;
}
embedCode.value = '<iframe src="' + source + '" width="' + width + '" height="' + height + '" allowfullscreen="true" allowtransparency="true" style="border:none;"></iframe>';
} else {
embedCode.value = '';
}
if (e) {
selectEmbedCode();
}
}
function selectEmbedCode() {
embedCode.select();
}
bindInputs(embedOtherHeight, embedOtherWidth, 4 / 3);
embedCode.addEventListener('focus', selectEmbedCode);
embedCode.addEventListener('click', selectEmbedCode);
embedHideControls.addEventListener('change', updateEmbedCode);
embedAutoStart.addEventListener('change', updateEmbedCode);
embedLightContent.addEventListener('change', updateEmbedCode);
embed480.addEventListener('change', updateEmbedCode);
embedOther.addEventListener('change', updateEmbedCode);
embedOtherHeight.addEventListener('change', updateEmbedCode);
embedOtherWidth.addEventListener('change', updateEmbedCode);
/** Local File Loading **/
var uploadFileInput = querySelector('#upload-file-input');
function cancelDragEvent(e) {
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
}
document.body.addEventListener('dragover', cancelDragEvent);
document.body.addEventListener('dragenter', cancelDragEvent);
document.body.addEventListener('drop', function(e) {
e.preventDefault();
var file = e.dataTransfer.files[0];
if (file) {
loadProjectFile(file);
}
});
uploadFileInput.addEventListener('change', function(e) {
var file = e.target.files[0];
if (file) {
loadProjectFile(file);
}
});
/** Studio selector **/
var examples = querySelector('#examples');
var examplesContainer = querySelector('#examples-container');
var examplesViewContainer = querySelector('#examples-view');
var examplesFooter = querySelector('#examples-footer');
examplesContainer.style.height = '0px';
var studioView = null;
function hideStudioView() {
examplesContainer.style.height = '0px';
if (studioView && studioView.visible && studioView.root.parentNode) {
studioView.scrollTop = studioView.projectList.scrollTop;
examplesViewContainer.removeChild(studioView.root);
}
}
function initStudioView() {
studioView = new StudioView(STUDIO);
studioView.visible = false;
studioView.onselect = function(id, el) {
location.hash = '#' + id;
};
studioView.loadNextPage();
studioView.setTheme(document.documentElement.classList.contains('dark') ? 'dark' : 'light');
studioView.scrollTop = 0;
var link = document.createElement('a');
link.innerText = P.i18n.translate('index.studio.view');
link.target = '_blank';
link.rel = 'noopener';
link.href = studioView.getURL();
examplesFooter.appendChild(link);
}
examples.addEventListener('click', function(e) {
if (!studioView) {
initStudioView();
}
if (studioView.visible) {
hideStudioView();
} else {
examplesViewContainer.appendChild(studioView.root);
examplesContainer.style.height = studioViewAreaHeight + 'px';
}
studioView.visible = !studioView.visible;
});
/** Dark Mode **/
function getLocalDarkMode() {
try {
const value = localStorage.getItem('userPrefersDark');
if (!value) {
// value is undefined or null
return getDefaultDarkMode();
}
// value is the string 'true' or the string 'false'
return value === 'true';
} catch (e) {
console.warn('error getting local color choice', e);
return false;
}
}
function getDefaultDarkMode() {
return window.matchMedia('(prefers-color-scheme: dark)').matches;
}
function setDarkMode(dark) {
if (dark) {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
}
player.setOptions({ theme: dark ? 'dark' : 'light' });
if (studioView) {
studioView.setTheme(dark ? 'dark' : 'light');
}
try {
return localStorage.setItem('userPrefersDark', dark);
} catch (e) {
console.error('error saving color choice', e);
}
}
function toggleDarkMode(dark) {
setDarkMode(!document.documentElement.classList.contains('dark'));
}
querySelector('.theme-select a').addEventListener('click', function() {
toggleDarkMode();
});
setDarkMode(getLocalDarkMode());
/** Settings Menu **/
var settingFps = querySelector('#settings-fps');
var settingUsername = querySelector('#settings-username');
var settingImageSmoothing = querySelector('#settings-image-smoothing');
var settingSpriteFencing = querySelector('#settings-sprite-fencing');
var settingRemoveLimits = querySelector('#settings-remove-limits');
function syncOptionValues() {
if (+settingFps.value !== player.options.fps) {
settingFps.value = player.options.fps;
}
settingUsername.value = player.options.username;
settingImageSmoothing.checked = player.options.imageSmoothing;
settingSpriteFencing.checked = player.options.spriteFencing;
settingRemoveLimits.checked = player.options.removeLimits;
}
settingFps.addEventListener('input', (e) => {
if (settingFps.checkValidity()) {
player.setOptions({ fps: +settingFps.value });
}
});
settingUsername.addEventListener('input', (e) => {
player.setOptions({ username: settingUsername.value });
});
settingImageSmoothing.addEventListener('change', (e) => {
player.setOptions({ imageSmoothing: settingImageSmoothing.checked });
});
settingSpriteFencing.addEventListener('change', (e) => {
player.setOptions({ spriteFencing: settingSpriteFencing.checked });
});
settingRemoveLimits.addEventListener('change', (e) => {
player.setOptions({ removeLimits: settingRemoveLimits.checked });
});
syncOptionValues();
player.onoptionschange.subscribe(syncOptionValues);
/** Advanced Settings Menu **/
var advancedWebGL = querySelector('#advanced-webgl');
advancedWebGL.checked = P.config.useWebGL;
advancedWebGL.addEventListener('click', function(e) {
setSearchParam('webgl', e.target.checked, e);
});
function setSearchParam(key, value, event) {
event.preventDefault();
if (!confirm(P.i18n.translate('index.advanced.confirmation'))) {
return;
}
// if value === false, remove the key
// if value === true, add a key with no value
// else, add a key with the given value
if (value === false) {
var searchParams = new URLSearchParams(location.search);
searchParams.delete(key);
location.search = searchParams.toString();
} else {
var query = value === true ? key : key + '=' + value;
if (location.search.length > 1) {
location.search += '&' + query;
} else {
location.search = '?' + query;
}
}
}
if (initialId) {
loadProjectId(initialId);
} else {
urlInput.focus();
urlInput.setSelectionRange(urlInput.value.length, urlInput.value.length);
}
}());
</script>
</body>
</html>