michael
michael

Reputation: 1202

Raphael JS image animation performance

I'm trying to create an image animation using Raphael JS.

I want the effect of a bee moving randomly across the page, I've got a working example but it's a bit "jittery", and I'm getting this warning in the console:

"Resource interpreted as image but transferred with MIME type text/html"

I'm not sure if the warning is causing the "jittery" movement or its just the way I approached it using maths.

If anyone has a better way to create the effect, or improvements please let me know.

I have a demo online here

and heres my javascript code:

function random(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;  
} 

function BEE(x, y, scale) {
    this.x = x;
    this.y = y;
    this.s = scale;
    this.paper = Raphael("head", 915, 250);

    this.draw = function() {
        this.paper.clear();
        this.paper.image("bee.png", this.x, this.y, 159*this.s, 217*this.s);
    }

    this.update = function() {
        var deg = random(-25, 25);

        var newX = Math.cos(Raphael.rad(deg)) * 2;
        var newY = Math.sin(Raphael.rad(deg)) * 2;

        this.x += newX;
        this.y += newY;

        if( this.x > 915) {
            this.x = 0;
        }
        if( this.y > 250 || this.y < 0 ) {
            this.y = 125;
        }
    }
}

$(document).ready(function() {

    var bee = new BEE(100, 150, 0.4);
    var timer = setInterval(function(){
        bee.draw();
        bee.update();
    }, 15);
}

Upvotes: 2

Views: 4108

Answers (1)

Ruan Mendes
Ruan Mendes

Reputation: 92304

You are not using Raphael's best feature, the ability to just set attributes on objects you create, just like the DOM. You are re-instantiating the bee and clearing the paper at every step. This is how you would do it with the canvas tag, and it's so tedious error-prone, let the browser worry about what to repaint.

A better way to do what you are doing is the following

/**
 * I don't like closure object orientation, but if you're
 * going to use it, use it all the way
 * (instead of using this.x and this.y for private variables)
 */
function Bee(paper, x, y, scale) 
{

    // The Raphael img object for the bee)
    var img = paper.image("bee.png", x, y, 159 * scale, 217 * scale);

    var timerId = null;

    // Allows access to 'this' within closures
    var me = this;

    this.draw = function() {
      img.attr({x: x, y: y});
    }

    this.update = function() {
        var deg = random(-25, 25);

        var newX = Math.cos(Raphael.rad(deg)) * 2;
        var newY = Math.sin(Raphael.rad(deg)) * 2;

        x += newX;
        y += newY;

        if( x > 915) {
            x = 0;
        }
        if( y > 250 || y < 0 ) {
            y = 125;
        }
    }

    this.fly = function() {
        timerId = setInterval({
            me.update();
            me.draw();
        }, 15);
    }

    this.stop = function() {
      clearInterval(timerId);
      timerId = null;
    } 

    function random(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;  
    } 
}

$(document).ready(function() {
    var paper = Raphael("head", 915, 250);
    var bees = [ new Bee(paper, 100, 150, 0.4), new Bee(paper, 50, 10, 0.2) ];
    bees[0].fly();
    bees[1].fly();
    $(document).click(
        bees[0].stop();
        bees[1].stop();
    );

}

Upvotes: 7

Related Questions