gpt4 book ai didi

JavaScript 动画速度

转载 作者:行者123 更新时间:2023-11-28 08:21:34 25 4
gpt4 key购买 nike

我窃取了下面的代码,试图弄清楚如何构建类似的东西。但是我不知道在哪里可以调整动画的速度。 IE。此代码适用于从左到右移动的图像,但移动速度太慢。

<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 中,您还缺少 Canvas 宽度和高度元素的撇号:

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

});

查看 Updated JsFiddle example

为了进一步探索 HTML5 Canvas 的可能性,我建议阅读 html5canvastutorials 中的教程。

关于JavaScript 动画速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22894880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com