2026-10-31 23:35:38 -05:00
|
|
|
const setupScriptRunner = () => {
|
2026-11-01 00:25:24 -05:00
|
|
|
canvasContext = canvas.getContext("2d");
|
2026-10-31 23:35:38 -05:00
|
|
|
Painter.canvas = canvas;
|
|
|
|
|
Painter.canvasContext = canvasContext;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-01 00:41:38 -05:00
|
|
|
const saveScript = () => {
|
2026-11-02 00:30:26 -06:00
|
|
|
let serializedNodes = [];
|
|
|
|
|
for (const elm of script.children) {
|
|
|
|
|
serializedNodes.push( elm.serialize() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
downloadScript( JSON.stringify(serializedNodes, null, 4) );
|
2026-11-01 00:41:38 -05:00
|
|
|
}
|
|
|
|
|
|
2026-11-02 00:30:26 -06:00
|
|
|
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-11-01 00:41:38 -05:00
|
|
|
}
|
|
|
|
|
|
2026-10-31 23:35:38 -05:00
|
|
|
const runScript = () => {
|
2026-11-01 00:25:24 -05:00
|
|
|
resetCanvasState();
|
2026-10-31 23:35:38 -05:00
|
|
|
for (const elm of script.children) {
|
|
|
|
|
elm.action(Painter);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-02 00:30:26 -06:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|