UNPKG

scrawl-canvas

Version:
955 lines (629 loc) 119 kB
<!DOCTYPE html> <html> <head> <title>Canvas factory</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="../core/animationloop.html"> ./source/core/animationloop.js </a> <a class="source" href="../core/document.html"> ./source/core/document.js </a> <a class="source" href="../core/events.html"> ./source/core/events.js </a> <a class="source" href="../core/init.html"> ./source/core/init.js </a> <a class="source" href="../core/library.html"> ./source/core/library.js </a> <a class="source" href="../core/random-seed.html"> ./source/core/random-seed.js </a> <a class="source" href="../core/snippets.html"> ./source/core/snippets.js </a> <a class="source" href="../core/userInteraction.html"> ./source/core/userInteraction.js </a> <a class="source" href="../core/utilities.html"> ./source/core/utilities.js </a> <a class="source" href="action.html"> ./source/factory/action.js </a> <a class="source" href="anchor.html"> ./source/factory/anchor.js </a> <a class="source" href="animation.html"> ./source/factory/animation.js </a> <a class="source" href="bezier.html"> ./source/factory/bezier.js </a> <a class="source" href="block.html"> ./source/factory/block.js </a> <a class="source" href="canvas.html"> ./source/factory/canvas.js </a> <a class="source" href="cell.html"> ./source/factory/cell.js </a> <a class="source" href="cog.html"> ./source/factory/cog.js </a> <a class="source" href="color.html"> ./source/factory/color.js </a> <a class="source" href="conicGradient.html"> ./source/factory/conicGradient.js </a> <a class="source" href="coordinate.html"> ./source/factory/coordinate.js </a> <a class="source" href="crescent.html"> ./source/factory/crescent.js </a> <a class="source" href="element.html"> ./source/factory/element.js </a> <a class="source" href="emitter.html"> ./source/factory/emitter.js </a> <a class="source" href="filter.html"> ./source/factory/filter.js </a> <a class="source" href="filterEngine-bluenoiseData.html"> ./source/factory/filterEngine-bluenoiseData.js </a> <a class="source" href="filterEngine.html"> ./source/factory/filterEngine.js </a> <a class="source" href="fontAttributes.html"> ./source/factory/fontAttributes.js </a> <a class="source" href="gradient.html"> ./source/factory/gradient.js </a> <a class="source" href="grid.html"> ./source/factory/grid.js </a> <a class="source" href="group.html"> ./source/factory/group.js </a> <a class="source" href="imageAsset.html"> ./source/factory/imageAsset.js </a> <a class="source" href="line.html"> ./source/factory/line.js </a> <a class="source" href="lineSpiral.html"> ./source/factory/lineSpiral.js </a> <a class="source" href="loom.html"> ./source/factory/loom.js </a> <a class="source" href="mesh.html"> ./source/factory/mesh.js </a> <a class="source" href="net.html"> ./source/factory/net.js </a> <a class="source" href="noiseAsset.html"> ./source/factory/noiseAsset.js </a> <a class="source" href="oval.html"> ./source/factory/oval.js </a> <a class="source" href="palette.html"> ./source/factory/palette.js </a> <a class="source" href="particle.html"> ./source/factory/particle.js </a> <a class="source" href="particleForce.html"> ./source/factory/particleForce.js </a> <a class="source" href="particleHistory.html"> ./source/factory/particleHistory.js </a> <a class="source" href="particleSpring.html"> ./source/factory/particleSpring.js </a> <a class="source" href="particleWorld.html"> ./source/factory/particleWorld.js </a> <a class="source" href="pattern.html"> ./source/factory/pattern.js </a> <a class="source" href="phrase.html"> ./source/factory/phrase.js </a> <a class="source" href="picture.html"> ./source/factory/picture.js </a> <a class="source" href="polygon.html"> ./source/factory/polygon.js </a> <a class="source" href="polyline.html"> ./source/factory/polyline.js </a> <a class="source" href="quadratic.html"> ./source/factory/quadratic.js </a> <a class="source" href="quaternion.html"> ./source/factory/quaternion.js </a> <a class="source" href="radialGradient.html"> ./source/factory/radialGradient.js </a> <a class="source" href="rawAsset.html"> ./source/factory/rawAsset.js </a> <a class="source" href="rdAsset.html"> ./source/factory/rdAsset.js </a> <a class="source" href="rectangle.html"> ./source/factory/rectangle.js </a> <a class="source" href="renderAnimation.html"> ./source/factory/renderAnimation.js </a> <a class="source" href="shape.html"> ./source/factory/shape.js </a> <a class="source" href="spiral.html"> ./source/factory/spiral.js </a> <a class="source" href="spriteAsset.html"> ./source/factory/spriteAsset.js </a> <a class="source" href="stack.html"> ./source/factory/stack.js </a> <a class="source" href="star.html"> ./source/factory/star.js </a> <a class="source" href="state.html"> ./source/factory/state.js </a> <a class="source" href="tetragon.html"> ./source/factory/tetragon.js </a> <a class="source" href="ticker.html"> ./source/factory/ticker.js </a> <a class="source" href="tracer.html"> ./source/factory/tracer.js </a> <a class="source" href="tween.html"> ./source/factory/tween.js </a> <a class="source" href="unstackedElement.html"> ./source/factory/unstackedElement.js </a> <a class="source" href="vector.html"> ./source/factory/vector.js </a> <a class="source" href="videoAsset.html"> ./source/factory/videoAsset.js </a> <a class="source" href="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="canvas-factory">Canvas factory</h1> <p>Scrawl-canvas mediates between its system and &lt;canvas&gt; elements in the DOM using Canvas wrapper objects. The wrapper includes a handle to the visible element, but most work is done on a second &lt;canvas&gt; element - the <strong>base Cell</strong> - which is not part of the DOM, thus excluded from a browser’s Document interface (including events).</p> <p>The Canvas factory is not used directly; the factory is not exported as part of the <a href="../scrawl.html">scrawl object</a> during Scrawl-canvas initialization. Instead, wrappers can be created for DOM-based &lt;canvas&gt; elements using the following scrawl functions:</p> <ul> <li><code>scrawl.getCanvas</code> - locates a &lt;canvas&gt; element in the DOM and creates a wrapper for it.</li> <li><code>scrawl.addCanvas</code> - generates a new &lt;canvas&gt; element, creates a wrapper for it, then adds it to the DOM.</li> </ul> <p>During initialization Scrawl-canvas will search the DOM tree and automatically create Canvas wrappers for all the &lt;canvas&gt; elements it discovers. The first &lt;canvas&gt; element discovered becomes the <strong>current canvas</strong>; all entitys created without a specified <code>group</code> attribute will be assigned to that element’s wrapper’s base Cell’s Group object. We can change the current canvas by invoking the <code>scrawl.setCurrentCanvas</code> function.</p> <p>A canvas wrapper can include more than one Cell object. It will always include a base Cell object; additional Cells can be treated as <em><strong>cell layers</strong></em> and/or normal artefacts contributing to the final display.</p> <p>During their creation, Canvas wrappers will directly modify the DOM, adding &lt;div&gt; and &lt;nav&gt; elements to it. These new elements are used as <em>holds</em> where the Canvas will store data and text, mainly to expose &lt;a&gt; links and &lt;p&gt; blocks which expose scene details to assistive technologies (accessibility). These additional elements have zero dimensions and should not affect the layout or painting of the rest of the web page.</p> <p>Canvas wrapper objects use the <strong>base</strong>, <strong>position</strong>, <strong>dom</strong> and <strong>anchor</strong> mixins. Thus Canvas wrappers are also <strong>artefact</strong> objects: if a &lt;canvas&gt; element is a direct child of a Stack wrapper’s element then it can be positioned, dimensioned and rotated like any other artefact.</p> <p>By default, all Canvas wrappers will track mouse/touch movements across their &lt;canvas&gt; elements, supplying this data to constituent Cell objects and artefacts as-and-when-required.</p> <p>Canvas wrappers are used by Scrawl-canvas to invoke the <strong>Display cycle cascade</strong>. As such, they include <code>clear</code>, <code>compile</code>, <code>show</code> and <code>render</code> functions to manage the Display cycle. These functions are asynchronous, returning Promises.</p> <p>Canvas wrappers are excluded from the Scrawl-canvas packet system; they cannot be saved or cloned. Killing a Canvas wrapper will remove its &lt;canvas&gt; element from the DOM, alongside the additional elements added to the DOM during Canvas creation.</p> </div> </li> <li id="section-2"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-2">&#x00a7;</a> </div> <h4 id="demos">Demos:</h4> <ul> <li>All canvas and packets demos, and a few of the stack demos, include Canvas wrapper functionality - most of which happens behind the scenes and does not need to be directly coded. </li> <li><a href="../../demo/canvas-009.html">Canvas-009</a> - Pattern styles; Entity web link anchors; Dynamic accessibility</li> <li><a href="../../demo/dom-011.html">DOM-011</a> - Canvas controller <code>fit</code> attribute; Cell positioning (mouse)</li> <li><a href="../../demo/dom-012.html">DOM-012</a> - Add and remove (kill) Scrawl-canvas canvas elements programmatically</li> </ul> </div> </li> <li id="section-3"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-3">&#x00a7;</a> </div> <h4 id="imports">Imports</h4> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">import</span> { cell, constructors, artefact, group } <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../core/library.js&#x27;</span>; <span class="hljs-keyword">import</span> { rootElements, setRootElementsSort, setCurrentCanvas, domShow, scrawlCanvasHold } <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../core/document.js&#x27;</span>; <span class="hljs-keyword">import</span> { mergeOver, pushUnique, removeItem, xt, λ<span class="hljs-variable language_">this</span>, λ<span class="hljs-literal">null</span>, Ωempty } <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../core/utilities.js&#x27;</span>; <span class="hljs-keyword">import</span> { uiSubscribedElements } <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../core/userInteraction.js&#x27;</span>; <span class="hljs-keyword">import</span> { makeState } <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./state.js&#x27;</span>; <span class="hljs-keyword">import</span> { makeCell } <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./cell.js&#x27;</span>; <span class="hljs-keyword">import</span> { makeCoordinate } <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./coordinate.js&#x27;</span>; <span class="hljs-keyword">import</span> baseMix <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../mixin/base.js&#x27;</span>; <span class="hljs-keyword">import</span> domMix <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../mixin/dom.js&#x27;</span>; <span class="hljs-keyword">import</span> displayMix <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../mixin/displayShape.js&#x27;</span>;</pre></div></div> </li> <li id="section-4"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-4">&#x00a7;</a> </div> <h4 id="canvas-constructor">Canvas constructor</h4> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> <span class="hljs-title class_">Canvas</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">items = Ωempty</span>) { <span class="hljs-keyword">let</span> g, el; <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">makeName</span>(items.<span class="hljs-property">name</span>); <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">register</span>(); <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">initializePositions</span>(); <span class="hljs-variable language_">this</span>.<span class="hljs-property">dimensions</span>[<span class="hljs-number">0</span>] = <span class="hljs-number">300</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-property">dimensions</span>[<span class="hljs-number">1</span>] = <span class="hljs-number">150</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-property">pathCorners</span> = []; <span class="hljs-variable language_">this</span>.<span class="hljs-property">css</span> = {}; <span class="hljs-variable language_">this</span>.<span class="hljs-property">here</span> = {}; <span class="hljs-variable language_">this</span>.<span class="hljs-property">perspective</span> = { <span class="hljs-attr">x</span>: <span class="hljs-string">&#x27;50%&#x27;</span>, <span class="hljs-attr">y</span>: <span class="hljs-string">&#x27;50%&#x27;</span>, <span class="hljs-attr">z</span>: <span class="hljs-number">0</span> }; <span class="hljs-variable language_">this</span>.<span class="hljs-property">dirtyPerspective</span> = <span class="hljs-literal">true</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">initializeDomLayout</span>(items); <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">set</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">defs</span>); <span class="hljs-keyword">if</span> (!items.<span class="hljs-property">label</span>) items.<span class="hljs-property">label</span> = <span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-variable language_">this</span>.name}</span> canvas element`</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">initializeDisplayShapeActions</span>(); <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">initializeAccessibility</span>(); <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">set</span>(items); <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">cleanDimensions</span>(); el = <span class="hljs-variable language_">this</span>.<span class="hljs-property">domElement</span>; <span class="hljs-keyword">if</span> (!el) <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">cleanDimensions</span>(); <span class="hljs-keyword">else</span> { <span class="hljs-variable language_">this</span>.<span class="hljs-property">engine</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">domElement</span>.<span class="hljs-title function_">getContext</span>(<span class="hljs-string">&#x27;2d&#x27;</span>); <span class="hljs-variable language_">this</span>.<span class="hljs-property">state</span> = <span class="hljs-title function_">makeState</span>({ <span class="hljs-attr">engine</span>: <span class="hljs-variable language_">this</span>.<span class="hljs-property">engine</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>set up additional cells array</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-variable language_">this</span>.<span class="hljs-property">cells</span> = []; <span class="hljs-variable language_">this</span>.<span class="hljs-property">cellBatchesClear</span> = []; <span class="hljs-variable language_">this</span>.<span class="hljs-property">cellBatchesCompile</span> = []; <span class="hljs-variable language_">this</span>.<span class="hljs-property">cellBatchesShow</span> = []; <span class="hljs-keyword">let</span> baseWidth = <span class="hljs-variable language_">this</span>.<span class="hljs-property">currentDimensions</span>[<span class="hljs-number">0</span>], baseHeight = <span class="hljs-variable language_">this</span>.<span class="hljs-property">currentDimensions</span>[<span class="hljs-number">1</span>]; <span class="hljs-keyword">const</span> ds = el.<span class="hljs-property">dataset</span>; <span class="hljs-keyword">if</span> (ds.<span class="hljs-property">isResponsive</span>) { el.<span class="hljs-property">style</span>.<span class="hljs-property">width</span> = <span class="hljs-string">&#x27;100%&#x27;</span>; el.<span class="hljs-property">style</span>.<span class="hljs-property">height</span> = <span class="hljs-string">&#x27;100%&#x27;</span>; <span class="hljs-keyword">let</span> baseMatches = <span class="hljs-literal">true</span>; <span class="hljs-keyword">if</span> (ds.<span class="hljs-property">baseWidth</span> &amp;&amp; ds.<span class="hljs-property">baseHeight</span>) { baseMatches = <span class="hljs-literal">false</span>; baseWidth = <span class="hljs-built_in">parseFloat</span>(ds.<span class="hljs-property">baseWidth</span>); baseHeight = <span class="hljs-built_in">parseFloat</span>(ds.<span class="hljs-property">baseHeight</span>); } <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">set</span>({ <span class="hljs-attr">checkForResize</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">baseMatchesCanvasDimensions</span>: baseMatches, <span class="hljs-attr">ignoreCanvasCssDimensions</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">fit</span>: ds.<span class="hljs-property">fit</span> || <span class="hljs-variable language_">this</span>.<span class="hljs-property">fit</span>, }); <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">cleanDimensions</span>(); }</pre></div></div> </li> <li id="section-6"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-6">&#x00a7;</a> </div> <p>setup base cell</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">const</span> cellArgs = { <span class="hljs-attr">name</span>: <span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-variable language_">this</span>.name}</span>_base`</span>, <span class="hljs-attr">element</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">width</span>: baseWidth, <span class="hljs-attr">height</span>: baseHeight, <span class="hljs-attr">cleared</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">compiled</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">shown</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">isBase</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">host</span>: <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span>, <span class="hljs-attr">controller</span>: <span class="hljs-variable language_">this</span>, <span class="hljs-attr">order</span>: <span class="hljs-number">10</span>, }; <span class="hljs-keyword">if</span> (ds.<span class="hljs-property">baseClearAlpha</span>) cellArgs.<span class="hljs-property">clearAlpha</span> = <span class="hljs-built_in">parseFloat</span>(ds.<span class="hljs-property">baseClearAlpha</span>); <span class="hljs-keyword">if</span> (ds.<span class="hljs-property">baseBackgroundColor</span>) cellArgs.<span class="hljs-property">backgroundColor</span> = ds.<span class="hljs-property">baseBackgroundColor</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-property">base</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">buildCell</span>(cellArgs);</pre></div></div> </li> <li id="section-7"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-7">&#x00a7;</a> </div> <p>even if it is a child of a stack element, it needs to be recorded as a ‘rootElement’</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-title function_">pushUnique</span>(rootElements, <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span>); <span class="hljs-title function_">setRootElementsSort</span>();</pre></div></div> </li> <li id="section-8"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-8">&#x00a7;</a> </div> <h5 id="accessibility">Accessibility</h5> <p>if (!el.getAttribute(‘role’)) el.setAttribute(‘role’, ‘img’);</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">const</span> navigation = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">createElement</span>(<span class="hljs-string">&#x27;nav&#x27;</span>); navigation.<span class="hljs-property">id</span> = <span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-variable language_">this</span>.name}</span>-navigation`</span>; navigation.<span class="hljs-property">style</span>.<span class="hljs-property">width</span> = <span class="hljs-string">&#x27;0px&#x27;</span>; navigation.<span class="hljs-property">style</span>.<span class="hljs-property">height</span> = <span class="hljs-string">&#x27;0px&#x27;</span>; navigation.<span class="hljs-property">style</span>.<span class="hljs-property">maxWidth</span> = <span class="hljs-string">&#x27;0px&#x27;</span>; navigation.<span class="hljs-property">style</span>.<span class="hljs-property">maxHeight</span> = <span class="hljs-string">&#x27;0px&#x27;</span>; navigation.<span class="hljs-property">style</span>.<span class="hljs-property">border</span> = <span class="hljs-string">&#x27;0px&#x27;</span>; navigation.<span class="hljs-property">style</span>.<span class="hljs-property">padding</span> = <span class="hljs-string">&#x27;0px&#x27;</span>; navigation.<span class="hljs-property">style</span>.<span class="hljs-property">margin</span> = <span class="hljs-string">&#x27;0px&#x27;</span>; navigation.<span class="hljs-property">style</span>.<span class="hljs-property">overflow</span> = <span class="hljs-string">&#x27;hidden&#x27;</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-property">navigation</span> = navigation; el.<span class="hljs-title function_">appendChild</span>(navigation); <span class="hljs-keyword">const</span> textHold = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">createElement</span>(<span class="hljs-string">&#x27;div&#x27;</span>); textHold.<span class="hljs-property">id</span> = <span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-variable language_">this</span>.name}</span>-text-hold`</span>; textHold.<span class="hljs-property">style</span>.<span class="hljs-property">width</span> = <span class="hljs-string">&#x27;0px&#x27;</span>; textHold.<span class="hljs-property">style</span>.<span class="hljs-property">height</span> = <span class="hljs-string">&#x27;0px&#x27;</span>; textHold.<span class="hljs-property">style</span>.<span class="hljs-property">maxWidth</span> = <span class="hljs-string">&#x27;0px&#x27;</span>; textHold.<span class="hljs-property">style</span>.<span class="hljs-property">maxHeight</span> = <span class="hljs-string">&#x27;0px&#x27;</span>; textHold.<span class="hljs-property">style</span>.<span class="hljs-property">border</span> = <span class="hljs-string">&#x27;0px&#x27;</span>; textHold.<span class="hljs-property">style</span>.<span class="hljs-property">padding</span> = <span class="hljs-string">&#x27;0px&#x27;</span>; textHold.<span class="hljs-property">style</span>.<span class="hljs-property">margin</span> = <span class="hljs-string">&#x27;0px&#x27;</span>; textHold.<span class="hljs-property">style</span>.<span class="hljs-property">overflow</span> = <span class="hljs-string">&#x27;hidden&#x27;</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-property">textHold</span> = textHold; el.<span class="hljs-title function_">appendChild</span>(textHold); <span class="hljs-keyword">const</span> ariaLabel = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">createElement</span>(<span class="hljs-string">&#x27;div&#x27;</span>); ariaLabel.<span class="hljs-property">id</span> = <span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-variable language_">this</span>.name}</span>-ARIA-label`</span>; ariaLabel.<span class="hljs-property">textContent</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">label</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-property">ariaLabelElement</span> = ariaLabel; scrawlCanvasHold.<span class="hljs-title function_">appendChild</span>(ariaLabel); el.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;aria-labelledby&#x27;</span>, ariaLabel.<span class="hljs-property">id</span>); el.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;aria-live&#x27;</span>, <span class="hljs-string">&#x27;polite&#x27;</span>); <span class="hljs-keyword">const</span> ariaDescription = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">createElement</span>(<span class="hljs-string">&#x27;div&#x27;</span>); ariaDescription.<span class="hljs-property">id</span> = <span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-variable language_">this</span>.name}</span>-ARIA-description`</span>; ariaDescription.<span class="hljs-property">textContent</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">description</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-property">ariaDescriptionElement</span> = ariaDescription; scrawlCanvasHold.<span class="hljs-title function_">appendChild</span>(ariaDescription); el.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;aria-describedby&#x27;</span>, ariaDescription.<span class="hljs-property">id</span>); el.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;aria-live&#x27;</span>, <span class="hljs-string">&#x27;polite&#x27;</span>); <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">cleanAria</span>(); } <span class="hljs-variable language_">this</span>.<span class="hljs-property">dirtyCells</span> = <span class="hljs-literal">true</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">apply</span>(); <span class="hljs-variable language_">this</span>.<span class="hljs-property">dirtyDomDimensions</span> = <span class="hljs-literal">true</span>; <span class="hljs-keyword">if</span> (items.<span class="hljs-property">setAsCurrentCanvas</span>) <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">setAsCurrentCanvas</span>(); <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; };</pre></div></div> </li> <li id="section-9"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-9">&#x00a7;</a> </div> <h4 id="canvas-prototype">Canvas prototype</h4> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">let</span> P = <span class="hljs-title class_">Canvas</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span> = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">create</span>(<span class="hljs-title class_">Object</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>); P.<span class="hljs-property">type</span> = <span class="hljs-string">&#x27;Canvas&#x27;</span>; P.<span class="hljs-property">lib</span> = <span class="hljs-string">&#x27;canvas&#x27;</span>; P.<span class="hljs-property">isArtefact</span> = <span class="hljs-literal">true</span>; P.<span class="hljs-property">isAsset</span> = <span class="hljs-literal">false</span>;</pre></div></div> </li> <li id="section-10"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-10">&#x00a7;</a> </div> <h4 id="mixins">Mixins</h4> <ul> <li><a href="../mixin/base.html">base</a></li> <li><a href="../mixin/dom.html">dom</a></li> <li><a href="../mixin/displayShape.html">display</a></li> </ul> </div> <div class="content"><div class='highlight'><pre>P = <span class="hljs-title function_">baseMix</span>(P); P = <span class="hljs-title function_">domMix</span>(P); P = <span class="hljs-title function_">displayMix</span>(P);</pre></div></div> </li> <li id="section-11"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-11">&#x00a7;</a> </div> <h4 id="canvas-attributes">Canvas attributes</h4> <ul> <li>Attributes defined in the <a href="../mixin/base.html">base mixin</a>: <strong>name</strong>.</li> <li>Attributes defined in the <a href="../mixin/position.html">position mixin</a>: <strong>group, visibility, order, start, <em>startX</em>, <em>startY</em>, handle, <em>handleX</em>, <em>handleY</em>, offset, <em>offsetX</em>, <em>offsetY</em>, dimensions, <em>width</em>, <em>height</em>, pivoted, mimicked, lockTo, <em>lockXTo</em>, <em>lockYTo</em>, scale, roll, noUserInteraction, noPositionDependencies, noCanvasEngineUpdates, noFilters, noPathUpdates, purge, bringToFrontOnDrag</strong>.</li> <li>Attributes defined in the <a href="../mixin/delta.html">delta mixin</a>: <strong>delta, noDeltaUpdates</strong>.</li> <li>Attributes defined in the <a href="../mixin/pivot.html">pivot mixin</a>: <strong>pivot, pivotCorner, addPivotHandle, addPivotOffset, addPivotRotation</strong>.</li> <li>Attributes defined in the <a href="../mixin/mimic.html">mimic mixin</a>: <strong>mimic, useMimicDimensions, useMimicScale, useMimicStart, useMimicHandle, useMimicOffset, useMimicRotation, useMimicFlip, addOwnDimensionsToMimic, addOwnScaleToMimic, addOwnStartToMimic, addOwnHandleToMimic, addOwnOffsetToMimic, addOwnRotationToMimic</strong>.</li> <li>Attributes defined in the <a href="../mixin/path.html">path mixin</a>: <strong>path, pathPosition, addPathHandle, addPathOffset, addPathRotation, constantPathSpeed</strong>.</li> <li>Attributes defined in the <a href="../mixin/anchor.html">anchor mixin</a>: <strong>anchor</strong>.</li> <li>Attributes defined in the <a href="../mixin/dom.html">dom mixin</a>: <strong>domElement, pitch, yaw, offsetZ, css, classes, position, actionResize, trackHere, domAttributes</strong>.</li> <li>Attributes defined in the <a href="../mixin/displayShape.html">display mixin</a>: <strong>breakToBanner, breakToLandscape, breakToPortrait, breakToSkyscraper, actionBannerShape, actionLandscapeShape, actionRectangleShape, actionPortraitShape, actionSkyscraperShape</strong>.</li> </ul> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">let</span> defaultAttributes = {</pre></div></div> </li> <li id="section-12"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-12">&#x00a7;</a> </div> <p><strong>position</strong> - the CSS position value for the &lt;canvas&gt; element. This value will be set to <code>absolute</code> when the element is an artefact associated with a Stack; <code>relative</code> in other cases.</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">position</span>: <span class="hljs-string">&#x27;relative&#x27;</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><strong>trackHere</strong></p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">trackHere</span>: <span class="hljs-string">&#x27;subscribe&#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><strong>fit</strong> - String indicating how the base Cell should copy its contents over to the &lt;canvas&gt; element as the final step in the Display cycle. Accepted values are: <code>fill</code>, <code>contain</code>, <code>cover</code>, <code>none</code> (but not <code>scale-down</code>).</p> <p>The aim of this functionality is to replicate the CSS <code>object-fit</code> property - <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit">detailed here</a> - for &lt;canvas&gt; elements. We apply the fit attribute to the Canvas wrapper, not the element itself or its parent element.</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">fit</span>: <span class="hljs-string">&#x27;none&#x27;</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>DEPRECATED (because it is a really bad name) <strong>isComponent</strong> replaced by <strong>baseMatchesCanvasDimensions</strong> - set to true if, for example, canvas is being used as part of a Scrawl-canvas snippet.</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">isComponent</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">baseMatchesCanvasDimensions</span>: <span class="hljs-literal">false</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><strong>renderOnResize</strong> - perform an additional render of the scene following a change in the canvas’s dimensions</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">renderOnResize</span>: <span class="hljs-literal">true</span>,</pre></div></div> </li> <li id="section-17"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-17">&#x00a7;</a> </div> <p>For <em><strong>Device Pixel Ratio</strong></em>, we need to tell the canvas whether it should ignore resetting the canvas dimensions - if, for instance, we set them to ‘100%’ in CSS - and whether we have set the base to static dimensions (via <code>setBase</code>)</p> <ul> <li>We only need to worry about this after we invoke <code>scrawl.setIgnorePixelRatio(false)</code> in the demo code</li> </ul> <p><strong>ignoreCanvasCssDimensions</strong> - skip setting the &lt;canvas&gt; element’s CSS width and height when dimensions change. When the flag is set to <code>false</code> (default), we need to set the CSS values to a valuepx String which will then allow us to set the canvas drawing dimensions to take into account the display’s device pixel ratio value. Set the flag to <code>true</code> if the canvas has been initialized as responsive by setting its CSS dimensions values to a value% String. </p> <ul> <li>Note that Scrawl-canvas ignores any attempt to set CSS dimension (width, height) values in the <code>css</code> attribute of the <code>set({})</code> function.</li> </ul> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">ignoreCanvasCssDimensions</span>: <span class="hljs-literal">false</span>,</pre></div></div> </li> <li id="section-18"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-18">&#x00a7;</a> </div> <h5 id="accessibility-attributes">Accessibility attributes</h5> <p>&lt;canvas&gt; elements are <strong>raster images</strong> - they contain no information within their content (beyond pixel data) which can be analyzed or passed on to the browser or other device. The element <em>can</em> include <code>title</code> and various <code>item</code> attributes (alongside custom <code>data-</code> attributes) but inclusion of these depends entirely on the developer remembering to include them when coding up a web page.</p> <p>Scrawl-canvas attempts to automate <em>some</em> (but not <em>all</em>) accessibility work through inclusion of the following Canvas att