精灵图像的起始方向不同
Different start off direction for sprite image
目前,当我在画布上绘制精灵图像时,它会被绘制,但它会朝着相同的方向开始。以下是我的代码:
<!DOCTYPE html>
<html>
<button id="button" > Add a fish! </button>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Fish Animation</title>
<style type='text/css'>
canvas {border:1px solid; background-color:#EFEFEF;}
</style>
<script type='text/javascript'>
window.onload=function(){
var canvas = document.getElementById('pondCanvas');
var context = canvas.getContext('2d');
polyFillRAFNow() ;
var frameCount = 8 ; // assumes all strip are on a single line
var frameWidth, frameHeight ; // what count is the frameWidth/frameHeight, not the image's.
var frameScale = 0.4 ; // scale to have a not-too-big fish
// one more level of complexity : we will run through the animation array
// with animIndex to get the frame.
// you can change the indexes like you want. Here I loop with a subtitle
// slow down before looping again.
var animIndex = 0 ;
var animFrames = [0, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7, 6, 5, 4, 4, 3, 2, 2, 1, 1, 1];
// Other change : now xPos, yPos are the CENTER of the fish
var xPos = canvas.width / 2,
yPos = canvas.height / 2;
var speedX;
var speedY;
var speedXSign;
var speedYSign;
var fishes = [];
var anotherFish;
function Fish(xPos, yPos, speedX, speedY, imgFish, frameWidth, frameHeight) {
this.image = imgFish;
this.xPos = canvas.width / 2;
this.yPos = canvas.height / 2;
this.speedX = speedX;
this.speedY = speedY;
this.frameWidth = frameWidth;
this.frameHeight = frameHeight;
this.frameCount = 8;
this.frameScale = 0.4;
this.animIndex = 0;
this.animFrames = [0, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7, 6, 5, 4, 4, 3, 2, 2, 1, 1, 1];
}
Fish.prototype.changeDirection = function () {
speedXSign = this.speedX > 0 ? 1 : -1;
speedYSign = this.speedY > 0 ? 1 : -1;
this.speedX = speedXSign * (1 + Math.random() * 1.7);
this.speedY = speedYSign * (1 + Math.random() * 2);
};
Fish.prototype.move = function () {
this.animIndex++;
if ( this.animIndex == animFrames.length) this.animIndex = 0;
this.xPos += this.speedX;
if ((this.xPos + this.frameWidth * this.frameScale / 1.8) >= canvas.width ||
(this.xPos - this.frameWidth * this.frameScale / 1.8) <= 0) {
this.speedX = -this.speedX;
}
this.yPos += this.speedY;
if ((this.yPos + this.frameHeight * frameScale / 1.8) >= canvas.height ||
(this.yPos - this.frameHeight * frameScale / 1.8) <= 0) {
this.speedY = -this.speedY;
}
};
Fish.prototype.drawFish = function () {
context.save();
// translate in the middle of where we'll draw the fish
context.translate(this.xPos, this.yPos);
// compute speed sign
var speedXSign = this.speedX > 0 ? 1 : -1;
var speedYSign = this.speedY > 0 ? 1 : -1;
// scale to have a mirror effect
// notice that at this point you could rotate rather than scale
// and the drawImage code would be ok.
context.scale(speedXSign, speedYSign);
var frameIndex = animFrames[this.animIndex];
context.drawImage(imgFish, frameIndex * this.frameWidth, 0, frameWidth, frameHeight,
-frameWidth * frameScale / 2, -this.frameHeight * this.frameScale / 2,
this.frameWidth * this.frameScale, this.frameHeight * this.frameScale);
context.restore();
};
function animate() {
// request another animation frame
requestAnimationFrame(animate);
// clear the canvas
context.clearRect(0, 0, canvas.width, canvas.height);
// move and redraw all fish in the fishes[] array
for (var i = 0; i < fishes.length; i++){
var fish = fishes[i];
fish.changeDirection();
fish.move();
fish.drawFish();
}
}
var imgFish = new Image();
imgFish.onload = init;
imgFish.src = 'http://dl.dropbox.com/s/4x14mv75dx7bmi5/FishStrip.png';
function init() {
frameWidth = imgFish.width / frameCount ;
frameHeight = imgFish.height ;
document.getElementById("button").onclick = function() {
// create another fish using the Fish class
var anotherFish = new Fish(xPos, yPos, speedX, speedY, imgFish, frameWidth, frameHeight);
// put this new fish into the fishes[] array
fishes.push(anotherFish) ;
// draw this new fish
anotherFish.drawFish();
}
animate();
}
function polyFillRAFNow() {
// requestAnimationFrame polyfill
var w = window,
foundRequestAnimationFrame = w.requestAnimationFrame || w.webkitRequestAnimationFrame || w.msRequestAnimationFrame || w.mozRequestAnimationFrame || w.oRequestAnimationFrame || function (cb) {
setTimeout(cb, 1000 / 60);
};
window.requestAnimationFrame = foundRequestAnimationFrame;
}
}
</script>
</head>
<body>
<canvas id="pondCanvas" width="1024" height="600">
Canvas is not supported.
</canvas>
</body>
</html>
是否有可能开始在画布的中间,但在不同的方向,每次当我添加一个新的精灵图像?
您可以通过在构造函数中随机设置speedX和speedY的符号来随机化新鱼的初始X,Y方向:
function Fish(xPos, yPos, speedX, speedY, imgFish, frameWidth, frameHeight) {
this.image = imgFish;
this.xPos = canvas.width / 2;
this.yPos = canvas.height / 2;
this.speedX = speedX||(1 + Math.random() * 1.7);
this.speedY = speedY||(1 + Math.random() * 2);
this.frameWidth = frameWidth;
this.frameHeight = frameHeight;
this.frameCount = 8;
this.frameScale = 0.4;
this.animIndex = 0;
this.animFrames = [0, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7, 6, 5, 4, 4, 3, 2, 2, 1, 1, 1];
// randomly change this fishes initial direction
if(Math.random()<0.50){this.speedX*=-1;}
if(Math.random()<0.50){this.speedY*=-1;}
}
相关文章:
- 使用javascript根据图像的布局方向为图像添加不同的类名
- 根据调整大小的方向显示不同的图像
- 上传修复上传时的图像方向
- 在javascript中检测图像的EXIF方向
- 通过单击方向按钮在容器内移动图像
- 基于移动方向的图像高度
- 使用 Jquery 同时向不同方向移动多个图像
- Javascript EXIF 图像方向和图像预览
- 使用readAsDataURL检索原始图像时的图像方向
- 如何在两个方向上旋转图像90
- jQuery悬停在图像上,一个方向运行良好,另一个方向跳动
- CSS/Javascript解决方案,可根据图像方向自动应用高度/宽度属性
- 如何动画图像&从右到左改变方向
- 滚动,在方向改变时翻转图像
- 根据当前选择的图像使用方向键移动灯箱图像
- JQuery图像旋转动画,工作在一个方向,而不是另一个
- 使图像面移动方向html
- 在相反的方向上旋转两个图像
- 在圆形方向上移动图像的
- 用动画方向改变图像