UNPKG

scrawl-canvas

Version:
1,040 lines (670 loc) 127 kB
<!DOCTYPE html> <html> <head> <title>Emitter 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="emitter-factory">Emitter factory</h1> <p>The Scrawl-canvas particle physics engine is a simple system designed to allow developers a way to add particle-based effects to their canvas animation scenes. The physics engine is built on top of the following components:</p> <ul> <li><a href="./particle.html">Particle objects</a>, which represent a 3-dimensional coordinate - based on a Scrawl-canvas <a href="./vector.html">Vector object</a> - and include a history of recent positions which we can use to determine how to display that particle on screen.</li> <li><a href="./particleHistory.html">History arrays</a> which can be pooled (reused) to cut down on Array creation and distruction during the animation.</li> <li><a href="./particleForce.html">Force objects</a> which define the general and occasional forces to be applied to each particle in the system as the animation progresses - a <strong>gravity</strong> force object is pre-defined by Scrawl-canvas.</li> <li><a href="./particleSpring.html">Spring objects</a> used to define a constraint (connection) between two particles in a system.</li> <li><a href="./particleWorld.html">World objects</a> where we can store attributes and values used by various objects; these attributes can be set up so that they will be inherited by clones of the World object. We can also influence the speed of the physics animation here.</li> </ul> <p>We do not have to handle particle generation and manipulation ourselves. Instead, Scrawl-canvas gives us three dedicated <strong>entitys</strong> which we use to add particle animation effects to the canvas scene. These entitys are:</p> <ul> <li><a href="./tracer.html">Tracer</a> - this entity generates a single non-recycled (in other words: long lasting) particle with a history, which we can use to display trace effects in the animation.</li> <li><a href="./emitter.html">Emitter</a> - an entity which generates a stream of short-lived, recycled particles, each with its own history. Emitters are highly versatile entitys which can generate a wide range of effects.</li> <li><a href="./net.html">Net</a> - a (generally) larger entity which uses both forces and springs to manage the animation of its non-recycled particles. Note that other artefacts can use Net particles as a reference for their own positioning.</li> </ul> </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><a href="../../demo/particles-001.html">particles-001</a> - Emitter entity, and Particle World, basic functionality</li> <li><a href="../../demo/particles-002.html">particles-002</a> - Emitter using artefacts</li> <li><a href="../../demo/particles-003.html">particles-003</a> - Position Emitter entity: start; pivot; mimic; path; mouse; drag-and-drop</li> <li><a href="../../demo/particles-004.html">particles-004</a> - Emit particles along the length of a path</li> <li><a href="../../demo/particles-005.html">particles-005</a> - Emit particles from inside an artefact’s area</li> <li><a href="../../demo/particles-006.html">particles-006</a> - Fixed number of Particles in a field; preAction and postAction functionality</li> <li><a href="../../demo/particles-007.html">particles-007</a> - Particle Force objects: generation and functionality</li> <li><a href="../../demo/particles-012.html">particles-012</a> - Use Net entity particles as reference coordinates for other artefacts</li> <li><a href="../../demo/delaunator-001.html">delaunator-001</a> - Delauney triangulation and Voronoi cell visualisation</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> { constructors, tween, artefact, group, world } <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../core/library.js&#x27;</span>; <span class="hljs-keyword">import</span> { pushUnique, mergeOver, λ<span class="hljs-literal">null</span>, isa_fn, isa_obj, xt, xta, Ωempty } <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../core/utilities.js&#x27;</span>; <span class="hljs-keyword">import</span> { currentGroup } <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../core/document.js&#x27;</span>; <span class="hljs-keyword">import</span> { requestParticle, releaseParticle } <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./particle.js&#x27;</span>; <span class="hljs-keyword">import</span> { requestCell, releaseCell } <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./cell.js&#x27;</span>; <span class="hljs-keyword">import</span> { makeVector, requestVector, releaseVector } <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./vector.js&#x27;</span>; <span class="hljs-keyword">import</span> { requestCoordinate, releaseCoordinate } <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./coordinate.js&#x27;</span>; <span class="hljs-keyword">import</span> { makeColor } <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./color.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> entityMix <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../mixin/entity.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="emitter-constructor">Emitter constructor</h4> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> <span class="hljs-title class_">Emitter</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">items = Ωempty</span>) { <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-title function_">set</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">defs</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>The entity has a hit zone which can be used for drag-and-drop, and other user interactions. Thus the <code>onXYZ</code> UI functions remain relevant.</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-variable language_">this</span>.<span class="hljs-property">onEnter</span> = λ<span class="hljs-literal">null</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-property">onLeave</span> = λ<span class="hljs-literal">null</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-property">onDown</span> = λ<span class="hljs-literal">null</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-property">onUp</span> = λ<span class="hljs-literal">null</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>Each instantiated entity will include two color factories - one for creating random fillStyle color Strings for generated particles, the other for generating strokeStyle colors.</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-variable language_">this</span>.<span class="hljs-property">fillColorFactory</span> = <span class="hljs-title function_">makeColor</span>({ <span class="hljs-attr">name</span>: <span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-variable language_">this</span>.name}</span>-fillColorFactory`</span>}); <span class="hljs-variable language_">this</span>.<span class="hljs-property">strokeColorFactory</span> = <span class="hljs-title function_">makeColor</span>({ <span class="hljs-attr">name</span>: <span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-variable language_">this</span>.name}</span>-strokeColorFactory`</span>});</pre></div></div> </li> <li id="section-7"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-7">&#x00a7;</a> </div> <p>The <code>range</code> attributes use Vector objects in which to hold their data.</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-variable language_">this</span>.<span class="hljs-property">range</span> = <span class="hljs-title function_">makeVector</span>(); <span class="hljs-variable language_">this</span>.<span class="hljs-property">rangeFrom</span> = <span class="hljs-title function_">makeVector</span>();</pre></div></div> </li> <li id="section-8"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-8">&#x00a7;</a> </div> <p>As part of its <code>stamp</code> functionality the Emitter entity will invoke three user-defined <code>xyzAction</code> functions. If none of these functions are supplied to the entity, then it will not display anything on the canvas.</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-variable language_">this</span>.<span class="hljs-property">preAction</span> = λ<span class="hljs-literal">null</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-property">stampAction</span> = λ<span class="hljs-literal">null</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-property">postAction</span> = λ<span class="hljs-literal">null</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>Setup the particle store, including the arrays used for winnowing out and killing dead particles</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-variable language_">this</span>.<span class="hljs-property">particleStore</span> = []; <span class="hljs-variable language_">this</span>.<span class="hljs-property">deadParticles</span> = []; <span class="hljs-variable language_">this</span>.<span class="hljs-property">liveParticles</span> = []; <span class="hljs-keyword">if</span> (!items.<span class="hljs-property">group</span>) items.<span class="hljs-property">group</span> = currentGroup; <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">set</span>(items); <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">purge</span>) <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">purgeArtefact</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">purge</span>); <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</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="emitter-prototype">Emitter prototype</h4> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">let</span> P = <span class="hljs-title class_">Emitter</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;Emitter&#x27;</span>; P.<span class="hljs-property">lib</span> = <span class="hljs-string">&#x27;entity&#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-11"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-11">&#x00a7;</a> </div> <h4 id="mixins">Mixins</h4> </div> <div class="content"><div class='highlight'><pre>P = <span class="hljs-title function_">baseMix</span>(P); P = <span class="hljs-title function_">entityMix</span>(P);</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="emitter-attributes">Emitter 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, calculateOrder, stampOrder, 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/entity.html">entity mixin</a>: <strong>method, pathObject, winding, flipReverse, flipUpend, scaleOutline, lockFillStyleToEntity, lockStrokeStyleToEntity, onEnter, onLeave, onDown, onUp, <em>fillStyle, strokeStyle, globalAlpha, globalCompositeOperation, lineWidth, lineCap, lineJoin, lineDash, lineDashOffset, miterLimit, shadowOffsetX, shadowOffsetY, shadowBlur, shadowColor, filter</em></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/filter.html">filter mixin</a>: <strong>filters, isStencil</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-13"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-13">&#x00a7;</a> </div> <p><strong>world</strong> - World object; can be set using the String name of a World object, or the World object itself.</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">world</span>: <span class="hljs-literal">null</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>artefact</strong> - In theory, any Scrawl-canvas object whose <code>isArtefact</code> flag is set to <code>true</code> can be assigned to this attribute. However this has not been tested on non-entity artefacts. For now, stick to Scrawl-canvas entity objects.</p> <ul> <li>Can be set using the String name of an artefact object, or the artefact object itself.</li> </ul> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">artefact</span>: <span class="hljs-literal">null</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>range</strong> and <strong>rangeFrom</strong> - Vector objects with some convenience pseudo-attributes to make setting them a bit easier: <em>rangeX, rangeY, rangeZ, rangeFromX, rangeFromY, rangeFromZ</em>.</p> <ul> <li>These attributes set each generated particle’sinitial velocity; their values represent the distance travelled in the x, y and z directions, as measured in pixels-per-second.</li> <li>The <code>rangeFrom</code> attributes (float Numbers that can be negative) the lowest value in that dimension that will be generated. This value is <em><strong>local to the particle</strong></em> thus negative values are to the left (x) or above (y) or behind (z) the particle’s initial position.</li> <li>The <code>range</code> attributes (again, float Numbers that can be negative) are the <em><strong>maximum (or least maximum) random value</strong></em> which will be added to the rangeFrom value. </li> <li>All particles are assigned a (constrained) random velocity in this manner when they are generated.</li> </ul> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">range</span>: <span class="hljs-literal">null</span>, <span class="hljs-attr">rangeFrom</span>: <span class="hljs-literal">null</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>generationRate</strong> - positive integer Number - Emitter entitys use <em><strong>ephemeral particles</strong></em> to produce their visual effects, generating a steady stream of particles over time and then killing them off in various ways. Attribute <em>sets the maximum number of particles that the Emitter will generate every second</em>.</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">generationRate</span>: <span class="hljs-number">0</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>particleCount</strong> - positive integer Number - attribute <em>sets the maximum number of particles that the Emitter will manage and display at any one time</em>.</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">particleCount</span>: <span class="hljs-number">0</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>generateAlongPath</strong>, <strong>generateInArea</strong> - Object-based flags (default: <code>false</code>) - to set the flags, assign an entity object to them</p> <ul> <li>the default action is for the Emitter to generate its particles from a single coordinate which can be determined from the Emitter’s <code>lockTo</code> attribute - thus the coordinate can be the absolute/relative start coordinates, or a path/pivot/mimic reference entity, or a Net particle, or the mouse cursor.</li> <li>If we set the <code>generateAlongPath</code> attribute to a path-based entity then the Emitter will use that path to set the initial coordinate for all its generated particles</li> <li>If we set the <code>generateInArea</code> attribute to any entity then the Emitter will use that entity’s area to set the initial coordinate for all its generated particles</li> <li><code>generateInArea</code> takes precedence over <code>generateAlongPath</code>, which in turn takes precedence over the default coordinate behaviour</li> </ul> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">generateAlongPath</span>: <span class="hljs-literal">null</span>, <span class="hljs-attr">generateInArea</span>: <span class="hljs-literal">null</span>, <span class="hljs-attr">generateFromExistingParticles</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">generateFromExistingParticleHistories</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">limitDirectionToAngleMultiples</span>: <span class="hljs-number">0</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>generationChoke</strong> - Number measuring milliseconds (default: 15) - because both <code>generateAlongPath</code> and <code>generateInArea</code> functionalities use a <code>while</code> loop, we need a way to break out of those loops should they fail to generate an acceptable coordinate within a given amount of time. This attribute sets the maximum time the entity will spend on generating semi-random coordinates during any one Display cycle loop.</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">generationChoke</span>: <span class="hljs-number">15</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>Emitter entitys will continuously generate new particles (up to the limit set in the <code>particleCount</code> attribute). The <code>killAfterTime</code>, <code>killRadius</code> and <code>killBeyondCanvas</code> attributes set out the circumstances in which existing particles will be removed from the entity’s <code>particleStore</code> attribute</p> <ul> <li><strong>killAfterTime</strong> - a positive float Number - sets the maximum time (measured in <em><strong>seconds</strong></em>) that a particle will live before it is killed and removed. This time is set on particle generation and is not updatable. We can add some randomness to the time through the <strong>killAfterTimeVariation</strong> attribute.</li> <li><strong>killRadius</strong> - a positive float Number - sets the maximum distance (measurted in pixels) from its initial position that a particle can move. If it moves beyond that distance, it will be killed. Again, some variation can be introduced through the <strong>killRadiusVariation</strong> attribute.</li> <li><strong>killBeyondCanvas</strong> - a Boolean flag (default: <code>false</code>) - when set, any particle that moves beyond its host Cell’s canvas dimensions will be killed and removed.</li> </ul> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">killAfterTime</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">killAfterTimeVariation</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">killRadius</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">killRadiusVariation</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">killBeyondCanvas</span>: <span class="hljs-literal">false</span>,</pre></div></div> </li> <li id="section-21"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-21">&#x00a7;</a> </div> <p><strong>historyLength</strong> - positive integer Number - every Particle will keep a record of its recent state, in a set of ParticleHistory arrays stored in the Particle’s <code>history</code> Array. The Emitter entity will set the maximum permitted length of the history array whenever it generates a new Particle. </p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">historyLength</span>: <span class="hljs-number">1</span>,</pre></div></div> </li> <li id="section-22"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-22">&#x00a7;</a> </div> <p>Emitter entitys will, as part of the Display cycle, apply any force objects assigned to a Particle. The initial forces assigned to every new Particle will be in line with the Force objects included in the Emitter’s <strong>forces</strong> Array.</p> <ul> <li>To set the Array, supply a new Array containing Force objects, and/or the name Strings of those Force objects, to the <code>forces</code> attribute.</li> </ul> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">forces</span>: <span class="hljs-literal">null</span>,</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>mass</strong>, <strong>massVariation</strong> - positive float Number - the initial mass assigned to each Particle when it is generated.</p> <ul> <li>The <code>mass</code> attribute is used by the pre-defined <em><strong>gravity Force</strong></em></li> </ul> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">mass</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">massVariation</span>: <span class="hljs-number">0</span>,</pre></div></div> </li> <li id="section-24"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-24">&#x00a7;</a> </div> <p>Physics calculations are handled by the Emitter entity’s physics <strong>engine</strong> which must be a String value of either <code>euler</code> (the default engine), <code>improved-euler</code> or <code>runge-kutta</code>.</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">engine</span>: <span class="hljs-string">&#x27;euler&#x27;</span>,</pre></div></div> </li> <li id="section-25"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-25">&#x00a7;</a> </div> <p>Note that the <strong>hitRadius</strong> attribute is tied directly to the <strong>width</strong> and <strong>height</strong> attributes (which are effectively meaningless for this entity)</p> <ul> <li>This attribute is absolute - unlike other Scrawl-canvas radius attributes it cannot be set using a percentage String value</li> </ul> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">hitRadius</span>: <span class="hljs-number">10</span>,</pre></div></div> </li> <li id="section-26"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-26">&#x00a7;</a> </div> <p>We can tell the entity to display its hit zone by setting the <code>showHitRadius</code> flag. The hit zone outline color attribute <code>hitRadiusColor</code> accepts any valid CSS color String value</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">showHitRadius</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">hitRadiusColor</span>: <span class="hljs-string">&#x27;#000000&#x27;</span>,</pre></div></div> </li> <li id="section-27"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-27">&#x00a7;</a> </div> <p><strong>resetAfterBlur</strong> - positive float Number (measuring seconds) - physics simulations can be brittle, particularly if they are forced to calculate Particle loads (accelerations), velocities and speeds over a large time step. Rather than manage that time step in cases where the user may neglect or navigate away from the browser tab containing the physics animation, Scrawl-canvas will stop, clear, and recreate the scene if the time it takes the user to return to (re-focus on) the web page is greater than the value set in this attribute.</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-attr">resetAfterBlur</span>: <span class="hljs-number">3</span>,</pre></div></div> </li> <li id="section-28"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-28">&#x00a7;</a> </div> <h5 id="not-defined-in-the-defs-object-but-set-up-in-the-constructor-and-setters">Not defined in the defs object, but set up in the constructor and setters</h5> </div> </li> <li id="section-29"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-29">&#x00a7;</a> </div> <p><strong>particleStore</strong> - an Array where all the Emitter’s current particles will be stored. To render the entity, we need to iterate through these particles and use them to repeatedly stamp the Emitter’s artefact - or perform equivalent &lt;canvas&gt; context engine instructions - onto the host Cell. These actions will be defined in the <code>stampAction</code> function.</p> </div> </li> <li id="section-30"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-30">&#x00a7;</a> </div> <p>The user-defined stamp functions <strong>preAction</strong>, <strong>stampAction</strong> and <strong>postAction</strong> are invoked in turn one each tick of the Display cycle. By default these functions do nothing, meaning nothing gets drawn to the canvas</p> <ul> <li><code>preAction</code> and <code>postAction</code> - these functions receive a single argument, a Cell wrapper on which we can draw additional graphics (if needed) - see Demo <a href="../../demo/particles-006.html">Particles 006</a> for a working example</li> <li><code>stampAction</code> - define all major rendering actions in this function. The function receives the following arguments: <code>(artefact, particle, host)</code> - where <code>artefact</code> is the Emitter entity’s artefact object (if any has been defined/set); <code>particle</code> is the current Particle object whose history needs to be rendered onto the canvas; and <code>host</code> is the Cell wrapper on which