UNPKG

scrawl-canvas

Version:
1,006 lines (632 loc) 44.8 kB
<!DOCTYPE html> <html> <head> <title>Quaternion 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="quaternion-factory">Quaternion factory</h1> <p>Scrawl-canvas uses quaternion objects for some of its calculations - in particular for calculating DOM element 3D rotation values. These objects are not stored in the library; rather, they are kept in a <strong>quaternion pool</strong> and pulled from it when required.</p> <p>TODO: there’s errors in the math here! See in particular Demo <a href="../../demo/snippets-003.html">Snippets-003</a> to experiment:</p> <ul> <li>Pitch always appears to rotate with reference to the element. Yaw and roll, however, seem to rotate with reference to the stack frame, or the element - or possibly a combination of both - dependant on the values of the other euler attributes.</li> <li><code>quaternionMultiply</code> is almost certainly wrong - caused by trying to convert code meant for a y-axis-upwards frame to code that works in a y-axis-downwards frame</li> <li>The <code>setFromEuler</code> function is also (probably) wrong</li> <li>I hate quaternions!</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="imports">Imports</h4> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">import</span> { radian, constructors } <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../core/library.js&#x27;</span>; <span class="hljs-keyword">import</span> { isa_quaternion, isa_number, xt, xto, Ωempty } <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../core/utilities.js&#x27;</span>; <span class="hljs-keyword">import</span> { requestVector, releaseVector, makeVector } <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./vector.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="quaternion-constructor">Quaternion constructor</h4> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> <span class="hljs-title class_">Quaternion</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">items = Ωempty</span>) { <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = items.<span class="hljs-property">name</span> || <span class="hljs-string">&#x27;generic&#x27;</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-property">n</span> = items.<span class="hljs-property">n</span> || <span class="hljs-number">1</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-property">v</span> = <span class="hljs-title function_">makeVector</span>(); <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">set</span>(items); <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</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="quaternion-prototype">Quaternion prototype</h4> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">let</span> P = <span class="hljs-title class_">Quaternion</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;Quaternion&#x27;</span>;</pre></div></div> </li> <li id="section-5"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-5">&#x00a7;</a> </div> <h4 id="mixins">Mixins</h4> <p>Quaternion objects do not use mixins - they are regular Javascript objects. As such, they do not possess packet, clone or kill functionality.</p> </div> </li> <li id="section-6"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-6">&#x00a7;</a> </div> <h4 id="quaternion-attributes">Quaternion attributes</h4> <p>The <strong>name</strong>, <strong>n</strong> and <strong>v</strong> (Vector object) attributes are set in the Quaternion constructor - the Quaternion object does not have a <code>defs</code> object. The Vector attributes <strong>v.x</strong>, <strong>v.y</strong>, <strong>v.z</strong> can also be set directly.</p> <p><strong>We strongly advise against setting Quaternion object attributes directly!</strong> Quaternions are complex beasts and their <code>n</code> and <code>v</code> attributes are linked in mysterious (and possibly debauched) ways. Set and manipulate your Quaternion object’s attributes using the functions below.</p> </div> </li> <li id="section-7"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-7">&#x00a7;</a> </div> <h4 id="get-set-deltaset">Get, Set, deltaSet</h4> </div> </li> <li id="section-8"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-8">&#x00a7;</a> </div> <p>Quaternion attributes can be retrieved directly - <code>myquaternion.n</code>, <code>myquaternion.v</code>. The <strong>v</strong> attribute is a Vector object, thus its attributes can be retrieved using that object’s convenience functions <code>myquaternion.v.getXYCoordinate</code> and <code>myquaternion.v.getXYZCoordinate</code> - these functions will return Arrays.</p> </div> </li> <li id="section-9"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-9">&#x00a7;</a> </div> <p>The Quaternion <code>set</code> function is overloaded. It can accept the following arguments:</p> <ul> <li><code>set(Quaternion)</code> - a Quaternion object</li> <li><code>set(Vector)</code> - a Vector object</li> <li><code>set({x:Number, y:Number, z:Number})</code> - a Javascript object containing one or more attributes from: x, y, z - with values in degrees (not radians)</li> <li><code>set({pitch:Number, yaw:Number, roll:Number})</code> - a Javascript object containing one or more attributes: <strong>pitch</strong> (for x-axis rotations); <strong>yaw</strong> (for y-axis rotations); <strong>roll</strong> (for z-axis rotations) - with values in degrees (not radians)</li> </ul> </div> <div class="content"><div class='highlight'><pre>P.<span class="hljs-property">set</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">obj = Ωempty</span>) { <span class="hljs-keyword">if</span> (<span class="hljs-title function_">isa_quaternion</span>(obj)) <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">setFromQuaternion</span>(obj); <span class="hljs-comment">/* if ((obj &amp;&amp; obj.type &amp;&amp; obj.type === &#x27;Vector&#x27;)) return this.setFromVector(obj); */</span> <span class="hljs-keyword">if</span> (<span class="hljs-title function_">xto</span>(obj.<span class="hljs-property">pitch</span>, obj.<span class="hljs-property">yaw</span>, obj.<span class="hljs-property">roll</span>)) <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">setFromEuler</span>(obj); <span class="hljs-keyword">let</span> x, y, z, n, v, tv = <span class="hljs-variable language_">this</span>.<span class="hljs-property">v</span>; v = (<span class="hljs-title function_">xt</span>(obj.<span class="hljs-property">vector</span>) || <span class="hljs-title function_">xt</span>(obj.<span class="hljs-property">v</span>)) ? (obj.<span class="hljs-property">vector</span> || obj.<span class="hljs-property">v</span>) : <span class="hljs-literal">false</span>; n = (<span class="hljs-title function_">xt</span>(obj.<span class="hljs-property">scalar</span>) || <span class="hljs-title function_">xt</span>(obj.<span class="hljs-property">n</span>)) ? (obj.<span class="hljs-property">scalar</span> || obj.<span class="hljs-property">n</span> || <span class="hljs-number">0</span>) : <span class="hljs-literal">false</span>; x = (v) ? (v.<span class="hljs-property">x</span> || <span class="hljs-number">0</span>) : obj.<span class="hljs-property">x</span> || <span class="hljs-literal">false</span>; y = (v) ? (v.<span class="hljs-property">y</span> || <span class="hljs-number">0</span>) : obj.<span class="hljs-property">y</span> || <span class="hljs-literal">false</span>; z = (v) ? (v.<span class="hljs-property">z</span> || <span class="hljs-number">0</span>) : obj.<span class="hljs-property">z</span> || <span class="hljs-literal">false</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-property">n</span> = (<span class="hljs-title function_">isa_number</span>(n)) ? n : <span class="hljs-variable language_">this</span>.<span class="hljs-property">n</span>; tv.<span class="hljs-property">x</span> = (<span class="hljs-title function_">isa_number</span>(x)) ? x : tv.<span class="hljs-property">x</span>; tv.<span class="hljs-property">y</span> = (<span class="hljs-title function_">isa_number</span>(y)) ? y : tv.<span class="hljs-property">y</span>; tv.<span class="hljs-property">z</span> = (<span class="hljs-title function_">isa_number</span>(z)) ? z : tv.<span class="hljs-property">z</span>; <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; }; P.<span class="hljs-property">setFromQuaternion</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">item</span>) { <span class="hljs-keyword">if</span> (!<span class="hljs-title function_">isa_quaternion</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">`<span class="hljs-subst">${<span class="hljs-variable language_">this</span>.name}</span> Quaternion error - setFromQuaternion() bad argument: <span class="hljs-subst">${item}</span>`</span>); <span class="hljs-keyword">let</span> tv = <span class="hljs-variable language_">this</span>.<span class="hljs-property">v</span>, iv = item.<span class="hljs-property">v</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-property">n</span> = item.<span class="hljs-property">n</span>; tv.<span class="hljs-property">x</span> = iv.<span class="hljs-property">x</span>; tv.<span class="hljs-property">y</span> = iv.<span class="hljs-property">y</span>; tv.<span class="hljs-property">z</span> = iv.<span class="hljs-property">z</span>; <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; }; P.<span class="hljs-property">setFromEuler</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">items = Ωempty</span>) { <span class="hljs-keyword">let</span> pitch, yaw, roll, c1, c2, c3, s1, s2, s3, cos = <span class="hljs-title class_">Math</span>.<span class="hljs-property">cos</span>, sin = <span class="hljs-title class_">Math</span>.<span class="hljs-property">sin</span>, tv = <span class="hljs-variable language_">this</span>.<span class="hljs-property">v</span>; pitch = (items.<span class="hljs-property">pitch</span> || items.<span class="hljs-property">x</span> || <span class="hljs-number">0</span>) * radian; yaw = (items.<span class="hljs-property">yaw</span> || items.<span class="hljs-property">y</span> || <span class="hljs-number">0</span>) * radian; roll = (items.<span class="hljs-property">roll</span> || items.<span class="hljs-property">z</span> || <span class="hljs-number">0</span>) * radian; c1 = <span class="hljs-title function_">cos</span>( pitch / <span class="hljs-number">2</span> ); c2 = <span class="hljs-title function_">cos</span>( yaw / <span class="hljs-number">2</span> ); c3 = <span class="hljs-title function_">cos</span>( roll / <span class="hljs-number">2</span> ); s1 = <span class="hljs-title function_">sin</span>( pitch / <span class="hljs-number">2</span> ); s2 = <span class="hljs-title function_">sin</span>( yaw / <span class="hljs-number">2</span> ); s3 = <span class="hljs-title function_">sin</span>( roll / <span class="hljs-number">2</span> ); tv.<span class="hljs-property">x</span> = s1 * c2 * c3 + c1 * s2 * s3; tv.<span class="hljs-property">y</span> = c1 * s2 * c3 + s1 * c2 * s3; tv.<span class="hljs-property">z</span> = c1 * c2 * s3 - s1 * s2 * c3; <span class="hljs-variable language_">this</span>.<span class="hljs-property">n</span> = c1 * c2 * c3 - s1 * s2 * s3; <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="prototype-functions">Prototype functions</h4> </div> </li> <li id="section-11"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-11">&#x00a7;</a> </div> <p>Set the Quaternion attributes to their default values</p> </div> <div class="content"><div class='highlight'><pre>P.<span class="hljs-property">zero</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) { <span class="hljs-keyword">let</span> v = <span class="hljs-variable language_">this</span>.<span class="hljs-property">v</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-property">n</span> = <span class="hljs-number">1</span>; v.<span class="hljs-property">x</span> = <span class="hljs-number">0</span>; v.<span class="hljs-property">y</span> = <span class="hljs-number">0</span>; v.<span class="hljs-property">z</span> = <span class="hljs-number">0</span>; <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</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>Get the Quaternion’s <strong>magnitude</strong> value</p> </div> <div class="content"><div class='highlight'><pre>P.<span class="hljs-property">getMagnitude</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) { <span class="hljs-keyword">let</span> v = <span class="hljs-variable language_">this</span>.<span class="hljs-property">v</span>; <span class="hljs-keyword">return</span> <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">sqrt</span>((<span class="hljs-variable language_">this</span>.<span class="hljs-property">n</span> * <span class="hljs-variable language_">this</span>.<span class="hljs-property">n</span>) + (v.<span class="hljs-property">x</span> * v.<span class="hljs-property">x</span>) + (v.<span class="hljs-property">y</span> * v.<span class="hljs-property">y</span>) + (v.<span class="hljs-property">z</span> * v.<span class="hljs-property">z</span>)); };</pre></div></div> </li> <li id="section-13"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-13">&#x00a7;</a> </div> <p>Normalize the Quaternion</p> </div> <div class="content"><div class='highlight'><pre>P.<span class="hljs-property">normalize</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) { <span class="hljs-keyword">let</span> mag = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">getMagnitude</span>(), v = <span class="hljs-variable language_">this</span>.<span class="hljs-property">v</span>; <span class="hljs-keyword">if</span> (!mag) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-variable language_">this</span>.name}</span> Quaternion error - normalize() division by zero: <span class="hljs-subst">${mag}</span>`</span>); <span class="hljs-variable language_">this</span>.<span class="hljs-property">n</span> /= mag; <span class="hljs-variable language_">this</span>.<span class="hljs-property">n</span> = (<span class="hljs-variable language_">this</span>.<span class="hljs-property">n</span> &gt; -<span class="hljs-number">0.000001</span> &amp;&amp; <span class="hljs-variable language_">this</span>.<span class="hljs-property">n</span> &lt; <span class="hljs-number">0.000001</span>) ? <span class="hljs-number">0</span> : <span class="hljs-variable language_">this</span>.<span class="hljs-property">n</span>; v.<span class="hljs-property">x</span> /= mag; v.<span class="hljs-property">x</span> = (v.<span class="hljs-property">x</span> &gt; -<span class="hljs-number">0.000001</span> &amp;&amp; v.<span class="hljs-property">x</span> &lt; <span class="hljs-number">0.000001</span>) ? <span class="hljs-number">0</span> : v.<span class="hljs-property">x</span>; v.<span class="hljs-property">y</span> /= mag; v.<span class="hljs-property">y</span> = (v.<span class="hljs-property">y</span> &gt; -<span class="hljs-number">0.000001</span> &amp;&amp; v.<span class="hljs-property">y</span> &lt; <span class="hljs-number">0.000001</span>) ? <span class="hljs-number">0</span> : v.<span class="hljs-property">y</span>; v.<span class="hljs-property">z</span> /= mag; v.<span class="hljs-property">z</span> = (v.<span class="hljs-property">z</span> &gt; -<span class="hljs-number">0.000001</span> &amp;&amp; v.<span class="hljs-property">z</span> &lt; <span class="hljs-number">0.000001</span>) ? <span class="hljs-number">0</span> : v.<span class="hljs-property">z</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>Multiply the Quaternion by another Quaternion</p> </div> <div class="content"><div class='highlight'><pre>P.<span class="hljs-property">quaternionMultiply</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">item</span>) { <span class="hljs-keyword">if</span> (!<span class="hljs-title function_">isa_quaternion</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">`<span class="hljs-subst">${<span class="hljs-variable language_">this</span>.name}</span> Quaternion error - quaternionMultiply() bad argument: <span class="hljs-subst">${item}</span>`</span>); <span class="hljs-keyword">let</span> tv = <span class="hljs-variable language_">this</span>.<span class="hljs-property">v</span>, iv = item.<span class="hljs-property">v</span>, n1 = <span class="hljs-variable language_">this</span>.<span class="hljs-property">n</span>, x1 = tv.<span class="hljs-property">x</span>, y1 = tv.<span class="hljs-property">y</span>, z1 = tv.<span class="hljs-property">z</span>, n2 = item.<span class="hljs-property">n</span>, x2 = iv.<span class="hljs-property">x</span>, y2 = iv.<span class="hljs-property">y</span>, z2 = iv.<span class="hljs-property">z</span>; <span class="hljs-variable language_">this</span>.<span class="hljs-property">n</span> = (n1 * n2) - (x1 * x2) - (y1 * y2) - (z1 * z2); tv.<span class="hljs-property">x</span> = (n1 * x2) + (x1 * n2) + (y1 * z2) - (z1 * y2); tv.<span class="hljs-property">y</span> = (n1 * y2) + (y1 * n2) + (z1 * x2) - (x1 * z2); tv.<span class="hljs-property">z</span> = (n1 * z2) + (z1 * n2) + (x1 * y2) - (y1 * x2); <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</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>Retrieve the Quaternion’s current angle (returns a Number representing degrees, not radians)</p> </div> <div class="content"><div class='highlight'><pre>P.<span class="hljs-property">getAngle</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">degree</span>) { <span class="hljs-keyword">let</span> result; degree = (<span class="hljs-title function_">xt</span>(degree)) ? degree : <span class="hljs-literal">false</span>; result = <span class="hljs-number">2</span> * <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">acos</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">n</span>); <span class="hljs-keyword">if</span>(degree){ result *= (<span class="hljs-number">1</span> / radian); } <span class="hljs-keyword">return</span> (result &gt; -<span class="hljs-number">0.000001</span> &amp;&amp; result &lt; <span class="hljs-number">0.000001</span>) ? <span class="hljs-number">0</span> : result; };</pre></div></div> </li> <li id="section-16"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-16">&#x00a7;</a> </div> <p>Rotate the Quaternion using another Quaternion’s values</p> </div> <div class="content"><div class='highlight'><pre>P.<span class="hljs-property">quaternionRotate</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">item</span>) { <span class="hljs-keyword">if</span> (!<span class="hljs-title function_">isa_quaternion</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">`<span class="hljs-subst">${<span class="hljs-variable language_">this</span>.name}</span> Quaternion error - quaternionRotate() bad argument: <span class="hljs-subst">${item}</span>`</span>); <span class="hljs-keyword">let</span> q4 = <span class="hljs-title function_">requestQuaternion</span>(item), q5 = <span class="hljs-title function_">requestQuaternion</span>(<span class="hljs-variable language_">this</span>); <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">setFromQuaternion</span>(q4.<span class="hljs-title function_">quaternionMultiply</span>(q5)); <span class="hljs-title function_">releaseQuaternion</span>(q4); <span class="hljs-title function_">releaseQuaternion</span>(q5); <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span> };</pre></div></div> </li> <li id="section-17"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-17">&#x00a7;</a> </div> <h4 id="quaternion-pool">Quaternion pool</h4> <p>An attempt to reuse quaternions rather than constantly creating and deleting them</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> quaternionPool = [];</pre></div></div> </li> <li id="section-18"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-18">&#x00a7;</a> </div> <p><code>exported function</code> - retrieve a Quaternion from the quaternion pool</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> requestQuaternion = <span class="hljs-keyword">function</span> (<span class="hljs-params">items</span>) { <span class="hljs-keyword">if</span> (!quaternionPool.<span class="hljs-property">length</span>) { quaternionPool.<span class="hljs-title function_">push</span>(<span class="hljs-title function_">makeQuaternion</span>({ <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;pool&#x27;</span> })); } <span class="hljs-keyword">let</span> q = quaternionPool.<span class="hljs-title function_">shift</span>(); q.<span class="hljs-title function_">set</span>(items); <span class="hljs-keyword">return</span> q };</pre></div></div> </li> <li id="section-19"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-19">&#x00a7;</a> </div> <p><code>exported function</code> - return a Quaternion to the quaternion pool. Failing to return Quaternion to the pool may lead to more inefficient code and possible memory leaks.</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> releaseQuaternion = <span class="hljs-keyword">function</span> (<span class="hljs-params">q</span>) { <span class="hljs-keyword">if</span> (q &amp;&amp; q.<span class="hljs-property">type</span> === <span class="hljs-string">&#x27;Quaternion&#x27;</span>) { quaternionPool.<span class="hljs-title function_">push</span>(q.<span class="hljs-title function_">zero</span>()); } };</pre></div></div> </li> <li id="section-20"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-20">&#x00a7;</a> </div> <h4 id="factory">Factory</h4> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> makeQuaternion = <span class="hljs-keyword">function</span> (<span class="hljs-params">items</span>) { <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Quaternion</span>(items); }; constructors.<span class="hljs-property">Quaternion</span> = <span class="hljs-title class_">Quaternion</span>;</pre></div></div> </li> <li id="section-21"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-21">&#x00a7;</a> </div> <h4 id="exports">Exports</h4> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> { makeQuaternion, requestQuaternion, releaseQuaternion, };</pre></div></div> </li> </ul> </div> </body> </html>