HTML5 Canvas 逐幀動畫的實現
阿新 • • 發佈:2019-01-27
和C++遊戲開發相同,HTML5逐幀動畫需要的影象元素也是一張繪製了每一幀影象效果的圖片。通過迴圈繪製各幀的影象來實現動畫的效果。
本示例中演示的是一個小人,預設狀態下,小人朝右方站立;按下左/右方向鍵的時候,小人朝左/右方奔跑(在畫布中沒有位移);鬆開按鍵後保持奔跑的方向站立。
其中,向左或向右站立分別是一張6幀的圖片,向左或向右奔跑分別是一張12幀的圖片。
程式碼如下:
HTML程式碼:
- <canvasid="canvas"width="600"height="400">
-
<p>Your browser does not support the canvas element!
- </canvas>
以下這段程式碼已經在本人的博文中多次重用,所以就不解釋了。
[javascript] view plain copy- Array.prototype.remove = function(obj) {
- for (i inthis) {
- if (this[i] === obj) {
- this.splice(i, 1);
- }
- }
- }
-
function BasicObject(x, y, order) {
- this.x = x;
- this.y = y;
- this.order = isNaN(order) ? 0 : order;
- this.addTo = function(list) {
- list.push(this);
- list.sort(function(a, b) {return a.order - b.order;});
- }
- this.removeFrom = function(list) {
- list.remove(this);
- }
- }
- function FrameAnimationObject(x, y, order, image, frame) {
- BasicObject.call(this, x, y, order);
- this.image = image;
- this.frame = frame;
- this.currentFrame = 0;
- this.draw = function(context) {
- var sw = this.image.width / this.frame;
- var sx = this.currentFrame * sw;
- context.drawImage(this.image, sx, 0, sw, this.image.height, this.x, this.y, sw, this.image.height);
- this.currentFrame++;
- this.currentFrame = (this.currentFrame >= this.frame) ? 0 : this.currentFrame;
- }
- }
- FrameAnimationObject.prototype = new BasicObject();
奔跑小人的類,繼承自逐幀動畫基礎物件,指定了需求使用的影象資源,並添加了響應鍵盤事件的方法
[javascript] view plain copy- function Person(x, y, order) {
- FrameAnimationObject.call(this, x, y, order);
- this.image = new Image();
- this.image.src = "stop_right.png"
- this.frame = 6;
- this.onkeydown = function(event) {
- if (event.keyCode == 37) {
- this.image.src = "run_left.png";
- this.frame = 12;
- }
- elseif (event.keyCode == 39) {
- this.image.src = "run_right.png";
- this.frame = 12;
- }
- this.currentFrame = (this.currentFrame >= this.frame) ? 0 : this.currentFrame;
- }
- this.onkeyup = function(event) {
- if (event.keyCode == 37) {
- this.image.src = "stop_left.png";
- }
- elseif (event.keyCode == 39) {
- this.image.src = "stop_right.png";
- }
- this.frame = 6;
- this.currentFrame = (this.currentFrame >= this.frame) ? 0 : this.currentFrame;
- }
- }
- Person.prototype = new FrameAnimationObject();
動畫引擎類以及程式入口
[javascript] view plain copy- function Engin() {
- var canvas = document.getElementById("canvas");
- var context = canvas.getContext("2d");
- var buffer = document.createElement("canvas");
- buffer.width = canvas.width;
- buffer.height = canvas.height;
- var bufferCtx = buffer.getContext("2d");
- var objs = new Array();
- const FPS = 20;
- this.manage = function() {
- bufferCtx.clearRect(0, 0, buffer.width, buffer.height);
- context.clearRect(0, 0, canvas.width, canvas.height);
- for (x in objs) {
- if (objs[x].update) {
- objs[x].update(objs);
- }
- }
- for (x in objs) {
- if (objs[x].draw) {
- objs[x].draw(bufferCtx);
- }
- }
- context.drawImage(buffer, 0, 0);
- }
- document.onkeydown = function(event) {
- for (x in objs) {
- if (objs[x].onkeydown) {
- objs[x].onkeydown(event);
- }
- }
- }
- document.onkeyup = function(event) {
- for (x in objs) {
- if (objs[x].onkeyup) {
- objs[x].onkeyup(event);
- }
- }
- }
- this.run = function() {
- var p = new Person(canvas.width / 2, canvas.height / 2);
- p.addTo(objs);
- setInterval(this.manage, 1000 / FPS);
- }
- }
- window.onload = function() {
- new Engin().run();
- }
需要說明的是,本次將鍵盤事件的響應放到了動畫物件的類中來實現,並在引擎類中通過設定document的鍵盤事件來引用,這昂做事為了依照上一篇博文總所說的將動畫物件的邏輯操作封裝在最外層,同時避免了引擎類的過分膨脹。當動畫物件逐漸增多時,效果更加明顯。