diff --git a/draw.js b/draw.js index eba5b99..ed9fb60 100644 --- a/draw.js +++ b/draw.js @@ -1,6 +1,10 @@ class Drawing { - constructor(divId) { - this.div = document.getElementById(divId); + constructor(div) { + if (div instanceof HTMLElement) { + this.div = div; + } else { + this.div = document.getElementById(div); + } this.div.classList.add('drawing'); this.div.classList.add('container'); this.buttonsDiv = document.createElement('div'); @@ -54,7 +58,9 @@ class Drawing { } setTitle(title) { - this.titleDiv.innerHTML = title; + const h = document.createElement('h4'); + h.innerHTML = title; + this.titleDiv.appendChild(h); } setCaption(caption) { @@ -287,4 +293,69 @@ class Drawing { this.ctx.stroke(); }) } + + static fromText(node) { + const div = document.createElement('div'); + node.parentNode.parentNode.insertBefore(div, node.parentNode); + node.style.display = 'none'; + const d = new Drawing(div); + const lines = node.innerText.split('\n'); + for (let i = 0; i < lines.length; i++) { + const line = lines[i]; + if (line.startsWith("#")) continue; + const [cmd, ...args] = line.split(' '); + // console.log({cmd, args}); + switch (cmd) { + case 'title': { + d.setTitle(args.join(' ')); + } break; + case 'caption': { + d.setCaption(args.join(' ')); + } break; + case 'buttons': { + d.addButtons(); + } break; + case 'frame': { + const [x1, y1, x2, y2] = args.map(x => parseInt(x)); + d.setFrame([x1, y1], [x2, y2]); + } break; + case 'axes': { + const [x, y] = args.map(x => parseInt(x)); + d.polyline([0, y], [0, 0], [x, 0]); + } break; + case 'stroke': { + const style = args[0]; + const width = parseInt(args[1]); + d.setStroke(style, width); + } break; + case 'fill': { + const style = args[0]; + d.setFill(style); + } break; + case 'point': { + const [name, ...rest] = args; + let body = rest.join(' '); + while (lines[i + 1].startsWith(' ')) { + body += lines[i + 1]; + i += 1; + } + d.definePoint(name, () => { + return (function() { + return eval(body); + }).call(d); + }); + } break; + case 'circle': + case 'square': { + const p = args[0]; + const traceAge = args[1] ? parseInt(args[1]) : 0; + d[cmd](p, {trace: {age: traceAge}}); + } break; + case 'start': { + d.start(); + } break; + } + } + d.render(); + } } \ No newline at end of file diff --git a/reveal.html b/reveal.html index 10e997b..98aec9a 100644 --- a/reveal.html +++ b/reveal.html @@ -16,17 +16,31 @@
+