你還在抱怨自己寫的canvas demo徘徊在10幀以下嗎?你還在煩惱打開自己寫的應(yīng)用就聽見CUP風(fēng)扇轉(zhuǎn)嗎?你正在寫一個(gè)javascript Canvas庫(kù)嗎?那么下面九點(diǎn)就是你必須知道的! 一.預(yù)渲染 錯(cuò)誤代碼: var canvas = document.getElementById( "myCanvas" ); var cont
你還在抱怨自己寫的canvas demo徘徊在10幀以下嗎?你還在煩惱打開自己寫的應(yīng)用就聽見CUP風(fēng)扇轉(zhuǎn)嗎?你正在寫一個(gè)javascript Canvas庫(kù)嗎?那么下面九點(diǎn)就是你必須知道的!
錯(cuò)誤代碼:
var canvas = document.getElementById("myCanvas"); var context = this.canvas.getContext('2d'); var drawAsync = eval(Jscex.compile("async", function () { while (true) { drawMario(context); $await(Jscex.Async.sleep(1000)); } })) drawAsync().start();
正確代碼:
var canvas = document.getElementById("myCanvas"); var context = this.canvas.getContext('2d'); var m_canvas = document.createElement('canvas'); m_canvas.width = 64; m_canvas.height = 64; var m_context = m_canvas.getContext('2d'); drawMario(m_context); var drawAsync = eval(Jscex.compile("async", function () { while (true) { context.drawImage(m_canvas, 0, 0); $await(Jscex.Async.sleep(1000)); } })) drawAsync().start();
這里m_canvas的寬度和高度控制得越小越好。
錯(cuò)誤代碼:
for (var i = 0; i < points.length - 1; i++) { var p1 = points[i]; var p2 = points[i + 1]; context.beginPath(); context.moveTo(p1.x, p1.y); context.lineTo(p2.x, p2.y); context.stroke(); }
正確代碼:
context.beginPath(); for (var i = 0; i < points.length - 1; i++) { var p1 = points[i]; var p2 = points[i + 1]; context.moveTo(p1.x, p1.y); context.lineTo(p2.x, p2.y); } context.stroke();
錯(cuò)誤代碼:
for (var i = 0; i < STRIPES; i++) { context.fillStyle = (i % 2 ? COLOR1 : COLOR2); context.fillRect(i * GAP, 0, GAP, 480); }
正確代碼:
context.fillStyle = COLOR1; for (var i = 0; i < STRIPES / 2; i++) { context.fillRect((i * 2) * GAP, 0, GAP, 480); } context.fillStyle = COLOR2; for (var i = 0; i < STRIPES / 2; i++) { context.fillRect((i * 2 + 1) * GAP, 0, GAP, 480); }
錯(cuò)誤代碼:
context.fillRect(0, 0, canvas.width, canvas.height);
正確代碼:
context.fillRect(20, 20, 100, 100);
context.shadowOffsetX = 5; context.shadowOffsetY = 5; context.shadowBlur = 4; context.shadowColor = 'rgba(255, 0, 0, 0.5)'; context.fillRect(20, 20, 150, 100);
詳細(xì)性能差別:
http://simonsarris.com/blog/346-how-you-clear-your-canvas-matters
一般情況下:clearRect的性能優(yōu)于fillRect優(yōu)于canvas.width = canvas.width;
幾種取整數(shù)的方法:
rounded = (0.5 + somenum) | 0; rounded = ~ ~(0.5 + somenum); rounded = (0.5 + somenum) << 0;
(function () {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
window.cancelAnimationFrame =
window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame'];
}if (!window.requestAnimationFrame)
window.requestAnimationFrame = function (callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function () { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function (id) {
clearTimeout(id);
};
} ());
與渲染無關(guān)的計(jì)算交給worker
復(fù)雜的計(jì)算交給引擎(自己寫,或者用開源的),比如3D、物理
緩存load好的圖片,canvas上畫canvas,而不是畫image
本文已同步更新至:
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com