Pngy - Request Animation Frame

Animation class for PNG Sprites

by Ken Z

HTML

<body>
    <div id="mainContent">
        <div id="pngy"></div>
        <div id="controls">
             <h1>Controls</h1>

            <div class="button" onclick="sploosh.play()">Play</div>
            <div class="button" onclick="sploosh.pause()">Pause</div>
            <div class="button" onclick="sploosh.reverse()">Reverse</div>
            <div class="button" onclick="sploosh.reset()">Reset</div>
            <div class="button" onclick="sploosh.gotoAndPlay(15)">Go to and play</div>
            <div class="button" onclick="sploosh.gotoAndStop(15)">Go to and stop</div>
            <div class="button" onclick="sploosh.kill()">Kill clip (requires a refresh)</div>
            <div class="clear"></div>
             <h1>Sequences</h1>

             <h2>Play</h2>

            <div class="button" onclick="sploosh.playSequence('jump')">Jump</div>
            <div class="button" onclick="sploosh.playSequence('blink')">Blink</div>
            <div class="button" onclick="sploosh.playSequence('idle')">Idle</div>
            <div class="button" onclick="sploosh.playSequence('look')">Look</div>
            <div class="button" onclick="sploosh.playSequence('scared')">Scared</div>
            <div class="clear"></div>
             <h2>Play and loop</h2>

            <div class="button" onclick="sploosh.playSequence('jump',true)">Jump</div>
            <div class="button" onclick="sploosh.playSequence('blink',true)">Blink</div>
            <div class="button" onclick="sploosh.playSequence('idle',true)">Idle</div>
            <div class="button" onclick="sploosh.playSequence('look',true)">Look</div>
            <div class="button" onclick="sploosh.playSequence('scared',true)">Scared</div>
            <div class="clear"></div>
             <h1>Information</h1>

            <label for="currentFrame">Current frame</label>
            <input id="currentFrame" />
            <label for="frameRate">Change frame rate</label>
            <input id="frameRate" value="30"...

CSS

html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
 article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after, q:before, q:after {
    content:'';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}
html {
    width: 100%;
    height: 100%;
}
body {
    background: #eee;
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
}
h1 {
    font: bold 20px/20px Arial, san-serif;
    margin: 20px 0 0;
}
h2 {
    font: normal 14px/14px Arial, sans-serif;
    margin: 10px 0 5px;
}
hr {
    margin: 10px 0 20px;
}
#mainContent {
    width: 100%;
    max-width: 1000px;
    margin: 20px auto;
    overflow: auto;
}
#pngy {
    width: 100px;
    height: 212px;
    position: fixed;
}
#controls {
    float: left;
    height: 100%;
    border-left: 1px solid #ccc;
    padding: 10px;
    margin: 0 0 0 140px;
    overflow: auto;
    min-width: 100px;
}
.callbackDiv {
    width: 150px;
    overflow: auto;
    float: left;
    margin: 0 0 0 5px;
}
textarea {
    width: 95%;
    height: 100px;
    resize: none;
}
label {
    font: bold 12px/12px Arial, sans-serif;
    margin: 0 5px 0 10px;
}
#currentFrame, #frameRate {
    width:30px;
}
.button {
    padding:...

JavaScript

/*

 PNG Sprite class for having animated pngs

 Developed by: Bret Kruse ([email protected]) bretalankruse.com

 This code does not claim to be perfect, but it does what I need it to do. I plan to continue improving and optimizing as I get time.

 Copyright (c) 2014 Bret Kruse

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
 to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 */

function Pngy(params) {

    /* Get the item from the DOM */
    var node = document.querySelectorAll(params.node)[0];

    var self = this,
        frameRate = 30,
        frameColumn = 0,
        frameRow = 0,

        width,
        height,
        fullWidth,
        fullHeight,
        animationLoop = false,
        horizFrames,
        vertFrames,
        dir = 1,
        fullLooping = false,
        curFrame = 0,
        currentAnimation = null,
        totalFrames = params.totalFrames,
        img = params.img,
        events = params.events,
        sequences = params.sequences,

    // for controlling the FPS
        now,
        then = Date.now(),
       ...