const setupScriptRunner = () => { canvasContext = canvas.getContext("2d"); 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); } const runScript = () => { resetCanvasState(); 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; }