UNPKG

scrawl-canvas

Version:
1,011 lines (638 loc) 92.8 kB
<!DOCTYPE html> <html> <head> <title>Core utility functions</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="core-utility-functions">Core utility functions</h1> <p>A ragtag collection of helper functions which other modules can import and use</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> * <span class="hljs-keyword">as</span> library <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;./library.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="functions">Functions</h4> </div> </li> <li id="section-4"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-4">&#x00a7;</a> </div> <p><strong>addStrings</strong> adds the two arguments together and returns a percentage string value if either of the values was a string; or a sum of the two numbers.</p> <p>Examples:</p> <pre><code><span class="hljs-title function_">addStrings</span>(<span class="hljs-number">20</span>, <span class="hljs-number">40</span>); -&gt; <span class="hljs-number">60</span> <span class="hljs-title function_">addStrings</span>(<span class="hljs-string">&#x27;20%&#x27;</span>, <span class="hljs-number">40</span>); -&gt; <span class="hljs-string">&#x27;60%&#x27;</span> <span class="hljs-title function_">addStrings</span>(<span class="hljs-number">20</span>, <span class="hljs-string">&#x27;40%&#x27;</span>); -&gt; <span class="hljs-string">&#x27;60%&#x27;</span> <span class="hljs-title function_">addStrings</span>(<span class="hljs-string">&#x27;20%&#x27;</span>, <span class="hljs-string">&#x27;40%&#x27;</span>); -&gt; <span class="hljs-string">&#x27;60%&#x27;</span> <span class="hljs-title function_">addStrings</span>(<span class="hljs-string">&#x27;center&#x27;</span>, <span class="hljs-number">3</span>); -&gt; <span class="hljs-string">&#x27;53%&#x27;</span> </code></pre> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">const</span> <span class="hljs-title function_">addStrings</span> = (<span class="hljs-params">current, delta</span>) =&gt; { <span class="hljs-keyword">if</span> (!<span class="hljs-title function_">xt</span>(delta)) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">`core/utilities addStrings() error - no delta argument supplied <span class="hljs-subst">${current}</span>, <span class="hljs-subst">${delta}</span>`</span>); <span class="hljs-keyword">if</span> ((delta != <span class="hljs-literal">null</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>Correct for labels</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (<span class="hljs-string">&#x27;left&#x27;</span> === current || <span class="hljs-string">&#x27;top&#x27;</span> === current) current = <span class="hljs-string">&#x27;0%&#x27;</span>; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-string">&#x27;right&#x27;</span> === current || <span class="hljs-string">&#x27;bottom&#x27;</span> === current) current = <span class="hljs-string">&#x27;100%&#x27;</span>; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-string">&#x27;center&#x27;</span> === current) current = <span class="hljs-string">&#x27;50%&#x27;</span>; <span class="hljs-keyword">let</span> stringFlag = (current.<span class="hljs-property">substring</span> || delta.<span class="hljs-property">substring</span>) ? <span class="hljs-literal">true</span> : <span class="hljs-literal">false</span>; <span class="hljs-keyword">if</span> (<span class="hljs-title function_">isa_number</span>(current)) current += (<span class="hljs-title function_">isa_number</span>(delta) ? delta : <span class="hljs-built_in">parseFloat</span>(delta)); <span class="hljs-keyword">else</span> current = <span class="hljs-built_in">parseFloat</span>(current) + (<span class="hljs-title function_">isa_number</span>(delta) ? delta : <span class="hljs-built_in">parseFloat</span>(delta)); <span class="hljs-keyword">return</span> (stringFlag) ? current + <span class="hljs-string">&#x27;%&#x27;</span> : current; } <span class="hljs-keyword">return</span> current; };</pre></div></div> </li> <li id="section-6"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-6">&#x00a7;</a> </div> <p><strong>convertTime</strong> converts a time value into its component string suffix and (millisecond) number value properties; returns an array</p> <p>Examples:</p> <pre><code><span class="hljs-title function_">convertTime</span>(<span class="hljs-number">5000</span>); -&gt; [<span class="hljs-string">&#x27;ms&#x27;</span>, <span class="hljs-number">5000</span>] <span class="hljs-title function_">convertTime</span>(<span class="hljs-string">&#x27;50%&#x27;</span>); -&gt; [<span class="hljs-string">&#x27;%&#x27;</span>, <span class="hljs-number">50</span>] <span class="hljs-title function_">convertTime</span>(<span class="hljs-string">&#x27;5000ms&#x27;</span>); -&gt; [<span class="hljs-string">&#x27;ms&#x27;</span>, <span class="hljs-number">5000</span>] <span class="hljs-title function_">convertTime</span>(<span class="hljs-string">&#x27;5s&#x27;</span>); -&gt; [<span class="hljs-string">&#x27;ms&#x27;</span>, <span class="hljs-number">5000</span>] </code></pre> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> <span class="hljs-title function_">convertTime</span> = (<span class="hljs-params">item</span>) =&gt; { <span class="hljs-keyword">let</span> a, timeUnit, timeValue; <span class="hljs-keyword">if</span> (!<span class="hljs-title function_">xt</span>(item)) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">`core/utilities convertTime() error - no argument supplied`</span>); <span class="hljs-keyword">if</span> (<span class="hljs-title function_">isa_number</span>(item)) <span class="hljs-keyword">return</span> [<span class="hljs-string">&#x27;ms&#x27;</span>, item]; <span class="hljs-keyword">if</span> (!item.<span class="hljs-property">substring</span>) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">`core/utilities convertTime() error - invalid argument: <span class="hljs-subst">${item}</span>`</span>); a = item.<span class="hljs-title function_">match</span>(<span class="hljs-regexp">/^\d+\.?\d*(\D*)/</span>); timeUnit = (a[<span class="hljs-number">1</span>].<span class="hljs-property">toLowerCase</span>) ? a[<span class="hljs-number">1</span>].<span class="hljs-title function_">toLowerCase</span>() : <span class="hljs-string">&#x27;ms&#x27;</span>; timeValue = <span class="hljs-built_in">parseFloat</span>(item); <span class="hljs-keyword">if</span> (!<span class="hljs-title function_">isa_number</span>(timeValue)) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">`core/base error - convertTime() argument converts to NaN: <span class="hljs-subst">${item}</span>`</span>); <span class="hljs-keyword">switch</span> (timeUnit) { <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;s&#x27;</span>: timeValue *= <span class="hljs-number">1000</span>; <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;%&#x27;</span>: <span class="hljs-keyword">break</span>; <span class="hljs-attr">default</span>: timeUnit = <span class="hljs-string">&#x27;ms&#x27;</span>; } <span class="hljs-keyword">return</span> [timeUnit, timeValue]; };</pre></div></div> </li> <li id="section-7"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-7">&#x00a7;</a> </div> <p><strong>correctAngle</strong> makes sure any degree-based angle is in the range <code>0-360</code></p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> <span class="hljs-title function_">correctAngle</span> = (<span class="hljs-params">item</span>) =&gt; { <span class="hljs-keyword">if</span> (!item.<span class="hljs-property">toFixed</span> || <span class="hljs-built_in">isNaN</span>(item)) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${item}</span> is not a valid number`</span>); <span class="hljs-keyword">return</span> item; } item = item % <span class="hljs-number">360</span>; <span class="hljs-keyword">if</span> (item &lt; <span class="hljs-number">0</span>) { item += <span class="hljs-number">360</span>; } <span class="hljs-keyword">return</span> item; };</pre></div></div> </li> <li id="section-8"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-8">&#x00a7;</a> </div> <p><strong>correctForZero</strong> checks and corrects for minor deviations from zero (eNumbers)</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> <span class="hljs-title function_">correctForZero</span> = (<span class="hljs-params">item</span>) =&gt; { <span class="hljs-keyword">if</span> (!item.<span class="hljs-property">toFixed</span>) <span class="hljs-keyword">return</span> item; <span class="hljs-keyword">if</span> (item == <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span> item; <span class="hljs-keyword">if</span> (<span class="hljs-built_in">isNaN</span>(item)) <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; <span class="hljs-keyword">if</span> (item &lt; -<span class="hljs-number">0.000001</span>) <span class="hljs-keyword">return</span> item; <span class="hljs-keyword">if</span> (item &gt; <span class="hljs-number">0.000001</span>) <span class="hljs-keyword">return</span> item; <span class="hljs-keyword">return</span> <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><strong>λ functions</strong> helps us avoid errors when invoking a function attribute settable by the coder</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> λ<span class="hljs-literal">null</span> = <span class="hljs-function">() =&gt;</span> {}; <span class="hljs-keyword">const</span> λfirstArg = <span class="hljs-keyword">function</span> (<span class="hljs-params">a</span>) { <span class="hljs-keyword">return</span> a; }; <span class="hljs-keyword">const</span> λ<span class="hljs-variable language_">this</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) { <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; }; <span class="hljs-keyword">const</span> λpromise = <span class="hljs-function">() =&gt;</span> <span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</span>(<span class="hljs-literal">true</span>); <span class="hljs-keyword">const</span> Ωempty = {};</pre></div></div> </li> <li id="section-10"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-10">&#x00a7;</a> </div> <p><strong>generateUuid</strong> is a simple (crude) uuid generator <a href="http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript">http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript</a> (imported 2017-07-08)</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> <span class="hljs-title function_">generateUuid</span> = (<span class="hljs-params"></span>) =&gt; { <span class="hljs-keyword">function</span> <span class="hljs-title function_">s4</span>(<span class="hljs-params"></span>) { <span class="hljs-keyword">return</span> <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">floor</span>((<span class="hljs-number">1</span> + <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">random</span>()) * <span class="hljs-number">0x10000</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>).<span class="hljs-title function_">substring</span>(<span class="hljs-number">1</span>); } <span class="hljs-keyword">return</span> <span class="hljs-string">`<span class="hljs-subst">${s4()}</span><span class="hljs-subst">${s4()}</span>-<span class="hljs-subst">${s4()}</span>-<span class="hljs-subst">${s4()}</span>-<span class="hljs-subst">${s4()}</span>-<span class="hljs-subst">${s4()}</span><span class="hljs-subst">${s4()}</span><span class="hljs-subst">${s4()}</span>`</span>; };</pre></div></div> </li> <li id="section-11"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-11">&#x00a7;</a> </div> <p><strong>generateUniqueString</strong> is a simple random String generator <a href="https://gist.github.com/SimonHoiberg/ad2710c8626c5a74cddd8f6385795cc0">https://gist.github.com/SimonHoiberg/ad2710c8626c5a74cddd8f6385795cc0</a> (imported 2020-11-22)</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> <span class="hljs-title function_">generateUniqueString</span> = (<span class="hljs-params"></span>) =&gt; { <span class="hljs-keyword">return</span> performance.<span class="hljs-title function_">now</span>().<span class="hljs-title function_">toString</span>(<span class="hljs-number">36</span>) + <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">random</span>().<span class="hljs-title function_">toString</span>(<span class="hljs-number">36</span>).<span class="hljs-title function_">substr</span>(<span class="hljs-number">2</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><strong>interpolate</strong> clamp a value between a maximum and minimum value</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> interpolate = <span class="hljs-keyword">function</span> (<span class="hljs-params">val, min, max</span>) { <span class="hljs-keyword">return</span> min + val * (max - min); };</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>isa_boolean</strong> checks to make sure the argument is a boolean</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> <span class="hljs-title function_">isa_boolean</span> = item =&gt; (<span class="hljs-keyword">typeof</span> item === <span class="hljs-string">&#x27;boolean&#x27;</span>) ? <span class="hljs-literal">true</span> : <span class="hljs-literal">false</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>isa_canvas</strong> checks to make sure the argument is a DOM &lt;canvas&gt; element</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> <span class="hljs-title function_">isa_canvas</span> = item =&gt; (<span class="hljs-title class_">Object</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">toString</span>.<span class="hljs-title function_">call</span>(item) === <span class="hljs-string">&#x27;[object HTMLCanvasElement]&#x27;</span>) ? <span class="hljs-literal">true</span> : <span class="hljs-literal">false</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><strong>isa_dom</strong> checks to make sure the argument is a DOM element of some sort</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> <span class="hljs-title function_">isa_dom</span> = item =&gt; (item &amp;&amp; item.<span class="hljs-property">querySelector</span> &amp;&amp; item.<span class="hljs-property">dispatchEvent</span>) ? <span class="hljs-literal">true</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>isa_fn</strong> checks to make sure the argument is a JavaScript function object</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> <span class="hljs-title function_">isa_fn</span> = item =&gt; (<span class="hljs-keyword">typeof</span> item === <span class="hljs-string">&#x27;function&#x27;</span>) ? <span class="hljs-literal">true</span> : <span class="hljs-literal">false</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><strong>isa_number</strong> checks to make sure the argument is true number (excluding NaN)</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> <span class="hljs-title function_">isa_number</span> = item =&gt; (item != <span class="hljs-literal">null</span> &amp;&amp; item.<span class="hljs-property">toFixed</span> &amp;&amp; !<span class="hljs-title class_">Number</span>.<span class="hljs-built_in">isNaN</span>(item)) ? <span class="hljs-literal">true</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> <p><strong>isa_obj</strong> checks to make sure the argument is a JavaScript Object</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> <span class="hljs-title function_">isa_obj</span> = item =&gt; (<span class="hljs-title class_">Object</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">toString</span>.<span class="hljs-title function_">call</span>(item) === <span class="hljs-string">&#x27;[object Object]&#x27;</span>) ? <span class="hljs-literal">true</span> : <span class="hljs-literal">false</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><strong>isa_quaternion</strong> checks to make sure the argument is a Scrawl-canvas Quaternion object</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> <span class="hljs-title function_">isa_quaternion</span> = item =&gt; (item &amp;&amp; item.<span class="hljs-property">type</span> &amp;&amp; item.<span class="hljs-property">type</span> === <span class="hljs-string">&#x27;Quaternion&#x27;</span>) ? <span class="hljs-literal">true</span> : <span class="hljs-literal">false</span>;</pre></div></div> </li> <li id="section-20"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-20">&#x00a7;</a> </div> <p><strong>mergeInto</strong> takes two objects and merges the attributes of one into the other. This function mutates the ‘original’ object rather than generating a third, new onject</p> <p>Example:</p> <pre><code><span class="hljs-keyword">var</span> original = { <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">42</span>, <span class="hljs-attr">job</span>: <span class="hljs-string">&#x27;lawyer&#x27;</span> }; <span class="hljs-keyword">var</span> additional = { <span class="hljs-attr">age</span>: <span class="hljs-number">32</span>, <span class="hljs-attr">job</span>: <span class="hljs-string">&#x27;coder&#x27;</span>, <span class="hljs-attr">pet</span>: <span class="hljs-string">&#x27;cat&#x27;</span> }; scrawl.<span class="hljs-property">utils</span>.<span class="hljs-title function_">mergeInto</span>(original, additional); -&gt; { <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">42</span>, <span class="hljs-attr">job</span>: <span class="hljs-string">&#x27;lawyer&#x27;</span>, <span class="hljs-attr">pet</span>: <span class="hljs-string">&#x27;cat&#x27;</span> } </code></pre> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> <span class="hljs-title function_">mergeInto</span> = (<span class="hljs-params">original, additional</span>) =&gt; { <span class="hljs-keyword">if</span> (!<span class="hljs-title function_">isa_obj</span>(original) || !<span class="hljs-title function_">isa_obj</span>(additional)) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">`core/utilities mergeInto() error - insufficient arguments supplied <span class="hljs-subst">${original}</span>, <span class="hljs-subst">${additional}</span>`</span>); <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> key <span class="hljs-keyword">in</span> additional) { <span class="hljs-keyword">if</span> (additional.<span class="hljs-title function_">hasOwnProperty</span>(key) &amp;&amp; <span class="hljs-keyword">typeof</span> original[key] == <span class="hljs-string">&#x27;undefined&#x27;</span>) original[key] = additional[key]; } <span class="hljs-keyword">return</span> original; };</pre></div></div> </li> <li id="section-21"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-21">&#x00a7;</a> </div> <p><strong>mergeOver</strong> takes two objects and writes the attributes of one over the other. This function mutates the ‘original’ object rather than generating a third, new onject</p> <p>Example:</p> <pre><code><span class="hljs-keyword">var</span> original = { <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">42</span>, <span class="hljs-attr">job</span>: <span class="hljs-string">&#x27;lawyer&#x27;</span> }; <span class="hljs-keyword">var</span> additional = { <span class="hljs-attr">age</span>: <span class="hljs-number">32</span>, <span class="hljs-attr">job</span>: <span class="hljs-string">&#x27;coder&#x27;</span>, <span class="hljs-attr">pet</span>: <span class="hljs-string">&#x27;cat&#x27;</span> }; scrawl.<span class="hljs-property">utils</span>.<span class="hljs-title function_">mergeOver</span>(original, additional); -&gt; { <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">32</span>, <span class="hljs-attr">job</span>: <span class="hljs-string">&#x27;coder&#x27;</span>, <span class="hljs-attr">pet</span>: <span class="hljs-string">&#x27;cat&#x27;</span> } </code></pre> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> <span class="hljs-title function_">mergeOver</span> = (<span class="hljs-params">original, additional</span>) =&gt; { <span class="hljs-keyword">if</span> (!<span class="hljs-title function_">isa_obj</span>(original) || !<span class="hljs-title function_">isa_obj</span>(additional)) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">`core/utilities mergeOver() error - insufficient arguments supplied <span class="hljs-subst">${original}</span>, <span class="hljs-subst">${additional}</span>`</span>); <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> key <span class="hljs-keyword">in</span> additional) { <span class="hljs-keyword">if</span> (additional.<span class="hljs-title function_">hasOwnProperty</span>(key)) original[key] = additional[key]; } <span class="hljs-keyword">return</span> original; };</pre></div></div> </li> <li id="section-22"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-22">&#x00a7;</a> </div> <p><strong>mergeDiscard</strong> iterates over the additional object to perform a mergeOver operation, and also removing attributes from the original object where they have been set to null in the additional object</p> <p>Example:</p> <pre><code><span class="hljs-keyword">var</span> original = { <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">42</span>, <span class="hljs-attr">job</span>: <span class="hljs-string">&#x27;lawyer&#x27;</span> }; <span class="hljs-keyword">var</span> additional = { <span class="hljs-attr">age</span>: <span class="hljs-number">32</span>, <span class="hljs-attr">job</span>: <span class="hljs-literal">null</span>, <span class="hljs-attr">pet</span>: <span class="hljs-string">&#x27;cat&#x27;</span> }; scrawl.<span class="hljs-property">utils</span>.<span class="hljs-title function_">mergeOver</span>(original, additional); -&gt; { <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">32</span>, <span class="hljs-attr">pet</span>: <span class="hljs-string">&#x27;cat&#x27;</span> } </code></pre> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> <span class="hljs-title function_">mergeDiscard</span> = (<span class="hljs-params">original, additional</span>) =&gt; { <span class="hljs-keyword">if</span> (!<span class="hljs-title function_">isa_obj</span>(original) || !<span class="hljs-title function_">isa_obj</span>(additional)) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">`core/utilities mergeDiscard() error - insufficient arguments supplied <span class="hljs-subst">${original}</span>, <span class="hljs-subst">${additional}</span>`</span>); <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">entries</span>(additional).<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">[key, val]</span>) =&gt;</span> { <span class="hljs-keyword">if</span> (val === <span class="hljs-literal">null</span>) <span class="hljs-keyword">delete</span> original[key]; <span class="hljs-keyword">else</span> original[key] = additional[key]; }); <span class="hljs-keyword">return</span> original; };</pre></div></div> </li> <li id="section-23"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-23">&#x00a7;</a> </div> <p><strong>pushUnique</strong> adds a value to the end of an array, if that value is not already present in the array. This function mutates the array</p> <p>Example:</p> <pre><code><span class="hljs-keyword">var</span> myarray = [<span class="hljs-string">&#x27;apple&#x27;</span>, <span class="hljs-string">&#x27;orange&#x27;</span>]; scrawl.<span class="hljs-property">utils</span>.<span class="hljs-title function_">pushUnique</span>(myarray, <span class="hljs-string">&#x27;apple&#x27;</span>); -&gt; [<span class="hljs-string">&#x27;apple&#x27;</span>, <span class="hljs-string">&#x27;orange&#x27;</span>] scrawl.<span class="hljs-property">utils</span>.<span class="hljs-title function_">pushUnique</span>(myarray, <span class="hljs-string">&#x27;banana&#x27;</span>); -&gt; [<span class="hljs-string">&#x27;apple&#x27;</span>, <span class="hljs-string">&#x27;orange&#x27;</span>, <span class="hljs-string">&#x27;banana&#x27;</span>] </code></pre> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> <span class="hljs-title function_">pushUnique</span> = (<span class="hljs-params">myArray, potentialMember</span>) =&gt; { <span class="hljs-keyword">if</span> (!<span class="hljs-title function_">xta</span>(myArray, potentialMember)) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">`core/utilities pushUnique() error - insufficient arguments supplied <span class="hljs-subst">${myArray}</span>, <span class="hljs-subst">${potentialMember}</span>`</span>); <span class="hljs-keyword">if</span> (!<span class="hljs-title class_">Array</span>.<span class="hljs-title function_">isArray</span>(myArray)) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">`core/utilities pushUnique() error - argument not an array <span class="hljs-subst">${myArray}</span>`</span>); <span class="hljs-keyword">if</span> (<span class="hljs-title class_">Array</span>.<span class="hljs-title function_">isArray</span>(potentialMember)) { potentialMember.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> <span class="hljs-title function_">pushUnique</span>(myArray, item)); } <span class