SVG Wow

The SVG Wow logo springs into place with scripted elastic animation.

What you are seeing

An orange asterisk made of four rounded bars on a white background. On click it jolts into place as an orange exclamation mark falls in beside it and the words svg-wow.org and now available slide in from opposite sides.

How it works

The logo is four rounded bars, reused and rotated with the use element.

A small extension lets YUI animate values inside an SVG transform attribute.

One click starts several animations with different elastic easing.

With reduced motion turned on, the pieces glide in without bouncing.

History

Posted on July 23, 2009. A second version of the file carries the words coming soon.

Source

/animations/svg-wow.svg
<svg width="100%" height="100%"
     style="background:#fff"
     xmlns="http://www.w3.org/2000/svg"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     onload="new sw.animations.SVGWowComingSoon()">
<title>SVG Wow! logo animation</title>
<desc>An orange asterisk made of four rounded bars. On click or Enter it springs into place while an exclamation mark drops in and the words svg-wow.org and now available slide in from the sides.</desc>
<!-- ======================================================================= -->
<!-- Vincent Hardy                                                           -->
<!--  License terms: see svg-wow.org                                         -->
<!-- CC0 http://creativecommons.org/publicdomain/zero/1.0/                   -->
<!-- ======================================================================= -->

    <rect x="50%" y="50%" width="220" height="280" fill="none" stroke="gray" 
     display="none" transform="translate(-110,-140)" />

<svg viewBox="12 30 110 140" xmlns="http://www.w3.org/2000/svg"
     overflow="visible" width="220" height="280" x="50%" y="50%"
     xmlns:xlink="http://www.w3.org/1999/xlink">
    <script xlink:href="../res/2.0/scripts/compat/no-unload.js" />
    <script xlink:href="../res/2.0/scripts/yui/yahoo-dom-event/yahoo-dom-event.js" />
    <script xlink:href="../res/2.0/scripts/dom-utils.js" />
    <script xlink:href="../res/2.0/scripts/yui/animation/animation-min.js" />
    <script xlink:href="../res/2.0/scripts/animation/animate.js" />
    <script xlink:href="svg-wow.js" />
    <defs>
        <g id="spoke">
            <rect x="-30" y="-5" width="60" height="10" />
            <circle cx="-30" cy="0" r="6.5" />
            <circle cx="30" cy="0" r="6.5" />
        </g>

        <g id="spokeCluster">
            <use xlink:href="#spoke" />
            <use xlink:href="#spoke" transform="rotate(45)"/>
            <use xlink:href="#spoke" transform="rotate(90)"/>
            <use xlink:href="#spoke" transform="rotate(135)"/>
        </g>
       <g id="message">
            <text id="svg-wow.org" transform="translate(-500,0)">svg-wow.org</text>
            <text id="coming" transform="translate(500,0)" y="1.1em" fill="white">now available!</text>
       </g>

        <clipPath id="viewportClip">
            <rect width="120" height="140" x="-50" y="-50"/>
        </clipPath>

       <linearGradient id="whiteCast" x1="0" y1="0" x2="1" y2="0"
                       gradientUnits="objectBoundingBox">
            <stop offset="0" stop-color="white" />
            <stop offset="0.1" stop-color="white" stop-opacity="0" />
            <stop offset="0.9" stop-color="white" stop-opacity="0" />
            <stop offset="1" stop-color="white" />
       </linearGradient>

       <linearGradient id="subtle" x1="0" y1="0" x2="1" y2="1"
                       gradientUnits="objectBoundingBox">
            <stop offset="0" stop-color="gray" />
            <stop offset="0.1" stop-color="white" stop-opacity="0" />
            <stop offset="0.9" stop-color="white" stop-opacity="0" />
            <stop offset="1" stop-color="gray" />
       </linearGradient>
    </defs>

    <g id="content" clip-path="url(#viewportClip)">

        <g id="logo" transform="translate(0,0)">
            <use xlink:href="#spokeCluster" fill="black" stroke="black" stroke-width="6"/>
            <use xlink:href="#spokeCluster" fill="rgb(247, 171, 21)"/>
        </g>

        <g stroke-width="3" fill="rgb(247, 171, 21)" stroke="black">
            <rect id="exclamationBar" x="50" y="-35" width="13" height="50" rx="5"
              transform="translate(0,-200)"/>
            <circle id="exclamationPoint" cx="56.5" cy="30" r="7.5"
              transform="translate(0,-200)"/>
        </g>

        <g font-family="'American Typewriter', Georgia, 'Times New Roman', serif" font-size="14"
           font-weight="bold" text-anchor="middle"
           transform="translate(11.5,60)" stroke-linejoin="round"
           stroke-linecap="round">
           <use xlink:href="#message" stroke="black" fill="black" stroke-width="5" />
           <use xlink:href="#message" fill="rgb(247,171,21)" />
        </g>

        <rect x="-50" y="-50" width="130" height="140" fill="url(#whiteCast)" display="none"/>
    </g>

	<g id="bracket">
            <rect x="-60" y="-60" rx="1" ry="1" 
              width="120" height="120" fill="none" stroke="url(#subtle)"/>
            <rect x="-60" y="-60" rx="1" ry="1" transform="scale(-1,1)" 
              width="120" height="120" fill="none" stroke="url(#subtle)"/>
        </g>
