在HTML5上绘制多个精灵图像
Drawing multiple sprite images on the HTML5
目前我已经把一些函数放在一个类中。所以对于我来说,将多个精灵图像存储在一个数组中会更容易。然而,是否有一种方法,我可以手动绘制它们,而不是我通过数组输出设置它,它自动绘制?下面是我的代码:
<!DOCTYPE html>
<html>
<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 ;
var frameWidth, frameHeight ;
var frameScale = 0.4 ;
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];
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();
context.translate(this.xPos, this.yPos);
var speedXSign = this.speedX > 0 ? 1 : -1;
var speedYSign = this.speedY > 0 ? 1 : -1;
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() {
requestAnimationFrame(animate);
context.clearRect(0, 0, canvas.width, canvas.height);
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 ;
for (var i = 0; i < 3; i++) {
var anotherFish = new Fish(xPos, yPos, speedX, speedY, imgFish, frameWidth, frameHeight);
fishes.push(anotherFish);
anotherFish.drawFish();
}
animate();
}
function polyFillRAFNow() {
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>
手动添加鱼:
HTML:<button id="button" > Add a fish! </button>
JS:
document.getElementById("button").onclick = function() {
var anotherFish = new Fish(xPos, yPos, speedX, speedY, imgFish, frameWidth, frameHeight);
fishes.push(anotherFish);
anotherFish.drawFish();
animate();
};
你的代码在那里,我只是复制它并把它放在一个even listener
相关文章:
- 如何使用Pixijs获取在加载的精灵表资产中定义的图像
- ExtJS:“图像”类型的精灵:如何更改像素
- 如何在JS中缩放精灵图像
- 当使用HTTP/2时,缩小和连接JS/CSS文件,以及使用精灵进行图像处理仍然可以提供性能优势
- 是否可以更改我的 css 精灵用 javascript 显示的子图像
- 精灵图像的背景位置在 IE 中不起作用,用于倒计时时钟
- 画布绘制按分区背景精灵绘制的图像
- 图像精灵作为网页的背景
- 使用单击图像映射更改设置为背景的精灵图像的位置
- kineticJS 为精灵创建 ImageHitRegion,而不是图像
- 是否可以显示/隐藏画布图像的一部分,例如图像精灵
- 画架精灵使用多个图像进行动画
- 如何在保留现有过渡的同时切换到精灵图像
- 在相位器上显示带有间隔的精灵图像
- 精灵图像的随机移动位置
- 精灵图像的起始方向不同
- jQuery UI跳跃效果与CSS精灵图像
- 在HTML5上绘制多个精灵图像
- 用css中的变量控制精灵图像坐标
- 当尝试用html5 canvas动画一个精灵图像时,奇怪的行为