UNPKG

scrawl-canvas

Version:
1,014 lines (662 loc) 44.9 kB
<!DOCTYPE html> <html> <head> <title>Delta mixin</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="../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="anchor.html"> ./source/mixin/anchor.js </a> <a class="source" href="asset.html"> ./source/mixin/asset.js </a> <a class="source" href="assetAdvancedFunctionality.html"> ./source/mixin/assetAdvancedFunctionality.js </a> <a class="source" href="assetConsumer.html"> ./source/mixin/assetConsumer.js </a> <a class="source" href="base.html"> ./source/mixin/base.js </a> <a class="source" href="cascade.html"> ./source/mixin/cascade.js </a> <a class="source" href="delta.html"> ./source/mixin/delta.js </a> <a class="source" href="displayShape.html"> ./source/mixin/displayShape.js </a> <a class="source" href="dom.html"> ./source/mixin/dom.js </a> <a class="source" href="entity.html"> ./source/mixin/entity.js </a> <a class="source" href="filter.html"> ./source/mixin/filter.js </a> <a class="source" href="mimic.html"> ./source/mixin/mimic.js </a> <a class="source" href="path.html"> ./source/mixin/path.js </a> <a class="source" href="pattern.html"> ./source/mixin/pattern.js </a> <a class="source" href="pivot.html"> ./source/mixin/pivot.js </a> <a class="source" href="position.html"> ./source/mixin/position.js </a> <a class="source" href="shapeBasic.html"> ./source/mixin/shapeBasic.js </a> <a class="source" href="shapeCurve.html"> ./source/mixin/shapeCurve.js </a> <a class="source" href="shapePathCalculation.html"> ./source/mixin/shapePathCalculation.js </a> <a class="source" href="styles.html"> ./source/mixin/styles.js </a> <a class="source" href="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="delta-mixin">Delta mixin</h1> <p>This mixin defines additional attributes and functions for an artefact that uses delta functionality</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> { mergeOver, mergeDiscard, xt, Ωempty } <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../core/utilities.js&#x27;</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="export-function">Export function</h4> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-keyword">function</span> (<span class="hljs-params">P = Ωempty</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="shared-attributes">Shared attributes</h4> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">let</span> defaultAttributes = {</pre></div></div> </li> <li id="section-5"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-5">&#x00a7;</a> </div> <p><strong>delta</strong> - a Javascript object containing <code>{key:value, key:value, etc}</code> attributes. </p> <ul> <li>As part of the Display cycle, delta values get added to artefact attribute values - this is a very simple form of animation.</li> <li><strong>deltaConstraints</strong> - Object mirroring the <code>delta</code> object which we use to set bounds on the delta values, and instructions on what to do when those bounds are crossed.</li> <li><strong>noDeltaUpdates</strong> - Boolean flag to switch off the automatic application of delta attribute values as part of each iteration of the Display cycle.</li> <li><strong>checkDeltaConstraints</strong> - Boolean flag to switch ON automatic constraint checks.</li> <li>Delta updates can be invoked independently from the Display cycle by invoking <code>artefact.updateByDelta</code>, <code>artefact.reverseByDelta</code>.</li> <li>In addition to using <code>artefact.set</code>, we can also update the delta object values using <code>artefact.setDeltaValues</code>.</li> </ul> <pre><code><span class="hljs-comment">// This Block artefact will animate itself across the &lt;canvas&gt; element</span> <span class="hljs-comment">// - it will move to the right and upwards until the `delta` values are updated</span> <span class="hljs-comment">// - animation will stop when the `noDeltaUpdates` flag is set</span> <span class="hljs-keyword">let</span> myBlock = scrawl.<span class="hljs-title function_">makeBlock</span>({ <span class="hljs-attr">start</span>: [<span class="hljs-string">&#x27;left&#x27;</span>, <span class="hljs-number">500</span>], <span class="hljs-attr">delta</span>: { <span class="hljs-attr">startX</span>: <span class="hljs-number">0.5</span>, <span class="hljs-attr">startY</span>: <span class="hljs-string">&#x27;-0.3&#x27;</span>, }, <span class="hljs-attr">noDeltaUpdates</span>: <span class="hljs-literal">false</span>, }); </code></pre> <p>Note that the <code>delta</code> and <code>deltaConstraints</code> objects are set up in the mixin/position.js module, not here</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">delta</span>: <span class="hljs-literal">null</span>, <span class="hljs-attr">noDeltaUpdates</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">deltaConstraints</span>: <span class="hljs-literal">null</span>, <span class="hljs-attr">checkDeltaConstraints</span>: <span class="hljs-literal">false</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>We do not want to perform any delta checks on any uninitalized attributes - in other words: no checks on the first iteration of the Display cycle</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">performDeltaChecks</span>: <span class="hljs-literal">false</span>, }; P.<span class="hljs-property">defs</span> = <span class="hljs-title function_">mergeOver</span>(P.<span class="hljs-property">defs</span>, defaultAttributes); <span class="hljs-title function_">mergeOver</span>(P, defaultAttributes);</pre></div></div> </li> <li id="section-7"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-7">&#x00a7;</a> </div> <h4 id="packet-management">Packet management</h4> <p>No additional packet management functionality defined here</p> </div> </li> <li id="section-8"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-8">&#x00a7;</a> </div> <h4 id="clone-management">Clone management</h4> <p>No additional clone functionality defined here</p> </div> </li> <li id="section-9"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-9">&#x00a7;</a> </div> <h4 id="kill-management">Kill management</h4> <p>No additional kill functionality defined here</p> </div> </li> <li id="section-10"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-10">&#x00a7;</a> </div> <h4 id="get-set-deltaset">Get, Set, deltaSet</h4> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">let</span> S = P.<span class="hljs-property">setters</span>, D = P.<span class="hljs-property">deltaSetters</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>delta</strong></p> </div> <div class="content"><div class='highlight'><pre> S.<span class="hljs-property">delta</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">items = Ωempty</span>) { <span class="hljs-keyword">if</span> (items) <span class="hljs-variable language_">this</span>.<span class="hljs-property">delta</span> = <span class="hljs-title function_">mergeDiscard</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">delta</span>, items); }; S.<span class="hljs-property">deltaConstraints</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">items = Ωempty</span>) { <span class="hljs-keyword">if</span> (items) <span class="hljs-variable language_">this</span>.<span class="hljs-property">deltaConstraints</span> = <span class="hljs-title function_">mergeDiscard</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">deltaConstraints</span>, items); };</pre></div></div> </li> <li id="section-12"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-12">&#x00a7;</a> </div> <h4 id="prototype-functions">Prototype functions</h4> </div> </li> <li id="section-13"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-13">&#x00a7;</a> </div> <p><code>updateByDelta</code> - this function gets called as part of every display cycle iteration, meaning that if an attribute is set to a non-zero value in the <strong>delta</strong> attribute object then those <strong>delta animations</strong> will start playing immediately.</p> </div> <div class="content"><div class='highlight'><pre> P.<span class="hljs-property">updateByDelta</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) { <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">setDelta</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">delta</span>); <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">checkDeltaConstraints</span>) <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">performDeltaConstraintsChecks</span>(); <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</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><code>reverseByDelta</code> - The opposite action to ‘updateByDelta’; values in the <strong>delta</strong> attribute object will be subtracted from the current value for that Scrawl-canvas object.</p> </div> <div class="content"><div class='highlight'><pre> P.<span class="hljs-property">reverseByDelta</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) { <span class="hljs-keyword">let</span> temp = {}; <span class="hljs-keyword">const</span> delta = <span class="hljs-variable language_">this</span>.<span class="hljs-property">delta</span>, deltaKeys = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">keys</span>(delta), deltaLen = deltaKeys.<span class="hljs-property">length</span>; <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>, key, val; i &lt; deltaLen; i++) { key = deltaKeys[i]; val = delta[key]; <span class="hljs-keyword">if</span> (val.<span class="hljs-property">substring</span>) val = -(<span class="hljs-built_in">parseFloat</span>(val)) + <span class="hljs-string">&#x27;%&#x27;</span>; <span class="hljs-keyword">else</span> val = -val; temp[key] = val; } <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">setDelta</span>(temp); <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">checkDeltaConstraints</span>) <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">performDeltaConstraintsChecks</span>(); <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; }; P.<span class="hljs-property">performDeltaConstraintsChecks</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) { <span class="hljs-keyword">const</span> {delta, deltaConstraints} = <span class="hljs-variable language_">this</span>; <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">performDeltaChecks</span>) { <span class="hljs-keyword">const</span> keys = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">keys</span>(deltaConstraints), keysLen = keys.<span class="hljs-property">length</span>; <span class="hljs-keyword">let</span> key, keyIndex, item, min, max, action, performAction, performActionBecause, valArray, valIndex, val, i, isString, fMin, fMax, fVal; <span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i &lt; keysLen; i++) { key = keys[i]; item = deltaConstraints[key]; <span class="hljs-keyword">if</span> (<span class="hljs-title class_">Array</span>.<span class="hljs-title function_">isArray</span>(item) &amp;&amp; item.<span class="hljs-property">length</span> === <span class="hljs-number">3</span>) { [min, max, action] = deltaConstraints[key]; isString = (min.<span class="hljs-property">substring</span>) ? <span class="hljs-literal">true</span> : <span class="hljs-literal">false</span>; <span class="hljs-keyword">if</span> (isString) { keyIndex = [<span class="hljs-string">&#x27;startX&#x27;</span>, <span class="hljs-string">&#x27;startY&#x27;</span>, <span class="hljs-string">&#x27;handleX&#x27;</span>, <span class="hljs-string">&#x27;handleY&#x27;</span>, <span class="hljs-string">&#x27;offsetX&#x27;</span>, <span class="hljs-string">&#x27;offsetY&#x27;</span>, <span class="hljs-string">&#x27;width&#x27;</span>, <span class="hljs-string">&#x27;height&#x27;</span>].<span class="hljs-title function_">indexOf</span>(key); valArray = <span class="hljs-literal">false</span>; valIndex = <span class="hljs-number">0</span>; <span class="hljs-keyword">if</span> (keyIndex &gt;= <span class="hljs-number">0</span>) { <span class="hljs-keyword">if</span> (keyIndex &lt; <span class="hljs-number">2</span>) valArray = <span class="hljs-variable language_">this</span>.<span class="hljs-property">start</span>; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (keyIndex &lt; <span class="hljs-number">4</span>) valArray = <span class="hljs-variable language_">this</span>.<span class="hljs-property">handle</span>; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (keyIndex &lt; <span class="hljs-number">6</span>) valArray = <span class="hljs-variable language_">this</span>.<span class="hljs-property">offset</span>; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (keyIndex &lt; <span class="hljs-number">8</span>) valArray = <span class="hljs-variable language_">this</span>.<span class="hljs-property">dimensions</span>; <span class="hljs-keyword">if</span> ([<span class="hljs-string">&#x27;startY&#x27;</span>, <span class="hljs-string">&#x27;handleY&#x27;</span>, <span class="hljs-string">&#x27;offsetY&#x27;</span>, <span class="hljs-string">&#x27;height&#x27;</span>].<span class="hljs-title function_">indexOf</span>(key) &gt;= <span class="hljs-number">0</span>) valIndex = <span class="hljs-number">1</span>; val = valArray[valIndex]; } <span class="hljs-keyword">else</span> val = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">get</span>(key); fMin = <span class="hljs-built_in">parseFloat</span>(min); fMax = <span class="hljs-built_in">parseFloat</span>(max); fVal = <span class="hljs-built_in">parseFloat</span>(val); performAction = <span class="hljs-string">&#x27;&#x27;</span>; <span class="hljs-keyword">if</span> (fVal &lt; fMin) { performAction = action; performActionBecause = <span class="hljs-number">0</span>; } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (fVal &gt; fMax) { performAction = action; performActionBecause = <span class="hljs-number">1</span>; } <span class="hljs-keyword">if</span> (performAction) { <span class="hljs-keyword">switch</span> (performAction) { <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;reverse&#x27;</span> : delta[key] = -<span class="hljs-built_in">parseFloat</span>(delta[key]) + <span class="hljs-string">&#x27;%&#x27;</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">set</span>({ [key]: fVal + <span class="hljs-built_in">parseFloat</span>(delta[key]) + <span class="hljs-string">&#x27;%&#x27;</span>, }); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;loop&#x27;</span> : <span class="hljs-keyword">if</span> (performActionBecause) { <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">set</span>({ [key]: fVal - (fMax - fMin) + <span class="hljs-string">&#x27;%&#x27;</span>, }); } <span class="hljs-keyword">else</span> { <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">set</span>({ [key]: fVal + (fMax - fMin) + <span class="hljs-string">&#x27;%&#x27;</span>, }); } <span class="hljs-keyword">break</span>; } } } <span class="hljs-keyword">else</span> { val = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">get</span>(key); performAction = <span class="hljs-string">&#x27;&#x27;</span>; <span class="hljs-keyword">if</span> (val &lt; min) { performAction = action; performActionBecause = <span class="hljs-number">0</span>; } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (val &gt; max) { performAction = action; performActionBecause = <span class="hljs-number">1</span>; } <span class="hljs-keyword">if</span> (performAction) { <span class="hljs-keyword">switch</span> (performAction) { <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;reverse&#x27;</span> : delta[key] = -delta[key]; <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">set</span>({ [key]: val + delta[key], }); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;loop&#x27;</span> : <span class="hljs-keyword">if</span> (performActionBecause) { <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">set</span>({ [key]: val - (max - min), }); } <span class="hljs-keyword">else</span> { <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">set</span>({ [key]: val + (max - min), }); } <span class="hljs-keyword">break</span>; } } } } } } <span class="hljs-keyword">else</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">performDeltaChecks</span> = <span class="hljs-literal">true</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><code>setDeltaValues</code> Update the artefact’s <code>delta</code> object in a more intelligent way. The function accepts an object argument containing a set of instructions which will be interpreted and applied to the delat object.</p> <pre><code><span class="hljs-comment">// Original delta object:</span> artefact.<span class="hljs-property">delta</span>: { <span class="hljs-attr">width</span>: <span class="hljs-number">50</span>, <span class="hljs-attr">height</span>: <span class="hljs-string">&#x27;20%&#x27;</span>, <span class="hljs-attr">scale</span>: <span class="hljs-number">2</span>, } <span class="hljs-comment">// Function argument object to be applied to the delta object:</span> { <span class="hljs-attr">width</span>: <span class="hljs-string">&#x27;add:20&#x27;</span>, <span class="hljs-attr">height</span>: <span class="hljs-string">&#x27;multiply:0.8&#x27;</span>, <span class="hljs-attr">scale</span>: <span class="hljs-string">&#x27;remove&#x27;</span>, <span class="hljs-attr">roll</span>: <span class="hljs-string">&#x27;newNumber:45&#x27;</span>, } <span class="hljs-comment">// Result of applying the argument to the delta object:</span> artefact.<span class="hljs-property">delta</span>: { <span class="hljs-attr">width</span>: <span class="hljs-number">70</span>, <span class="hljs-attr">height</span>: <span class="hljs-string">&#x27;16%&#x27;</span>, <span class="hljs-attr">roll</span>: <span class="hljs-number">45</span>, } </code></pre> </div> <div class="content"><div class='highlight'><pre> P.<span class="hljs-property">setDeltaValues</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">items = Ωempty</span>) { <span class="hljs-keyword">const</span> delta = <span class="hljs-variable language_">this</span>.<span class="hljs-property">delta</span>, keys = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">keys</span>(items), keysLen = keys.<span class="hljs-property">length</span>; <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>, key, item, action, val, old; i &lt; keysLen; i++) { key = keys[i]; item = items[key]; old = delta[key]; <span class="hljs-keyword">if</span> (item.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">&#x27;:&#x27;</span>) &lt; <span class="hljs-number">0</span>) { action = item; val = <span class="hljs-literal">false</span>; } <span class="hljs-keyword">else</span> { [action, val] = item.<span class="hljs-title function_">split</span>(<span class="hljs-string">&#x27;:&#x27;</span>); } <span class="hljs-keyword">switch</span> (action) { <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;newString&#x27;</span> : <span class="hljs-keyword">if</span> (val != <span class="hljs-literal">null</span>) delta[key] = val; <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;newNumber&#x27;</span> : <span class="hljs-keyword">if</span> (val != <span class="hljs-literal">null</span>) delta[key] = <span class="hljs-built_in">parseFloat</span>(val); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;remove&#x27;</span> : <span class="hljs-keyword">delete</span> delta[key]; <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;update&#x27;</span> : <span class="hljs-keyword">if</span> (val != <span class="hljs-literal">null</span>) delta[key] = (old.<span class="hljs-property">substring</span>) ? val : <span class="hljs-built_in">parseFloat</span>(val); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;reverse&#x27;</span> : <span class="hljs-keyword">if</span> (old.<span class="hljs-property">substring</span>) val = -(<span class="hljs-built_in">parseFloat</span>(old)) + <span class="hljs-string">&#x27;%&#x27;</span>; <span class="hljs-keyword">else</span> val = -old; delta[key] = val; <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;add&#x27;</span> : <span class="hljs-keyword">if</span> (old.<span class="hljs-property">substring</span>) val = (<span class="hljs-built_in">parseFloat</span>(old) + <span class="hljs-built_in">parseFloat</span>(val)) + <span class="hljs-string">&#x27;%&#x27;</span>; <span class="hljs-keyword">else</span> val += old; delta[key] = val; <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;multiply&#x27;</span> : <span class="hljs-keyword">if</span> (old.<span class="hljs-property">substring</span>) val = (<span class="hljs-built_in">parseFloat</span>(old) * <span class="hljs-built_in">parseFloat</span>(val)) + <span class="hljs-string">&#x27;%&#x27;</span>; <span class="hljs-keyword">else</span> val *= old; delta[key] = val; <span class="hljs-keyword">break</span>; } } <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</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>Return the prototype</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> P; };</pre></div></div> </li> </ul> </div> </body> </html>