</svg>

</svg>
/animations/svg-wow-coming-soon.svg
<svg width="100%" height="100%"
     style="background:#fff"
     xmlns="http://www.w3.org/2000/svg"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     onload="new sw.animations.SVGWowComingSoon()">
<title>SVG Wow! logo animation</title>
<desc>An orange asterisk made of four rounded bars. On click or Enter it springs into place while an exclamation mark drops in and the words svg-wow.org and coming soon slide in from the sides.</desc>
<!-- ======================================================================= -->
<!-- Vincent Hardy                                                           -->
<!--  License terms: see svg-wow.org                                         -->
<!-- CC0 http://creativecommons.org/publicdomain/zero/1.0/                   -->
<!-- ======================================================================= -->

    <rect x="50%" y="50%" width="220" height="280" fill="none" stroke="gray" 
     display="none" transform="translate(-110,-140)" />

<svg viewBox="12 30 110 140" xmlns="http://www.w3.org/2000/svg"
     overflow="visible" width="220" height="280" x="50%" y="50%"
     xmlns:xlink="http://www.w3.org/1999/xlink">
    <script xlink:href="../res/2.0/scripts/compat/no-unload.js" />
    <script xlink:href="../res/2.0/scripts/yui/yahoo-dom-event/yahoo-dom-event.js" />
    <script xlink:href="../res/2.0/scripts/dom-utils.js" />
    <script xlink:href="../res/2.0/scripts/yui/animation/animation-min.js" />
    <script xlink:href="../res/2.0/scripts/animation/animate.js" />
    <script xlink:href="svg-wow.js" />
    <defs>
        <g id="spoke">
            <rect x="-30" y="-5" width="60" height="10" />
            <circle cx="-30" cy="0" r="6.5" />
            <circle cx="30" cy="0" r="6.5" />
        </g>

        <g id="spokeCluster">
            <use xlink:href="#spoke" />
            <use xlink:href="#spoke" transform="rotate(45)"/>
            <use xlink:href="#spoke" transform="rotate(90)"/>
            <use xlink:href="#spoke" transform="rotate(135)"/>
        </g>
       <g id="message">
            <text id="svg-wow.org" transform="translate(-500,0)">svg-wow.org</text>
            <text id="coming" transform="translate(500,0)" y="1.1em" fill="white">coming soon</text>
       </g>

        <clipPath id="viewportClip">
            <rect width="120" height="140" x="-50" y="-50"/>
        </clipPath>

       <linearGradient id="whiteCast" x1="0" y1="0" x2="1" y2="0"
                       gradientUnits="objectBoundingBox">
            <stop offset="0" stop-color="white" />
            <stop offset="0.1" stop-color="white" stop-opacity="0" />
            <stop offset="0.9" stop-color="white" stop-opacity="0" />
            <stop offset="1" stop-color="white" />
       </linearGradient>

       <linearGradient id="subtle" x1="0" y1="0" x2="1" y2="1"
                       gradientUnits="objectBoundingBox">
            <stop offset="0" stop-color="gray" />
            <stop offset="0.1" stop-color="white" stop-opacity="0" />
            <stop offset="0.9" stop-color="white" stop-opacity="0" />
            <stop offset="1" stop-color="gray" />
       </linearGradient>
    </defs>

    <g id="content" clip-path="url(#viewportClip)">

        <g id="logo" transform="translate(0,0)">
            <use xlink:href="#spokeCluster" fill="black" stroke="black" stroke-width="6"/>
            <use xlink:href="#spokeCluster" fill="rgb(247, 171, 21)"/>
        </g>

        <g stroke-width="3" fill="rgb(247, 171, 21)" stroke="black">
            <rect id="exclamationBar" x="50" y="-35" width="13" height="50" rx="5"
              transform="translate(0,-200)"/>
            <circle id="exclamationPoint" cx="56.5" cy="30" r="7.5"
              transform="translate(0,-200)"/>
        </g>

        <g font-family="'American Typewriter', Georgia, 'Times New Roman', serif" font-size="14"
           font-weight="bold" text-anchor="middle"
           transform="translate(11.5,60)" stroke-linejoin="round"
           stroke-linecap="round">
           <use xlink:href="#message" stroke="black" fill="black" stroke-width="5" />
           <use xlink:href="#message" fill="rgb(247,171,21)" />
        </g>

        <rect x="-50" y="-50" width="130" height="140" fill="url(#whiteCast)" display="none"/>
    </g>

	<g id="bracket">
            <rect x="-60" y="-60" rx="1" ry="1" 
              width="120" height="120" fill="none" stroke="url(#subtle)"/>
            <rect x="-60" y="-60" rx="1" ry="1" transform="scale(-1,1)" 
              width="120" height="120" fill="none" stroke="url(#subtle)"/>
        </g>
