Files
500-Lines-or-Less/1 - Blockcode/resources/js/script-runner.js
T

48 lines
1.1 KiB
JavaScript
Raw Normal View History

2026-10-31 23:35:38 -05:00
const setupScriptRunner = () => {
canvasContext = canvas.getContext("2d");
2026-10-31 23:35:38 -05:00
Painter.canvas = canvas;
Painter.canvasContext = canvasContext;
}
const saveScript = () => {
let serializedNodes = [];
for (const elm of script.children) {
serializedNodes.push( elm.serialize() );
}
downloadScript( JSON.stringify(serializedNodes, null, 4) );
}
const loadScript = (file) => {
const reader = new FileReader();
reader.onload = (e) => {
try {
insertLoadedBlocks( JSON.parse(e.target.result) );
} catch (error) {
console.error('Failed to load JSON file: ', error);
}
};
reader.readAsText(file);
}
2026-10-31 23:35:38 -05:00
const runScript = () => {
resetCanvasState();
2026-10-31 23:35:38 -05:00
for (const elm of script.children) {
elm.action(Painter);
}
}
const downloadScript = (
content, fileName = "block-script.json", contentType = 'text/plain'
) => {
let link = document.createElement("A");
let file = new Blob([content], {type: contentType});
link.href = URL.createObjectURL(file);
link.download = fileName;
link.click();
delete link;
}