Title: [202665] trunk/Websites/webkit.org
Revision
202665
Author
[email protected]
Date
2016-06-29 21:17:33 -0700 (Wed, 29 Jun 2016)

Log Message

Demo and JS implementation of our spring() animation
timing function.

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

Modified Paths

Added Paths

Diff

Modified: trunk/Websites/webkit.org/ChangeLog (202664 => 202665)


--- 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)
@@ -1,5 +1,15 @@
 2016-06-29  Dean Jackson  <[email protected]>
 
+        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  <[email protected]>
+
         Small change to wording, and link to the interactive version.
 
         * blog-files/color-gamut/index.html:

Added: trunk/Websites/webkit.org/demos/spring/index.html (0 => 202665)


--- 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)
@@ -0,0 +1,159 @@
+<!DOCTYPE html>
+<html>
+<head>
+<title>Spring timing function demo</title>
+<link href="" rel="stylesheet">
+<style>
+
+body {
+    background-color: #CECECE;
+    font-family: "Source Sans Pro", 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);
+}
+
+</style>
+<script src=""
+<script src=""
+<script>
+
+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 < 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("useSystem", newValue);
+    }
+};
+
+window.addEventListener("load", function() {
+    var s = new Spring(document.getElementById("object"));
+    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("canvas").addEventListener("click", function (event) {
+        s.moveTo(event.clientX, event.clientY);
+    }, false);
+
+}, false);
+
+</script>
+</head>
+<body>
+    <h1>Spring timing function</h1>
+    <p>
+        Click/tap to move the object. Selecting "useSystemSpring" will run the
+        transition using the spring() timing function. Otherwise it
+        is the embedded _javascript_ solver, which should be identical.
+    </p>
+
+    <div id="canvas">
+        <div id="object"></div>
+    </div>
+
+</body>
+</html>
Property changes on: trunk/Websites/webkit.org/demos/spring/index.html
___________________________________________________________________

Added: svn:eol-style

+native \ No newline at end of property

Added: svn:keywords

+Date Revision \ No newline at end of property

Added: svn:mime-type

+text/html \ No newline at end of property

Added: trunk/Websites/webkit.org/demos/spring/spring.js (0 => 202665)


--- 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)
@@ -0,0 +1,56 @@
+/*
+ * 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 < 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 < 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;
+};
Property changes on: trunk/Websites/webkit.org/demos/spring/spring.js
___________________________________________________________________

Added: svn:eol-style

+native \ No newline at end of property

Added: svn:keywords

+Date Revision \ No newline at end of property

Added: svn:mime-type

+text/plain \ No newline at end of property
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to