[英]Javascript animation speed
我偷了下面的代码,试图找出如何构建类似的东西。 但是,我看不到可以调整动画速度的位置。 也就是说,此代码用于图像从左向右移动,但是移动速度太慢。
<canvas id="canvas" width=1200 height=300></canvas>
<script>
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 120);
};
})();
var canvas = document.getElementById("canvas"),
cx = canvas.getContext("2d");
function Card(x,y){
this.x = x || -300;
this.y = y || 0;
this.img=new Image();
this.init=function(){
// this makes myCard available in the img.onload function
// otherwise "this" inside img.onload refers to the img
var self=this;
this.img.onload = function()
{
self.draw();
loop();
}
this.img.src = "f15ourbase.png";
}
this.draw = function(){
cx.drawImage(this.img, this.x, this.y);
}
}
var myCard = new Card(50,50);
myCard.init();
function loop(){
if(myCard.x<canvas.width-0){
requestAnimFrame(loop);
}
cx.clearRect(0, 0, canvas.width, canvas.height);
myCard.x++;
myCard.draw();
}
</script>
更改您的window.setTimeout(callback, 1000 / 120);
返回window.setTimeout(callback, 1000 / 60);
而不是做:
myCard.x++;
例如,将其移动5个像素:
// this increases 5 pixels
myCard.x = myCard.x + 5;
在HTML中,画布的width和height元素也缺少撇号:
<canvas id="canvas" width="1200" height="300"></canvas>
编辑:
我以某种神秘的方式投下了不带注释(?)的投票,由于我讨厌它,我最终制作了一个新代码,该代码试图解决大部分(如果不是全部)我可以在提供的代码作者中找到的问题-为所有相关部分添加了代码注释。
// Animation frame gist
window.requestAnimFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
// Variable declarations
var canvas = document.getElementById("canvas"),
cx = canvas.getContext("2d"),
myCard;
/**
* Card declaration
*
*/
var Card = function(x, y) {
// Values after || is values what orginal post author wanted to use
this.x = x || -300;
this.y = y || 0;
this.img = undefined;
// Image to be used for this card
this.imgSrc = "f15ourbase.png";
};
/**
* Initialize
*
*/
Card.prototype.init = function(callback) {
// self refers to this card
var self = this;
this.img = new Image();
// onload success callback
this.img.onload = function() {
// triggering callback on successfull load
return callback();
};
// onload failure callback
this.img.onerror = function() {
// Returning error message for the caller of this method
return callback("Loading image failed!");
};
// Triggering image loading
this.img.src = this.imgSrc;
};
/**
* Draw this on canvas
*
*/
Card.prototype.draw = function() {
// cx (context) could be also passed, now it is global
cx.drawImage(this.img, this.x, this.y);
};
/**
* Main loop
*
*/
function loop() {
// Clear canvas
cx.clearRect(0, 0, canvas.width, canvas.height);
// If card is still within visible area, we continue loop
if(myCard.x < canvas.width) {
// Draw card
myCard.draw();
// Move card by 5 pixels
myCard.x = myCard.x + 5;
// Schedule next loop
// "return" makes sure that we are not executing lines below this
return requestAnimFrame(loop);
}
// Just for debugging, diplayed when animation done
console.log("Animation finished");
};
// Main program starts - Creating a new card instance
myCard = new Card(50, 50);
// Initializing card
myCard.init(function(err) {
// if error with loading the image of the card, we notify
if(err) {
return alert(err);
}
// no errors, we can do the main loop
loop();
});
为了进一步探索HTML5 canvas的可能性,我建议阅读html5canvastutorials的教程
如果您希望能够更改画布中的动画速度,则应更改window.requestAnimFrame函数。 我已经为这种情况创建了一个jsfiddle (只需尝试更改setTimeOut方法的毫秒数,您将看到:window.setTimeout(callback,100);)
因此,最终代码应为:
window.requestAnimFrame = (function(){
return function( callback ){
window.setTimeout(callback, 100);
};
})();
var canvas = document.getElementById("canvas"),
cx = canvas.getContext("2d");
function Card(x,y){
this.x = x || -300;
this.y = y || 0;
this.img=new Image();
this.init=function(){
// this makes myCard available in the img.onload function
// otherwise "this" inside img.onload refers to the img
var self=this;
this.img.onload = function()
{
self.draw();
loop();
}
this.img.src = "f15ourbase.png";
}
this.draw = function(){
cx.drawImage(this.img, this.x, this.y);
}
}
var myCard = new Card(50,50);
myCard.init();
function loop(){
if(myCard.x<canvas.width-0){
requestAnimFrame(loop);
}
cx.clearRect(0, 0, canvas.width, canvas.height);
myCard.x++;
myCard.draw();
}
更改
window.setTimeout(callback, 1000 / 120);
至
window.setTimeout(callback, 1000 / 60);
通过每帧增加x值来加快移动速度:
var NumOfPixelsPerFrame = 4;
myCard.x += NumOfPixelsPerFrame ;
为防止卡片滑出视线,请仅在其宽度小于画布宽度的情况下才移动卡片。
if((myCard.x + myCard.width) < canvas.width){
//Put movement code here
}
注意:默认的hotpixel是图像的左上方像素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.