<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[202665] trunk/Websites/webkit.org</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/202665">202665</a></dd>
<dt>Author</dt> <dd>dino@apple.com</dd>
<dt>Date</dt> <dd>2016-06-29 21:17:33 -0700 (Wed, 29 Jun 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>Demo and JS implementation of our spring() animation
timing function.

* demos/spring/index.html: Added.
* demos/spring/spring.js: Added.
(SpringSolver):
(SpringSolver.prototype.solve):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkWebsiteswebkitorgChangeLog">trunk/Websites/webkit.org/ChangeLog</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li>trunk/Websites/webkit.org/demos/spring/</li>
<li><a href="#trunkWebsiteswebkitorgdemosspringindexhtml">trunk/Websites/webkit.org/demos/spring/index.html</a></li>
<li><a href="#trunkWebsiteswebkitorgdemosspringspringjs">trunk/Websites/webkit.org/demos/spring/spring.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkWebsiteswebkitorgChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Websites/webkit.org/ChangeLog (202664 => 202665)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Websites/webkit.org/ChangeLog        2016-06-30 03:55:34 UTC (rev 202664)
+++ trunk/Websites/webkit.org/ChangeLog        2016-06-30 04:17:33 UTC (rev 202665)
</span><span class="lines">@@ -1,5 +1,15 @@
</span><span class="cx"> 2016-06-29  Dean Jackson  &lt;dino@apple.com&gt;
</span><span class="cx"> 
</span><ins>+        Demo and JS implementation of our spring() animation
+        timing function.
+
+        * demos/spring/index.html: Added.
+        * demos/spring/spring.js: Added.
+        (SpringSolver):
+        (SpringSolver.prototype.solve):
+
+2016-06-29  Dean Jackson  &lt;dino@apple.com&gt;
+
</ins><span class="cx">         Small change to wording, and link to the interactive version.
</span><span class="cx"> 
</span><span class="cx">         * blog-files/color-gamut/index.html:
</span></span></pre></div>
<a id="trunkWebsiteswebkitorgdemosspringindexhtml"></a>
<div class="addfile"><h4>Added: trunk/Websites/webkit.org/demos/spring/index.html (0 => 202665)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Websites/webkit.org/demos/spring/index.html                                (rev 0)
+++ trunk/Websites/webkit.org/demos/spring/index.html        2016-06-30 04:17:33 UTC (rev 202665)
</span><span class="lines">@@ -0,0 +1,159 @@
</span><ins>+&lt;!DOCTYPE html&gt;
+&lt;html&gt;
+&lt;head&gt;
+&lt;title&gt;Spring timing function demo&lt;/title&gt;
+&lt;link href=&quot;https://fonts.googleapis.com/css?family=Source+Sans+Pro&quot; rel=&quot;stylesheet&quot;&gt;
+&lt;style&gt;
+
+body {
+    background-color: #CECECE;
+    font-family: &quot;Source Sans Pro&quot;, sans-serif;
+    color: #5E275A;
+}
+
+h1 {
+    font-size: 200%;
+    color: #5E275A;
+    border-bottom: 1px solid #888;
+    width: 500px;
+    margin-top: 0;
+}
+
+p {
+    width: 500px;
+}
+
+#canvas {
+    position: absolute;
+    top: 0;
+    left: 0;
+    width: 100vw;
+    height: 100vh;
+    z-index: -1;
+}
+
+#object {
+    position: absolute;
+    left: -20px;
+    top: -20px;
+    width: 40px;
+    height: 40px;
+    border-radius: 20px;
+    box-sizing: border-box;
+    border: 2px solid #6F6F6F;
+    background-color: #6E9FB7;
+    transform: translate(100px, 200px);
+    will-change: transform;
+}
+
+.useSystem #object {
+    transition-property: transform;
+    transition-duration: 10s; /* large enough to let the spring happen */
+    transition-timing-function: spring(1 100 10 0);
+}
+
+&lt;/style&gt;
+&lt;script src=&quot;https://raw.github.com/dataarts/dat.gui/master/build/dat.gui.min.js&quot;&gt;&lt;/script&gt;
+&lt;script src=&quot;spring.js&quot;&gt;&lt;/script&gt;
+&lt;script&gt;
+
+var Spring = function(element)
+{
+    this.element = element;
+    this.currentX = 100;
+    this.currentY = 200;
+    this.animationIsRunning = false;
+
+    this.mass = 1;
+    this.stiffness = 100;
+    this.damping = 10;
+    this.initialVelocity = 0;
+    this.useSystemSpring = false;
+};
+
+Spring.prototype = {
+    moveTo: function (x, y)
+    {
+        if (this._useSystemSpring) {
+            this.animationIsRunning = false;
+            this.element.style.transitionTimingFunction = `spring(${this.mass} ${this.stiffness} ${this.damping} ${this.initialVelocity})`;
+            this.element.style.transform = `translate(${x}px, ${y}px)`;
+        } else {
+            this.animationIsRunning = true;
+            var bounds = this.element.getBoundingClientRect();
+            this.currentX = bounds.left + 20;
+            this.currentY = bounds.top + 20;
+            this.startX = this.currentX;
+            this.startY = this.currentY;
+            this.targetX = x;
+            this.targetY = y;
+            this.startTime = Date.now() / 1000;
+            this.solver = new SpringSolver(this.mass, this.stiffness, this.damping, this.initialVelocity);
+            window.requestAnimationFrame(this.step.bind(this));
+        }
+    },
+
+    step: function()
+    {
+        if (!this.animationIsRunning) {
+            return;
+        }
+
+        var elapsed = Date.now() / 1000 - this.startTime;
+        var proportion = this.solver.solve(elapsed);
+
+        this.currentX = this.startX + (this.targetX - this.startX) * proportion;
+        this.currentY = this.startY + (this.targetY - this.startY) * proportion;
+        this.element.style.transform = `translate(${this.currentX}px, ${this.currentY}px)`;
+
+        // Keep animating for 10 seconds. Too bad if it hasn't finished by then.
+        if (elapsed &lt; 10 * 1000) {
+            window.requestAnimationFrame(this.step.bind(this));
+        } else {
+            this.animationIsRunning = false;
+        }
+    },
+
+    get useSystemSpring()
+    {
+        return this._useSystemSpring;
+    },
+
+    set useSystemSpring(newValue)
+    {
+        this._useSystemSpring = newValue;
+        document.body.classList.toggle(&quot;useSystem&quot;, newValue);
+    }
+};
+
+window.addEventListener(&quot;load&quot;, function() {
+    var s = new Spring(document.getElementById(&quot;object&quot;));
+    var gui = new dat.GUI();
+    gui.add(s, 'mass', 0, 20);
+    gui.add(s, 'stiffness', 0, 200);
+    gui.add(s, 'damping', 0, 50);
+    gui.add(s, 'initialVelocity', -50, 50);
+    gui.add(s, 'useSystemSpring');
+
+    document.getElementById(&quot;canvas&quot;).addEventListener(&quot;click&quot;, function (event) {
+        s.moveTo(event.clientX, event.clientY);
+    }, false);
+
+}, false);
+
+&lt;/script&gt;
+&lt;/head&gt;
+&lt;body&gt;
+    &lt;h1&gt;Spring timing function&lt;/h1&gt;
+    &lt;p&gt;
+        Click/tap to move the object. Selecting &quot;useSystemSpring&quot; will run the
+        transition using the spring() timing function. Otherwise it
+        is the embedded JavaScript solver, which should be identical.
+    &lt;/p&gt;
+
+    &lt;div id=&quot;canvas&quot;&gt;
+        &lt;div id=&quot;object&quot;&gt;&lt;/div&gt;
+    &lt;/div&gt;
+
+&lt;/body&gt;
+&lt;/html&gt;
</ins><span class="cx">Property changes on: trunk/Websites/webkit.org/demos/spring/index.html
</span><span class="cx">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4>Added: svn:eol-style</h4></div>
<ins>+native
</ins><span class="cx">\ No newline at end of property
</span><a id="svnkeywords"></a>
<div class="addfile"><h4>Added: svn:keywords</h4></div>
<ins>+Date Revision
</ins><span class="cx">\ No newline at end of property
</span><a id="svnmimetype"></a>
<div class="addfile"><h4>Added: svn:mime-type</h4></div>
<ins>+text/html
</ins><span class="cx">\ No newline at end of property
</span><a id="trunkWebsiteswebkitorgdemosspringspringjs"></a>
<div class="addfile"><h4>Added: trunk/Websites/webkit.org/demos/spring/spring.js (0 => 202665)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Websites/webkit.org/demos/spring/spring.js                                (rev 0)
+++ trunk/Websites/webkit.org/demos/spring/spring.js        2016-06-30 04:17:33 UTC (rev 202665)
</span><span class="lines">@@ -0,0 +1,56 @@
</span><ins>+/*
+ * Copyright (C) 2016 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+function SpringSolver(mass, stiffness, damping, initialVelocity)
+{
+    this.m_w0 = Math.sqrt(stiffness / mass);
+    this.m_zeta = damping / (2 * Math.sqrt(stiffness * mass));
+
+    if (this.m_zeta &lt; 1) {
+        // Under-damped.
+        this.m_wd = this.m_w0 * Math.sqrt(1 - this.m_zeta * this.m_zeta);
+        this.m_A = 1;
+        this.m_B = (this.m_zeta * this.m_w0 + -initialVelocity) / this.m_wd;
+    } else {
+        // Critically damped (ignoring over-damped case for now).
+        this.m_wd = 0;
+        this.m_A = 1;
+        this.m_B = -initialVelocity + this.m_w0;
+    }
+}
+
+SpringSolver.prototype.solve = function (t)
+{
+    if (this.m_zeta &lt; 1) {
+        // Under-damped
+        t = Math.exp(-t * this.m_zeta * this.m_w0) * (this.m_A * Math.cos(this.m_wd * t) + this.m_B * Math.sin(this.m_wd * t));
+    } else {
+        // Critically damped (ignoring over-damped case for now).
+        t = (this.m_A + thism_B * t) * Math.exp(-t * this.m_w0);
+    }
+
+    // Map range from [1..0] to [0..1].
+    return 1 - t;
+};
</ins><span class="cx">Property changes on: trunk/Websites/webkit.org/demos/spring/spring.js
</span><span class="cx">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4>Added: svn:eol-style</h4></div>
<ins>+native
</ins><span class="cx">\ No newline at end of property
</span><a id="svnkeywords"></a>
<div class="addfile"><h4>Added: svn:keywords</h4></div>
<ins>+Date Revision
</ins><span class="cx">\ No newline at end of property
</span><a id="svnmimetype"></a>
<div class="addfile"><h4>Added: svn:mime-type</h4></div>
<ins>+text/plain
</ins><span class="cx">\ No newline at end of property
</span></div>

</body>
</html>