Raphael JS图像动画表演
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有使用 Raphael 的最佳功能,即为您创建的对象设置属性的能力,就像 DOM 一样。您正在重新实例化蜜蜂并在每一步中清除纸张。这就是使用canvas标签的方式,它是如此繁琐且容易出错,让浏览器担心要重新绘制什么。
做你正在做的事情的更好方法如下
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