現在這幾篇寫的都是比較基礎的東西,有過相應開發經驗的朋友可直接忽略啦。
計算機模擬的動畫都是由很多靜態的一連串影像(sprite)在一定幀率(fps)內逐幀播放出來的。
對於js來說,我們可以用提供的定時器(setInterval或setTimeout,兩者其實都可以),制造一個逐幀播放的舞台現場(stage),每一幀清空畫布后再把所有的精靈全畫上去,下一幀再清再畫,只要改變精靈的相應屬性就能產生動畫的效果。所以我們對於精靈(Sprite)類來說,要提供一個畫(draw)的方法,一個進入每一幀時(onenter)要執行的方法;對於舞台(stage)對象,就提供往舞台添加和刪除精靈(addChild/removeChild)的方法,定時器的相關操作方法(start,stop,step)。再來看,其實Sprite也是可以添加精靈達到一樣的層次關系的,所以也依然可以提供addChild,removeChild的方法,所以我們可以把Sprite做為基類,stage對象繼承了Sprite的方法后可根據需要重寫和添加自己的方法
Sprite類
根據實際需要實現了其他一些方法。在js/lib/目錄下新建一個Sprite.js文件(不了解文件目錄結構請查看上一篇介紹文章,如果是讀者自己規划的文件結構那還是按自己的方法來放吧):
this.Sprite = __class__({ x: 0, // 精靈的中心點在stage上的x坐標 y: 0, // 精靈的中心點在stage上的y坐標 width: 0, // 寬 height: 0, // 高
alpha: 1, // 透明度 0 ~ 1 scaleX: 1, // X軸上的縮放比 scaleY: 1, // Y軸上的縮放比 rotation: 0, // 旋轉角度 visible: true, // 可見否 DEG_TO_RAD: Math.PI / 180, // 一角度多少弧度,常量 __init__: function () { this._object = [] // 存放精靈 },
// 把精靈畫到畫布前的修正,如把x,y修正為sprite的中心點坐標,畫的時候不會以左上角為錨點 __beforedraw__: function (ctx) { ctx.translate(-this.width/2, -this.height/2) if (this.scaleX !== 1 || this.scaleY !== 1) ctx.scale(this.scaleX, this.scaleY) if (this.rotation % 360 !== 0) ctx.rotate(this.DEG_TO_RAD * this.rotation) if (this.alpha !== 1) ctx.globalAlpha = this.alpha this.draw(ctx) // 真正調用畫的方法 this._drawall(ctx) // 把所有的子精靈都 畫出來 }, draw: function (ctx) { // 現階段版本要自己實現畫的方法 // implement your own draw method }, _drawall: function (ctx) { if (this.size() > 0) for (var i = 0, obj; obj = this._object[i]; i++) { this.onenter() if (obj.visible) { // 精靈可見才畫,不可見它與它的子精靈都不會畫到stage上 ctx.save() obj.__beforedraw__(ctx) ctx.restore() } } },
// 進入每幀時調用,如果有做逐幀動畫的需求可以實現(就是重寫)該方法 onenter: function () { // implement your own onenter method }, addChild: function (obj) { return this.addChildAt(this.size(), obj) }, removeChild: function (obj) { this.removeChildAt(this.indexOf(obj)) return this },
// 獲取指定索引的子精靈 child: function (index) { return this._object[index] },
// 添加精靈到指定位置 addChildAt: function (index, obj) { if (this._object.indexOf(obj) === -1) this._object.splice(index, 0, obj) return this },
// 刪除指定位置的精靈,如果存在 removeChildAt: function (index) { if (this.child(index) !== void 0) this._object.splice(index, 1) return this },
// 獲取指定精靈的索引 indexOf: function (obj) { return this._object.indexOf(obj) },
// 獲取包含的精靈個數 size: function () { return this._object.length },
// 移除所有精靈 clear: function () { this._object.length = 0 return this } })
stage
舞台管理對象,之所以不用寫成一個類是基本不會反復生成多個舞台實例,一個界面一個canvas就夠了。上面也提到stage具有的很多方法Sprite類都有,所以我們可以繼承Sprite類,又因為只用做成一個對象,所以直接實例化Sprite類就OK了,然后在這個實例中添加或重寫方法:
js/lib/stage.js
this.stage = new Sprite()
// 拷貝方法到stage對象中 __copy__({ // 初始化stage,傳入一個canvas dom對象 init: function (canvas) { this.canvas = canvas this.context = canvas.getContext('2d') this.width = canvas.width this.height = canvas.height return this }, // 運行,參數fps為幀率,默認每秒24幀 run: function (fps) { fps = Number(fps) || 24 this._interval = Math.round(1000 / fps) this._starttimer() this.running = true return this }, // 停止運行 stop: function () { this.running = false this._stoptimer() return this }, running: false, __timer: null, _starttimer: function () { if (!this.__timer) this.__timer = setInterval(function (obj) { obj._drawall() }, this._interval, this) }, _stoptimer: function () { if (this.__timer) { clearInterval(this.__timer) this.__timer = null } }, // override _drawall: function () { if (this.size()) { // 清空畫布 this.context.clearRect(0, 0, this.width, this.height) Sprite.prototype._drawall.call(this, this.context) } } }, stage)
這兩個東西都實現了,現在嘗試來使用它們。
其實還有一個大前提,就是你已經基本了解 html5 canvas的API,如果這個條件不滿足可以先去補習一下下,很簡單的幾個東西。
感慨web前端這塊好象沒有特別智能如 android eclipse和ios xcode這樣的ide,對文件的引用什么的都不是很友好,或者是我寡聞了,除了靜態語言其他時間全是vi的可悲啊……先來調整一下之前定義的東西,在js/lib/sys.js里再加一個工具吧,路徑添加和獲取的對象,稍稍方法我們用來加載js文件:
this.__path__ = function () { var arr = [] return { // 添加路徑 add: function () { for (var i = 0, str; str = arguments[i]; ++i) arr.push(str) }, // 獲取所有路徑,供__load__使用 list: function () { return arr.slice() } } }()
修改js/etc/mtm_module.js文件吧,不用再為每個目錄定義一個數組了
__path__.add( 'js/mtm/Test.js', 'js/mtm/OtherTest.js' )
個性一個js/bin/main.js里開頭調用__load__方法的參數為:
// __path__.list() 可獲取所添加的要加載的路徑 __load__(__path__.list(), function () {})
因為現在在Lib下加了兩個文件 (Sprite.js,stage.js),以后要是再加其他類文件 的話都要手動去寫標簽太麻煩(個人感覺),在js/etc/目錄下新建一個lib_module.js文件
__path__.add( 'js/lib/Sprite.js', 'js/lib/stage.js' )
痛苦的是還要在Index.html里用標簽引用lib_module.js ……
好了先來初始化stage,在main.js中加載完文件的回調函數里寫:
__load__(__path__.list(), function () { var cvs = document.createElement('CANVAS'), doc = document.documentElement cvs.width = doc.clientWidth cvs.height = doc.clientHeight document.body.appendChild(cvs) stage.init(cvs).run() // 初始化並運行 })
我們在js/mtm/Ball.js里寫一個例子,Ball類:
var Ball = __class__(Sprite, { __init__: function (color, radius) { Sprite.call(this) this.color = color || 'red' this.radius = radius || 20 }, draw: function (ctx) { ctx.fillStyle = this.color ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true) ctx.fill() } })
使用這個Ball類只要生成實例,並添加到stage就OK了,在js/bin/main.js的 初始化stage完后試試吧.
var ball = new Ball() // 設置ball的位置在stage中央 ball.x = stage.width / 2 ball.y = stage.height / 2 stage.addChild(ball)
用瀏覽器打開Index.html頁面就看到一個紅色圓球在瀏覽器中心點上了,如果一路進來都沒寫錯什么東西的話。
工具都基本准備完了,下去就開始一個個實現精彩的例子(有些例子沒必要就不實現了)。