</svg>

</svg>
/animations/svg-wow.js
/** =======================================================================
 *  Vincent Hardy
 *  License terms: see svg-wow.org
 *  CC0 http://creativecommons.org/publicdomain/zero/1.0/
 *  ======================================================================= */
sw.animations = sw.animations || {};

/**
 * Trying to go crazy on using scripted animations :-)
 */
sw.animations.SVGWowComingSoon = function () {
    this.initialize();
}

sw.animations.SVGWowComingSoon.prototype = {
    /**
     * Initializes the set of animation effects.
     */
    initialize : function () {
        var Animate = sw.animation.Animate;
        var reducedMotion = window.matchMedia &&
            window.matchMedia("(prefers-reduced-motion: reduce)").matches;

        var strongElasticOut = function (t, b, c, d, a, p) {
            if (p === undefined) {
                p = d * .2;
            }
            return yui.util.Easing.elasticOut(t, b, c, d, a, p);
        };
        // With reduced motion the pieces glide in without the elastic bounce.
        var softOut = reducedMotion ? yui.util.Easing.easeOut : yui.util.Easing.elasticOut;
        if (reducedMotion) {
            strongElasticOut = yui.util.Easing.easeOut;
        }

        var startAnim = new Animate("logo",
                        {txf: {from: {tx: 12}, to: {tx: 0}, template: "translate(#tx,0)"}},
                        1.2, softOut);
        var animBracket = new Animate("bracket",
                        {txf: {from: {sx: 1, sy: 1}, to: {sx: 4, sy: 4}, template: "scale(#sx,#sy)"},
                 sy: {from: 1, to: 4}, "stroke-width": {from: 1, to: 0.25}},
                        0.5, undefined, "scale(#sx,#sy)");
    
        var animBar = new Animate("exclamationBar",
                        {txf: {from: {ty: -200}, to: {ty: 0}, template: "translate(0,#ty)"}},
                        1.6, softOut);

        var animPoint = new Animate("exclamationPoint",
                        {txf: {from: {ty: -250}, to: {ty: 0}, template: "translate(0,#ty)"}},
                        2, strongElasticOut);

        var svgWowLine = new Animate("svg-wow.org",
                        {txf: {from: {tx: -120}, to: {tx: 0}, template: "translate(#tx,0)"}},
                        2, softOut);
        var comingLine = new Animate("coming",
                        {txf: {from: {tx: 120}, to: {tx: 0}, template: "translate(#tx,0)"}},
                        2, softOut);

        yui.util.Event.addListener(document, "click", startAnim.getStartHandler());

        // Keyboard: Enter or space plays the animation.
        document.addEventListener("keydown", function (evt) {
            if (evt.key === "Enter" || evt.key === " ") {
                evt.preventDefault();
                startAnim.animate();
            }
        }, false);

        // Show the bracket again so that the animation can be replayed.
        startAnim.onBegin(function () {
            document.getElementById("bracket").setAttribute("display", "inline");
        });

        startAnim.onBegin(animBar);
        startAnim.onBegin(animPoint);
        startAnim.onBegin(svgWowLine);
        startAnim.onBegin(comingLine);
        startAnim.onBegin(animBracket);

        animBracket.onEnd(function () {
            document.getElementById("bracket").setAttribute("display", "none");
        });
    }
};
/res/2.0/scripts/animation/animate.js
/** =======================================================================
 *  Vincent Hardy
 *  License terms: see svg-wow.org
 *  CC0 http://creativecommons.org/publicdomain/zero/1.0/
 *  ======================================================================= */

