Raphael JS图像动画表演

发布于 2024-10-09 01:02:43 字数 1257 浏览 9 评论 0原文

我正在尝试使用 Raphael JS 创建图像动画。

我想要蜜蜂在页面上随机移动的效果,我有一个工作示例,但它有点“紧张”,我在控制台中收到此警告:

“资源解释为图像,但使用 MIME 类型文本传输/html”

我不确定该警告是否导致了“紧张”运动,或者只是我使用数学处理它的方式。

如果有人有更好的方法来创建效果或改进,请告诉我。

我在这里有一个在线演示

,这是我的javascript代码:

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);
}

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柳若烟 2024-10-16 01:02:43

您没有使用 Raphael 的最佳功能,即为您创建的对象设置属性的能力,就像 DOM 一样。您正在重新实例化蜜蜂并在每一步中清除纸张。这就是使用canvas标签的方式,它是如此繁琐且容易出错,让浏览器担心要重新绘制什么。

做你正在做的事情的更好方法如下

/**
 * 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();
    );

}

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();
    );

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文