UNPKG

scrawl-canvas

Version:
1,102 lines (702 loc) 92 kB
<!DOCTYPE html> <html> <head> <title>DOM management</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"> <link rel="stylesheet" media="all" href="../../docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <ul id="jump_to"> <li> <a class="large" href="javascript:void(0);">Jump To &hellip;</a> <a class="small" href="javascript:void(0);">+</a> <div id="jump_wrapper"> <div id="jump_page_wrapper"> <div id="jump_page"> <a class="source" href="animationloop.html"> ./source/core/animationloop.js </a> <a class="source" href="document.html"> ./source/core/document.js </a> <a class="source" href="events.html"> ./source/core/events.js </a> <a class="source" href="init.html"> ./source/core/init.js </a> <a class="source" href="library.html"> ./source/core/library.js </a> <a class="source" href="random-seed.html"> ./source/core/random-seed.js </a> <a class="source" href="snippets.html"> ./source/core/snippets.js </a> <a class="source" href="userInteraction.html"> ./source/core/userInteraction.js </a> <a class="source" href="utilities.html"> ./source/core/utilities.js </a> <a class="source" href="../factory/action.html"> ./source/factory/action.js </a> <a class="source" href="../factory/anchor.html"> ./source/factory/anchor.js </a> <a class="source" href="../factory/animation.html"> ./source/factory/animation.js </a> <a class="source" href="../factory/bezier.html"> ./source/factory/bezier.js </a> <a class="source" href="../factory/block.html"> ./source/factory/block.js </a> <a class="source" href="../factory/canvas.html"> ./source/factory/canvas.js </a> <a class="source" href="../factory/cell.html"> ./source/factory/cell.js </a> <a class="source" href="../factory/cog.html"> ./source/factory/cog.js </a> <a class="source" href="../factory/color.html"> ./source/factory/color.js </a> <a class="source" href="../factory/conicGradient.html"> ./source/factory/conicGradient.js </a> <a class="source" href="../factory/coordinate.html"> ./source/factory/coordinate.js </a> <a class="source" href="../factory/crescent.html"> ./source/factory/crescent.js </a> <a class="source" href="../factory/element.html"> ./source/factory/element.js </a> <a class="source" href="../factory/emitter.html"> ./source/factory/emitter.js </a> <a class="source" href="../factory/filter.html"> ./source/factory/filter.js </a> <a class="source" href="../factory/filterEngine-bluenoiseData.html"> ./source/factory/filterEngine-bluenoiseData.js </a> <a class="source" href="../factory/filterEngine.html"> ./source/factory/filterEngine.js </a> <a class="source" href="../factory/fontAttributes.html"> ./source/factory/fontAttributes.js </a> <a class="source" href="../factory/gradient.html"> ./source/factory/gradient.js </a> <a class="source" href="../factory/grid.html"> ./source/factory/grid.js </a> <a class="source" href="../factory/group.html"> ./source/factory/group.js </a> <a class="source" href="../factory/imageAsset.html"> ./source/factory/imageAsset.js </a> <a class="source" href="../factory/line.html"> ./source/factory/line.js </a> <a class="source" href="../factory/lineSpiral.html"> ./source/factory/lineSpiral.js </a> <a class="source" href="../factory/loom.html"> ./source/factory/loom.js </a> <a class="source" href="../factory/mesh.html"> ./source/factory/mesh.js </a> <a class="source" href="../factory/net.html"> ./source/factory/net.js </a> <a class="source" href="../factory/noiseAsset.html"> ./source/factory/noiseAsset.js </a> <a class="source" href="../factory/oval.html"> ./source/factory/oval.js </a> <a class="source" href="../factory/palette.html"> ./source/factory/palette.js </a> <a class="source" href="../factory/particle.html"> ./source/factory/particle.js </a> <a class="source" href="../factory/particleForce.html"> ./source/factory/particleForce.js </a> <a class="source" href="../factory/particleHistory.html"> ./source/factory/particleHistory.js </a> <a class="source" href="../factory/particleSpring.html"> ./source/factory/particleSpring.js </a> <a class="source" href="../factory/particleWorld.html"> ./source/factory/particleWorld.js </a> <a class="source" href="../factory/pattern.html"> ./source/factory/pattern.js </a> <a class="source" href="../factory/phrase.html"> ./source/factory/phrase.js </a> <a class="source" href="../factory/picture.html"> ./source/factory/picture.js </a> <a class="source" href="../factory/polygon.html"> ./source/factory/polygon.js </a> <a class="source" href="../factory/polyline.html"> ./source/factory/polyline.js </a> <a class="source" href="../factory/quadratic.html"> ./source/factory/quadratic.js </a> <a class="source" href="../factory/quaternion.html"> ./source/factory/quaternion.js </a> <a class="source" href="../factory/radialGradient.html"> ./source/factory/radialGradient.js </a> <a class="source" href="../factory/rawAsset.html"> ./source/factory/rawAsset.js </a> <a class="source" href="../factory/rdAsset.html"> ./source/factory/rdAsset.js </a> <a class="source" href="../factory/rectangle.html"> ./source/factory/rectangle.js </a> <a class="source" href="../factory/renderAnimation.html"> ./source/factory/renderAnimation.js </a> <a class="source" href="../factory/shape.html"> ./source/factory/shape.js </a> <a class="source" href="../factory/spiral.html"> ./source/factory/spiral.js </a> <a class="source" href="../factory/spriteAsset.html"> ./source/factory/spriteAsset.js </a> <a class="source" href="../factory/stack.html"> ./source/factory/stack.js </a> <a class="source" href="../factory/star.html"> ./source/factory/star.js </a> <a class="source" href="../factory/state.html"> ./source/factory/state.js </a> <a class="source" href="../factory/tetragon.html"> ./source/factory/tetragon.js </a> <a class="source" href="../factory/ticker.html"> ./source/factory/ticker.js </a> <a class="source" href="../factory/tracer.html"> ./source/factory/tracer.js </a> <a class="source" href="../factory/tween.html"> ./source/factory/tween.js </a> <a class="source" href="../factory/unstackedElement.html"> ./source/factory/unstackedElement.js </a> <a class="source" href="../factory/vector.html"> ./source/factory/vector.js </a> <a class="source" href="../factory/videoAsset.html"> ./source/factory/videoAsset.js </a> <a class="source" href="../factory/wheel.html"> ./source/factory/wheel.js </a> <a class="source" href="../mixin/anchor.html"> ./source/mixin/anchor.js </a> <a class="source" href="../mixin/asset.html"> ./source/mixin/asset.js </a> <a class="source" href="../mixin/assetAdvancedFunctionality.html"> ./source/mixin/assetAdvancedFunctionality.js </a> <a class="source" href="../mixin/assetConsumer.html"> ./source/mixin/assetConsumer.js </a> <a class="source" href="../mixin/base.html"> ./source/mixin/base.js </a> <a class="source" href="../mixin/cascade.html"> ./source/mixin/cascade.js </a> <a class="source" href="../mixin/delta.html"> ./source/mixin/delta.js </a> <a class="source" href="../mixin/displayShape.html"> ./source/mixin/displayShape.js </a> <a class="source" href="../mixin/dom.html"> ./source/mixin/dom.js </a> <a class="source" href="../mixin/entity.html"> ./source/mixin/entity.js </a> <a class="source" href="../mixin/filter.html"> ./source/mixin/filter.js </a> <a class="source" href="../mixin/mimic.html"> ./source/mixin/mimic.js </a> <a class="source" href="../mixin/path.html"> ./source/mixin/path.js </a> <a class="source" href="../mixin/pattern.html"> ./source/mixin/pattern.js </a> <a class="source" href="../mixin/pivot.html"> ./source/mixin/pivot.js </a> <a class="source" href="../mixin/position.html"> ./source/mixin/position.js </a> <a class="source" href="../mixin/shapeBasic.html"> ./source/mixin/shapeBasic.js </a> <a class="source" href="../mixin/shapeCurve.html"> ./source/mixin/shapeCurve.js </a> <a class="source" href="../mixin/shapePathCalculation.html"> ./source/mixin/shapePathCalculation.js </a> <a class="source" href="../mixin/styles.html"> ./source/mixin/styles.js </a> <a class="source" href="../mixin/tween.html"> ./source/mixin/tween.js </a> </div> </div> </li> </ul> <ul class="sections"> <li id="section-1"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-1">&#x00a7;</a> </div> <h1 id="dom-management">DOM management</h1> <p>Core DOM element discovery and management functionality, and the Scrawl-canvas <strong>Display cycle</strong>.</p> <p>Scrawl-canvas breaks down the display cycle into three parts: <strong>clear</strong>; <strong>compile</strong>; and <strong>show</strong>. These can be triggered either as a single, combined <strong>render</strong> operation, or separately as-and-when needed.</p> <p>The order in which DOM stack and canvas elements are processed during the display cycle can be changed by setting that element’s controller’s <strong>order</strong> attribute to a higher or lower integer value; elements are processed (in batches) from lowest to highest order value</p> </div> </li> <li id="section-2"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-2">&#x00a7;</a> </div> <h4 id="imports">Imports</h4> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">import</span> { isa_canvas, generateUniqueString, isa_dom, pushUnique, removeItem, xt, Ωempty } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;./utilities.js&quot;</span>; <span class="hljs-keyword">import</span> { artefact, canvas, group, stack, css, xcss } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;./library.js&quot;</span>; <span class="hljs-keyword">import</span> { getPixelRatio, getIgnorePixelRatio } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;./events.js&quot;</span>; <span class="hljs-keyword">import</span> { makeStack } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;../factory/stack.js&quot;</span>; <span class="hljs-keyword">import</span> { makeElement } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;../factory/element.js&quot;</span>; <span class="hljs-keyword">import</span> { makeCanvas } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;../factory/canvas.js&quot;</span>;</pre></div></div> </li> <li id="section-3"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-3">&#x00a7;</a> </div> <h4 id="root-elements">Root elements</h4> <p><code>Exported array and function</code> (to modules). Root elements are the boxes whose dimensions define the dimensions and positioning of all that they contain. All &lt;canvas&gt; elements are root elements. Scrawl-canvas also allows other DOM elements (typically &lt;div&gt; elements) to be defined as root elements called <strong>stacks</strong>, whose child elements can then be positioned and dimensioned just like entity objects in a canvas.</p> <p>Stacks can be nested within each other, and canvases can also be nested inside a stack. (Nothing can nest inside a canvas element). Only the top level stack will be included in the rootElements array.</p> <p>The Scrawl-canvas <strong>Display cycle</strong> can start at the rootElements array, with each member of the array processed in turn.</p> <p>The <strong>rootElements</strong> array keeps track of all ‘root’ stack artefacts, and also includes all canvas artefacts, whether they’re part of a stack or not. <strong>setRootElementSort</strong> forces the root</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">let</span> rootElements = [], setRootElementsSort = <span class="hljs-function">() =&gt;</span> {rootElementsSort = <span class="hljs-literal">true</span>};</pre></div></div> </li> <li id="section-4"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-4">&#x00a7;</a> </div> <p>Local variables</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">let</span> rootElements_sorted = [], rootElementsSort = <span class="hljs-literal">true</span>;</pre></div></div> </li> <li id="section-5"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-5">&#x00a7;</a> </div> <p>Scrawl-canvas rootElements sorter uses a ‘bucket sort’ algorithm</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> sortRootElements = <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) { <span class="hljs-keyword">let</span> floor = <span class="hljs-title class_">Math</span>.<span class="hljs-property">floor</span>; <span class="hljs-keyword">if</span> (rootElementsSort) { rootElementsSort = <span class="hljs-literal">false</span>; <span class="hljs-keyword">let</span> buckets = [], art, order, i, iz, item; <span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>, iz = rootElements.<span class="hljs-property">length</span>; i &lt; iz; i++) { item = rootElements[i]; art = artefact[item]; <span class="hljs-keyword">if</span> (art) { order = <span class="hljs-title function_">floor</span>(art.<span class="hljs-property">order</span>) || <span class="hljs-number">0</span>; <span class="hljs-keyword">if</span> (!buckets[order]) buckets[order] = []; buckets[order].<span class="hljs-title function_">push</span>(art.<span class="hljs-property">name</span>); } } rootElements_sorted = buckets.<span class="hljs-title function_">reduce</span>(<span class="hljs-function">(<span class="hljs-params">a, v</span>) =&gt;</span> a.<span class="hljs-title function_">concat</span>(v), []); } };</pre></div></div> </li> <li id="section-6"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-6">&#x00a7;</a> </div> <h4 id="stack-discovery">Stack discovery</h4> <p><code>Exported function</code> (to modules). Parse the DOM, looking for all elements that have been given a <strong>data-stack</strong> attribute; then create <strong>Stack</strong> artefact wrappers for each of them. </p> <p>This function will also create wrappers for all <strong>direct child elements</strong> (one level down) within the stack, and create appropriate wrappers (Stack, Canvas, Element) for them.</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> getStacks = <span class="hljs-keyword">function</span> (<span class="hljs-params">query = <span class="hljs-string">&#x27;[data-scrawl-stack]&#x27;</span></span>) { <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelectorAll</span>(query).<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">el</span> =&gt;</span> <span class="hljs-title function_">addInitialStackElement</span>(el)); };</pre></div></div> </li> <li id="section-7"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-7">&#x00a7;</a> </div> <p>Create a <strong>stack</strong> artefact wrapper for a given stack element.</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> addInitialStackElement = <span class="hljs-keyword">function</span> (<span class="hljs-params">el</span>) { <span class="hljs-keyword">let</span> mygroup = el.<span class="hljs-title function_">getAttribute</span>(<span class="hljs-string">&#x27;data-scrawl-group&#x27;</span>), myname = el.<span class="hljs-property">id</span> || el.<span class="hljs-title function_">getAttribute</span>(<span class="hljs-string">&#x27;name&#x27;</span>), position = <span class="hljs-string">&#x27;absolute&#x27;</span>; <span class="hljs-keyword">if</span> (!mygroup) { el.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;data-scrawl-group&#x27;</span>, <span class="hljs-string">&#x27;root&#x27;</span>); mygroup = <span class="hljs-string">&#x27;root&#x27;</span>; position = <span class="hljs-string">&#x27;relative&#x27;</span>; } <span class="hljs-keyword">if</span> (!myname) { myname = <span class="hljs-title function_">generateUniqueString</span>(); el.<span class="hljs-property">id</span> = myname; } <span class="hljs-keyword">let</span> mystack = <span class="hljs-title function_">makeStack</span>({ <span class="hljs-attr">name</span>: myname, <span class="hljs-attr">domElement</span>: el, <span class="hljs-attr">group</span>: mygroup, <span class="hljs-attr">host</span>: mygroup, <span class="hljs-attr">position</span>: position, <span class="hljs-attr">setInitialDimensions</span>: <span class="hljs-literal">true</span> }); <span class="hljs-title function_">processNewStackChildren</span>(el, myname); <span class="hljs-keyword">return</span> mystack; };</pre></div></div> </li> <li id="section-8"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-8">&#x00a7;</a> </div> <p>Helper function for addInitialStackElement()</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> processNewStackChildren = <span class="hljs-keyword">function</span> (<span class="hljs-params">el, name</span>) { <span class="hljs-keyword">let</span> hostDims = el.<span class="hljs-title function_">getBoundingClientRect</span>(), y = <span class="hljs-number">0</span>;</pre></div></div> </li> <li id="section-9"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-9">&#x00a7;</a> </div> <p>Only go down one level of hierarchy here; stacks don’t do hierarchies, only interested in knowing about immediate child elements</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-title class_">Array</span>.<span class="hljs-title function_">from</span>(el.<span class="hljs-property">children</span>).<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">child</span> =&gt;</span> { <span class="hljs-keyword">if</span> (child.<span class="hljs-title function_">getAttribute</span>(<span class="hljs-string">&#x27;data-scrawl-stack&#x27;</span>) == <span class="hljs-literal">null</span> &amp;&amp; !<span class="hljs-title function_">isa_canvas</span>(child) &amp;&amp; child.<span class="hljs-property">tagName</span> !== <span class="hljs-string">&#x27;SCRIPT&#x27;</span>) { <span class="hljs-keyword">let</span> dims = child.<span class="hljs-title function_">getBoundingClientRect</span>(), computed = <span class="hljs-variable language_">window</span>.<span class="hljs-title function_">getComputedStyle</span>(child); <span class="hljs-keyword">let</span> yHeight = <span class="hljs-built_in">parseFloat</span>(computed.<span class="hljs-property">marginTop</span>) + <span class="hljs-built_in">parseFloat</span>(computed.<span class="hljs-property">borderTopWidth</span>) + <span class="hljs-built_in">parseFloat</span>(computed.<span class="hljs-property">paddingTop</span>) + <span class="hljs-built_in">parseFloat</span>(computed.<span class="hljs-property">paddingBottom</span>) + <span class="hljs-built_in">parseFloat</span>(computed.<span class="hljs-property">borderBottomWidth</span>) + <span class="hljs-built_in">parseFloat</span>(computed.<span class="hljs-property">marginBottom</span>); y = (!y) ? dims.<span class="hljs-property">top</span> - hostDims.<span class="hljs-property">top</span> : y; <span class="hljs-keyword">let</span> args = { <span class="hljs-attr">name</span>: child.<span class="hljs-property">id</span> || child.<span class="hljs-title function_">getAttribute</span>(<span class="hljs-string">&#x27;name&#x27;</span>), <span class="hljs-attr">domElement</span>: child, <span class="hljs-attr">group</span>: name, <span class="hljs-attr">host</span>: name, <span class="hljs-attr">position</span>: <span class="hljs-string">&#x27;absolute&#x27;</span>, <span class="hljs-attr">width</span>: dims.<span class="hljs-property">width</span>, <span class="hljs-attr">height</span>: dims.<span class="hljs-property">height</span>, <span class="hljs-attr">startX</span>: dims.<span class="hljs-property">left</span> - hostDims.<span class="hljs-property">left</span>, <span class="hljs-attr">startY</span>: y, <span class="hljs-attr">classes</span>: (child.<span class="hljs-property">className</span>) ? child.<span class="hljs-property">className</span> : <span class="hljs-string">&#x27;&#x27;</span>, }; y += yHeight + dims.<span class="hljs-property">height</span>; <span class="hljs-title function_">makeElement</span>(args); }</pre></div></div> </li> <li id="section-10"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-10">&#x00a7;</a> </div> <p>No need to worry about processing child stacks - they’ll already be in the list of stacks to be processed</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">else</span> child.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;data-scrawl-group&#x27;</span>, name); }); };</pre></div></div> </li> <li id="section-11"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-11">&#x00a7;</a> </div> <p><code>Exported function</code> (to modules and scrawl object). Use <strong>addStack</strong> to add a Scrawl-canvas stack element to a web page, or transform an existing element into a stack element. The items argument can include </p> <ul> <li><strong>element</strong> - the DOM element to be the stack - if not present, will autogenerate a div element</li> <li><strong>host</strong> - the host element, either as the DOM element itself, or some sort of CSS search string; if not present, will create the stack at the stack element’s current place or, failing all else, add the stack to the end of the document body</li> <li><strong>name</strong> - String identifier for the stack; if not included the function will attempt to use the element’s existing id or name attribute or, failing that, generate a random name for the stack.</li> <li>any other regular stack attribute</li> </ul> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> addStack = <span class="hljs-keyword">function</span> (<span class="hljs-params">items = Ωempty</span>) {</pre></div></div> </li> <li id="section-12"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-12">&#x00a7;</a> </div> <p>define variables</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">let</span> el, host, hostinscrawl, mystack, mygroup, name, position = <span class="hljs-string">&#x27;absolute&#x27;</span>, newElement = <span class="hljs-literal">false</span>;</pre></div></div> </li> <li id="section-13"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-13">&#x00a7;</a> </div> <p>get, or generate a new, stack-to-be element</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (items.<span class="hljs-property">element</span> &amp;&amp; items.<span class="hljs-property">element</span>.<span class="hljs-property">substring</span>) el = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelector</span>(items.<span class="hljs-property">element</span>); <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-title function_">isa_dom</span>(items.<span class="hljs-property">element</span>)) el = items.<span class="hljs-property">element</span>; <span class="hljs-keyword">else</span> { newElement = <span class="hljs-literal">true</span>; el = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">createElement</span>(<span class="hljs-string">&#x27;div&#x27;</span>); }</pre></div></div> </li> <li id="section-14"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-14">&#x00a7;</a> </div> <p>get element’s host (parent-to-be) element</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (items.<span class="hljs-property">host</span> &amp;&amp; items.<span class="hljs-property">host</span>.<span class="hljs-property">substring</span>) { host = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelector</span>(items.<span class="hljs-property">host</span>); <span class="hljs-keyword">if</span> (!host) host = <span class="hljs-variable language_">document</span>.<span class="hljs-property">body</span>; } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-title function_">isa_dom</span>(items.<span class="hljs-property">host</span>)) host = items.<span class="hljs-property">host</span>; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-title function_">xt</span>(el.<span class="hljs-property">parentElement</span>)) host = el.<span class="hljs-property">parentElement</span>; <span class="hljs-keyword">else</span> host = <span class="hljs-variable language_">document</span>.<span class="hljs-property">body</span>;</pre></div></div> </li> <li id="section-15"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-15">&#x00a7;</a> </div> <p>if dimensions have been set in the argument, apply them to the stack-to-be element</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (<span class="hljs-title function_">xt</span>(items.<span class="hljs-property">width</span>)) el.<span class="hljs-property">style</span>.<span class="hljs-property">width</span> = (items.<span class="hljs-property">width</span>.<span class="hljs-property">toFixed</span>) ? <span class="hljs-string">`<span class="hljs-subst">${items.width}</span>px`</span> : items.<span class="hljs-property">width</span>; <span class="hljs-keyword">if</span> (<span class="hljs-title function_">xt</span>(items.<span class="hljs-property">height</span>)) el.<span class="hljs-property">style</span>.<span class="hljs-property">height</span> = (items.<span class="hljs-property">height</span>.<span class="hljs-property">toFixed</span>) ? <span class="hljs-string">`<span class="hljs-subst">${items.height}</span>px`</span> : items.<span class="hljs-property">height</span>;</pre></div></div> </li> <li id="section-16"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-16">&#x00a7;</a> </div> <p>make sure the stack-to-be element has an id attribute</p> </div> <div class="content"><div class='highlight'><pre> name = items.<span class="hljs-property">name</span> || el.<span class="hljs-property">id</span> || el.<span class="hljs-title function_">getAttribute</span>(<span class="hljs-string">&#x27;name&#x27;</span>) || <span class="hljs-string">&#x27;&#x27;</span>; <span class="hljs-keyword">if</span> (!name) name = <span class="hljs-title function_">generateUniqueString</span>(); el.<span class="hljs-property">id</span> = name;</pre></div></div> </li> <li id="section-17"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-17">&#x00a7;</a> </div> <p>set the ‘data-scrawl-stack’ attribute on the stack-to-be element</p> </div> <div class="content"><div class='highlight'><pre> el.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;data-scrawl-stack&#x27;</span>, <span class="hljs-string">&#x27;data-scrawl-stack&#x27;</span>);</pre></div></div> </li> <li id="section-18"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-18">&#x00a7;</a> </div> <p>determine whether the parent element is already known to Scrawl-canvas - affects the stack-to-be element’s group </p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (host &amp;&amp; host.<span class="hljs-title function_">getAttribute</span>(<span class="hljs-string">&#x27;data-scrawl-stack&#x27;</span>) != <span class="hljs-literal">null</span>) { hostinscrawl = artefact[host.<span class="hljs-property">id</span>]; mygroup = (hostinscrawl) ? hostinscrawl.<span class="hljs-property">name</span> : <span class="hljs-string">&#x27;root&#x27;</span>; } <span class="hljs-keyword">else</span> mygroup = <span class="hljs-string">&#x27;root&#x27;</span>;</pre></div></div> </li> <li id="section-19"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-19">&#x00a7;</a> </div> <p>set the ‘data-scrawl-group’ attribute on the stack-to-be element</p> </div> <div class="content"><div class='highlight'><pre> el.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;data-scrawl-group&#x27;</span>, mygroup);</pre></div></div> </li> <li id="section-20"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-20">&#x00a7;</a> </div> <p>determine what the stack-to-be element’s position style attribute will be</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (mygroup === <span class="hljs-string">&#x27;root&#x27;</span>) position = <span class="hljs-string">&#x27;relative&#x27;</span>;</pre></div></div> </li> <li id="section-21"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-21">&#x00a7;</a> </div> <p>add (or move) the stack-to-be element to/in the DOM</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (!el.<span class="hljs-property">parentElement</span> || host.<span class="hljs-property">id</span> !== el.<span class="hljs-property">parentElement</span>.<span class="hljs-property">id</span>) host.<span class="hljs-title function_">appendChild</span>(el);</pre></div></div> </li> <li id="section-22"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-22">&#x00a7;</a> </div> <p>create the Scrawl-canvas Stack artefact</p> </div> <div class="content"><div class='highlight'><pre> mystack = <span class="hljs-title function_">makeStack</span>({ <span class="hljs-attr">name</span>: name, <span class="hljs-attr">domElement</span>: el, <span class="hljs-attr">group</span>: mygroup, <span class="hljs-attr">host</span>: mygroup, <span class="hljs-attr">position</span>: position, <span class="hljs-attr">setInitialDimensions</span>: <span class="hljs-literal">true</span> }); <span class="hljs-title function_">processNewStackChildren</span>(el, name);</pre></div></div> </li> <li id="section-23"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-23">&#x00a7;</a> </div> <p>in case any of the child elements were already a Scrawl-canvas stack, un-root them (if required)</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-title class_">Array</span>.<span class="hljs-title function_">from</span>(el.<span class="hljs-property">childNodes</span>).<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">child</span> =&gt;</span> { <span class="hljs-keyword">if</span> (child.<span class="hljs-property">id</span> &amp;&amp; rootElements.<span class="hljs-title function_">indexOf</span>(child.<span class="hljs-property">id</span>) &gt;= <span class="hljs-number">0</span>) <span class="hljs-title function_">removeItem</span>(rootElements, child.<span class="hljs-property">id</span>); });</pre></div></div> </li> <li id="section-24"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-24">&#x00a7;</a> </div> <p>set the new Stack’s remaining attributes, clearing out any attributes already handled</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">delete</span> items.<span class="hljs-property">name</span>; <span class="hljs-keyword">delete</span> items.<span class="hljs-property">element</span>; <span class="hljs-keyword">delete</span> items.<span class="hljs-property">host</span>; <span class="hljs-keyword">delete</span> items.<span class="hljs-property">width</span>; <span class="hljs-keyword">delete</span> items.<span class="hljs-property">height</span>; mystack.<span class="hljs-title function_">set</span>(items);</pre></div></div> </li> <li id="section-25"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-25">&#x00a7;</a> </div> <p>tidy up and complete</p> </div> <div class="content"><div class='highlight'><pre> rootElementsSort = <span class="hljs-literal">true</span>; <span class="hljs-keyword">return</span> mystack; };</pre></div></div> </li> <li id="section-26"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-26">&#x00a7;</a> </div> <h4 id="canvas-discovery">Canvas discovery</h4> <p><code>Exported function</code> (to modules). Parse the DOM, looking for &lt;canvas&gt; elements; then create <strong>Canvas</strong> artefact and <strong>Cell</strong> asset wrappers for each canvas found. Canvas elements do not need to be part of a stack and can appear anywhere in the HTML body.</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> getCanvases = <span class="hljs-keyword">function</span> (<span class="hljs-params">query = <span class="hljs-string">&#x27;[data-scrawl-canvas]&#x27;</span></span>) { <span class="hljs-keyword">let</span> item; <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelectorAll</span>(query).<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">el, index</span>) =&gt;</span> { item = <span class="hljs-title function_">addInitialCanvasElement</span>(el); <span class="hljs-keyword">if</span> (!index) <span class="hljs-title function_">setCurrentCanvas</span>(item); }); };</pre></div></div> </li> <li id="section-27"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-27">&#x00a7;</a> </div> <p>Create a <strong>canvas</strong> artefact wrapper for a given canvas element.</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> addInitialCanvasElement = <span class="hljs-keyword">function</span> (<span class="hljs-params">el</span>) { <span class="hljs-keyword">let</span> mygroup = el.<span class="hljs-title function_">getAttribute</span>(<span class="hljs-string">&#x27;data-scrawl-group&#x27;</span>), myname = el.<span class="hljs-property">id</span> || el.<span class="hljs-title function_">getAttribute</span>(<span class="hljs-string">&#x27;name&#x27;</span>), position = <span class="hljs-string">&#x27;absolute&#x27;</span>; <span class="hljs-keyword">if</span> (!mygroup) { el.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;data-scrawl-group&#x27;</span>, <span class="hljs-string">&#x27;root&#x27;</span>); mygroup = <span class="hljs-string">&#x27;root&#x27;</span>; position = el.<span class="hljs-property">style</span>.<span class="hljs-property">position</span>; } <span class="hljs-keyword">if</span> (!myname) { myname = <span class="hljs-title function_">generateUniqueString</span>(); el.<span class="hljs-property">id</span> = myname; } <span class="hljs-keyword">return</span> <span class="hljs-title function_">makeCanvas</span>({ <span class="hljs-attr">name</span>: myname, <span class="hljs-attr">domElement</span>: el, <span class="hljs-attr">group</span>: mygroup, <span class="hljs-attr">host</span>: mygroup, <span class="hljs-attr">position</span>: position, <span class="hljs-attr">setInitialDimensions</span>: <span class="hljs-literal">true</span> }); };</pre></div></div> </li> <li id="section-28"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-28">&#x00a7;</a> </div> <p><code>Exported function</code> (to modules and scrawl object). Parse the DOM, looking for a specific &lt;canvas&gt; element; then create a <strong>Canvas</strong> artefact and <strong>Cell</strong> asset wrapper for it.</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> getCanvas = <span class="hljs-keyword">function</span> (<span class="hljs-params">search</span>) { <span class="hljs-keyword">let</span> el = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelector</span>(search), canvas = <span class="hljs-literal">false</span>; <span class="hljs-keyword">if</span> (el) canvas = <span class="hljs-title function_">addInitialCa