forked from ccrma/webchuck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
60 lines (51 loc) · 1.49 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
var code;
fetch("blit2.ck").then((response) => response.text()).then((text) => {
code = text;
});
var Chuck;
import('https://cdn.jsdelivr.net/npm/webchuck/+esm').then(async (module) => {
Chuck = module.Chuck; // Chuck class
document.getElementById("try").disabled = false;
});
var theChuck;
// Button to run ChucK code
async function runChuckCode() {
// Create ChucK object if it doesn't exist
await Chuck;
if (!theChuck) {
theChuck = await Chuck.init([]);
}
theChuck.runCode(code); // Run ChucK code
};
const delay = 10; // Delay between each character typing (in milliseconds)
var typeCallback;
function typeWriterAnimation(element, text, delay) {
let charIndex = 0;
function type() {
if (charIndex < text.length) {
element.textContent += text.charAt(charIndex);
charIndex++;
typeCallback = setTimeout(type, delay);
}
}
type();
}
function stopTypeWriterAnimation() {
clearTimeout(typeCallback);
}
var play = false;
document.getElementById("try").addEventListener("click", function() {
const animationContainer = document.getElementById("animationContainer");
if (play) {
animationContainer.textContent = "";
theChuck.removeLastCode();
document.getElementById("try").textContent = "Try WebChucK!";
stopTypeWriterAnimation();
} else {
typeWriterAnimation(animationContainer, code, delay);
runChuckCode();
document.getElementById("try").textContent = "Stop";
}
play = !play;
});
hljs.highlightAll();