介绍
本系列博客将主要介绍如今大红大紫的移动Web应用程序开发最重要的三个工具:HTML5,JavaScript, CSS3。
本篇是HTML5介绍的第三篇,主要介绍HTML5的Canvas API。
相关文章:
移动Web应用程序开发 HTML5篇 (一) HTML5简介
移动Web应用程序开发 HTML5篇 (二) 新功能介绍和测试
<canvas>是HTML5引入的一个新的元素,可以使用JavaScript来绘制图形、合成图象、以及做一些动画。<canvas>最先出现在Apple Mac OS X Dashboard中,也被应用于Safari,随着后来基于Gecko1.8的浏览器Firefox 1.5的加入变得流行起来,目前
<canvas>是HTML 5标准规范的一部分。目前最新版本的主流浏览器都支持<canvas>,支持情况如下图:
一个简单的Canvas例子,代码如下:
[xhtml]<!DOCTYPE HTML>
<html>
<canvas id="diagonal" style="border: 1px solid;" width="200" height="200">
</canvas>
</html>
<script>
function drawDiagonal() {
// Get the canvas element and its drawing context
var canvas = document.getElementById('diagonal');
var context = canvas.getContext('2d');
// Create a path in absolute coordinates
context.beginPath();
context.moveTo(100, 140);
context.lineTo(140, 70);
// Stroke the line onto the canvas
context.stroke();
}
window.addEventListener("load", drawDiagonal, true);
</script>[/xhtml]
代码运行效果如图,通过绝对坐标画出一条线:
Canvas把整个画布用坐标值进行了表示,左上角为(0,0),依次沿着X轴和Y轴延伸。坐标图如下所示:
再看一个例子:
05 | <script type="application/x-javascript"> |
09 | var canvas = document.getElementById("canvas"); |
11 | if (canvas.getContext) { |
13 | var ctx = canvas.getContext("2d"); |
16 | ctx.fillStyle = "rgb(200,0,0)"; |
18 | ctx.fillRect (10, 10, 55, 50); |
21 | ctx.fillStyle = "rgba(0, 0, 200, 0.5)"; |
23 | ctx.fillRect (30, 30, 55, 50); |
33 | <body onload="draw();"> |
35 | <canvas id="canvas" width="150" height="150"></canvas> |
运行效果如下图所示,分别创建了两个色块,对其进行颜色赋值和透明度设置:
相关代码 下载
•3. Canvas 动画
Canvas的动画功能是Flash的克星之一,结合了JavaScript和CSS3,可以说任何Flash能够做出的动画在Canvas中都可以实现。Canvas动画的原理和Flash类似:通过移动相应活动的图形来展示动画
如果是一个2D的canvas动画,那么在JavaScript中需要新建以下Object, 本例来源于KineticJS 2d JavaScript Library:
01 | this.canvas = document.getElementById(canvasId); |
03 | this.context = this.canvas.getContext("2d"); |
05 | this.drawStage = undefined; |
07 | this.listening = false; |
14 | this.mouseDown = false; |
21 | this.currentRegion = null; |
23 | this.regionCounter = 0; |
25 | this.lastRegionIndex = null; |
32 | this.timeInterval = 0; |
40 | this.animating = false; |
其动画部分代码:
01 | Kinetic_2d.prototype.isAnimating = function(){ |
03 | return this.animating; |
08 | Kinetic_2d.prototype.getFrame = function(){ |
14 | Kinetic_2d.prototype.startAnimation = function(){ |
16 | this.animating = true; |
18 | var date = new Date(); |
20 | this.startTime = date.getTime(); |
22 | this.lastTime = this.startTime; |
25 | if (this.drawStage !== undefined) { |
36 | Kinetic_2d.prototype.stopAnimation = function(){ |
38 | this.animating = false; |
42 | Kinetic_2d.prototype.getTimeInterval = function(){ |
44 | return this.timeInterval; |
48 | Kinetic_2d.prototype.getTime = function(){ |
54 | Kinetic_2d.prototype.getFps = function(){ |
56 | return this.timeInterval > 0 ? 1000 / this.timeInterval : 0; |
60 | Kinetic_2d.prototype.animationLoop = function(){ |
67 | var date = new Date(); |
69 | var thisTime = date.getTime(); |
71 | this.timeInterval = thisTime - this.lastTime; |
73 | this.t += this.timeInterval; |
75 | this.lastTime = thisTime; |
78 | if (this.drawStage !== undefined) { |
87 | requestAnimFrame(function(){ |
读者可以从以下地址下载源码进行测试学习:下载地址。
更多的例子,

包括Canvas 3D动画,下载地址,Canvas动画是HTML5游戏开发最重要的工具,更多关于Canvas的信息将会和接下来的CSS3一起介绍。
本篇完。
参考资料:"Pro. HTML5 Programing" http://www.kineticjs.com/ ...