(function () {

sw.animation = sw.animation || {};

yui = YAHOO;

var Anim = yui.util.Anim;

/**
 * Object with the names of the transform pseudo attributes.
 */
var PSEUDO_ATTRIBUTES = {
    tx: "#tx",
    ty: "#ty",
    sx: "#sx",
    sy: "#sy",
    r: "#r"
};

var PSEUDO_DEFAULTS = {
    tx: 0,
    ty: 0,
    sx: 1,
    sy: 1,
    r: 0
};

var PSEUDO_ATTRIBUTES_ARRAY = ["tx", "ty", "sx", "sy", "r"];
var N_PSEUDO_ATTRIBUTES = PSEUDO_ATTRIBUTES_ARRAY.length;

/**
 * Simple extension of the YUI animation classes to handle SVG transforms.
 *
 * The extension handles the "r", "sx", "sy", "tx" and "ty" pseudo attributes
 * by manipulating the transform of the target element using a template.
 *
 * @param p_el the element that will be animated
 * @param p_attributes the attributes to animate. This animation class adds the
 *        "r" (rotation), "tx", "ty", "sx" and "sy" (scale) pseudo-attributes.
 * @param p_oTransformTemplate the template for the transform attribute to
 *        animate. By default, it is
 *        "translate(#tx,#ty) scale(#sx,#sy) rotate(#r)"
 *        but it can be set to any string.
 * @param p_oDuration the animation duration
 * @param p_oMethod easing method
 *
 * @see http://developer.yahoo.com/yui/docs/YAHOO.util.Anim.html
 */
sw.animation.Animate = function (p_el, p_attributes,
                                 p_oDuration,
                                 p_oMethod) {
    sw.animation.Animate.superclass.constructor.call(this, p_el,
            p_attributes, p_oDuration, p_oMethod);

    var el = sw.stringToElement(p_el);

    var _endCallbacks = [];
    var _endAnimations = [];
    var _beginCallbacks = [];
    var _beginAnimations = [];

    this._endCallbacks = _endCallbacks;
    this._endAnimations = _endAnimations;
    this._beginCallbacks = _beginCallbacks;
    this._beginAnimations = _beginAnimations;

    this.onComplete.subscribe(function () {
        for (var i = 0; i < _endCallbacks.length; i++) {
            _endCallbacks[i]();
        }

        for (i = 0; i < _endAnimations.length; i++) {
            _endAnimations[i].animate();
        }
    });

    this.onStart.subscribe(function () {
        for (var i = 0; i < _beginCallbacks.length; i++) {
            _beginCallbacks[i]();
        }

        for (i = 0; i < _beginAnimations.length; i++) {
            _beginAnimations[i].animate();
        }
    });
};


yui.lang.extend(sw.animation.Animate,
                Anim, {



    /**
     * Sets the new attribute value
     *
     * @param p_attr the attribute name
     * @param p_val the attribute value
     * @param p_unit the unit
     */
    setAttribute : function (p_attr, p_val, p_unit) {
        var el = this.getEl();
        if (attributeHandlers[p_attr] &&
            attributeHandlers[p_attr].setAttribute) {
            return attributeHandlers[p_attr].setAttribute.call(this, el, p_val);
        } else {
            // Let the base class handle the attributes
            sw.animation.Animate.superclass.setAttribute.call(this,
                    p_attr, p_val, p_unit);
        }
    },

    /**
     * Gets the attribute value
     *
     * @param p_attr the attribute name
     */
    getAttribute : function (p_attr) {
        if (attributeHandlers[p_attr] &&
            attributeHandlers[p_attr].getAttribute) {
            return attributeHandlers[p_attr].getAttribute.call(this, this.getEl());
        } else {
            // Let the base class handle the attribute
            return sw.animation.Animate.superclass.
                getAttribute.call(this, p_attr);
        }
    },

    /**
     * Overrides the default implementation to check if there is an attribute
     * handler for the attribute.
     * @param {String} attr The name of the attribute.
     * @param {Number} start The value this attribute should start from for this animation.
     * @param {Number} end  The value this attribute should end at for this animation.
     * @return The Value to be applied to the attribute.
     */
    doMethod : function (attr, start, end) {
        var val;

        if (attributeHandlers[attr] &&
            attributeHandlers[attr].interpolate) {
            var p = this.method(this.currentFrame, 0, 100, this.totalFrames) / 100;
            val = attributeHandlers[attr].interpolate.call(this, start, end, p);
        } else {
            val = Anim.prototype.doMethod.call(this, attr, start, end);
        }

        return val;
    },

    /**
     * Implementation helper: applies the current transform.
     * Note that this quite costly, from a computation perspective, so
     * it should be use conservatively.
     */
    applyTransform : function () {
        var txf = this.transformTemplate;
        var el = this.getEl();
        var attrs = PSEUDO_ATTRIBUTES;
        var a = PSEUDO_ATTRIBUTES_ARRAY;
        var n = a.length;
        var p;

        for (var i = 0; i < n; i++) {
            p = a[i];
            txf = txf.replace(attrs[p], el[p]);
        }

        el.setAttribute("transform", txf);
    },

    /**
     * Returns a handler that will start this animation instance when invoked.
     */
    getStartHandler : function () {
        var that = this;
        if (this._startHandler === undefined) {
            this._startHandler = function () {
                that.animate();
            };
        }

        return this._startHandler;
    },

    /**
     * Adds a function of animation to call or play when this animation ends
     *
     * @param p_animOrCallback an animation or function to trigger when this
     *        animation ends
     */
    onEnd : function (p_animOrCallback) {
        if (typeof p_animOrCallback === "function") {
            this._endCallbacks.push(p_animOrCallback);
        } else if (p_animOrCallback !== null &&
                   p_animOrCallback !== undefined &&
                   typeof p_animOrCallback.animate === "function") {
            this._endAnimations.push(p_animOrCallback);
        }
    },

    /**
     * Adds a function of animation to call or play when this animation begins
     *
     * @param p_animOrCallback an animation or function to trigger when this
     *        animation begins
     */
    onBegin : function (p_animOrCallback) {
        if (typeof p_animOrCallback === "function") {
            this._beginCallbacks.push(p_animOrCallback);
        } else if (p_animOrCallback !== null &&
                   p_animOrCallback !== undefined &&
                   typeof p_animOrCallback.animate === "function") {
            this._beginAnimations.push(p_animOrCallback);
        }
    }
});

/**
 * Class static
 */
var attributeHandlers = {};

/**
 * Adds a new way to handle a given attribute. The handler should have the
 * following:
 *
 * - setAttribute. A funtion taking the element and value as parameters.
 * - getAttribute. A function taking the element as parameter and
 *   returning the attribute value.
 * - interpolate. A function taking the start value, end value, current
 *   progress in time interval and interpolation method as a parameter. It
 *   should return the value to apply to the animation target.
 *
 * @param p_attributeName the name of the attribute to which the handler
 *        applies.
 * @param p_attributeHandler an object holding the setAttribute, getAttribute
 *        and interpolate methods
 */
sw.animation.Animate.addAttributeHandler = function (p_attributeName,
                                                     p_attributeHandler) {
    attributeHandlers[p_attributeName] = p_attributeHandler;
};

// =============================================================================
// Generic handler for the SVG transform attribute
// =============================================================================
sw.animation.Animate.addAttributeHandler("txf", {
    setAttribute: function (e, v) {
        var txf = this.attributes.txf.template, val, p;
        if (txf) {
            for (var p in v) {
                if (v.hasOwnProperty(p)) {
                    val = v[p];
                    txf = txf.replace(PSEUDO_ATTRIBUTES[p], val);
                    e[p] = val;
                }
            }

            e.setAttribute("transform", txf);
        }
    },

    getAttribute: function (e) {
        var result = {
            tx: 0,
            ty: 0,
            sx: 1,
            sy: 1,
            r: 0
        };
        for (var i = 0; i < N_PSEUDO_ATTRIBUTES; i++) {
            a = PSEUDO_ATTRIBUTES_ARRAY[i];
            if (e[a] !== undefined) {
                result[a] = e[a];
            } else {
                e[a] = result[a];
            }
        }
        return result;
    },

    interpolate: function (start, end, p) {
        var s, e, a, result = {}, elt;
        for (var i = 0; i < N_PSEUDO_ATTRIBUTES; i++) {
            a = PSEUDO_ATTRIBUTES_ARRAY[i];
            s = start[a];
            e = end[a];

            if (s === undefined) {
                elt = this.getEl();
                if (elt[a] !== undefined) {
                    s = elt[a];
                }
                if (s === undefined) {
                    s = PSEUDO_DEFAULTS[a];
                }

                start[a] = s;
            }

            if (e !== undefined) {
                result[a] = p * e + (1 - p) * s;
            } else {
                result[a] = s;
            } 
        }
        return result;
    }
});

})();