介绍
本系列博客将主要介绍如今大红大紫的移动Web应用程序开发最重要的三个工具:HTML5,JavaScript, CSS3。
本篇是HTML5介绍的第三篇,主要介绍HTML5的Canvas API。
相关文章:
移动Web应用程序开发 HTML5篇 (一) HTML5简介
移动Web应用程序开发 HTML5篇 (二) 新功能介绍和测试
•1. Canvas API介绍
<canvas>是HTML5引入的一个新的元素,可以使用JavaScript来绘制图形、合成图象、以及做一些动画。<canvas>最先出现在Apple Mac OS X Dashboard中,也被应用于Safari,随着后来基于Gecko1.8的浏览器Firefox 1.5的加入变得流行起来,目前<canvas>是HTML 5标准规范的一部分。目前最新版本的主流浏览器都支持<canvas>,支持情况如下图:
•2. 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轴延伸。坐标图如下所示:
再看一个例子:
01 <html>
02  
03  <head>
04  
05   <script type="application/x-javascript">
06  
07     function draw() {
08  
09       var canvas = document.getElementById("canvas");
10  
11       if (canvas.getContext) {
12  
13         var ctx = canvas.getContext("2d");
14  
15  
16         ctx.fillStyle = "rgb(200,0,0)";
17  
18         ctx.fillRect (10, 10, 55, 50);
19  
20  
21         ctx.fillStyle = "rgba(0, 0, 200, 0.5)";
22  
23         ctx.fillRect (30, 30, 55, 50);
24  
25       }
26  
27     }
28  
29   </script>
30  
31  </head>
32  
33  <body onload="draw();">
34  
35    <canvas id="canvas" width="150" height="150"></canvas>
36  
37  </body>
38  
39 </html>

 
运行效果如下图所示,分别创建了两个色块,对其进行颜色赋值和透明度设置:
相关代码 下载
•3. Canvas 动画
Canvas的动画功能是Flash的克星之一,结合了JavaScript和CSS3,可以说任何Flash能够做出的动画在Canvas中都可以实现。Canvas动画的原理和Flash类似:通过移动相应活动的图形来展示动画
如果是一个2D的canvas动画,那么在JavaScript中需要新建以下Object, 本例来源于KineticJS 2d JavaScript Library:
01     this.canvas = document.getElementById(canvasId);
02  
03     this.context = this.canvas.getContext("2d");
04  
05     this.drawStage = undefined;
06  
07     this.listening = false;
08  
09  
10     // Canvas Events
11  
12     this.mousePos = null;
13  
14     this.mouseDown = false;
15  
16     this.mouseUp = false;
17  
18  
19     // Region Events
20  
21     this.currentRegion = null;
22  
23     this.regionCounter = 0;
24  
25     this.lastRegionIndex = null;
26  
27  
28     // Animation
29  
30     this.t = 0;
31  
32     this.timeInterval = 0;
33  
34     this.startTime = 0;
35  
36     this.lastTime = 0;
37  
38     this.frame = 0;
39  
40 this.animating = false;

其动画部分代码:
01 Kinetic_2d.prototype.isAnimating = function(){
02  
03     return this.animating;
04  
05 };
06  
07  
08 Kinetic_2d.prototype.getFrame = function(){
09  
10     return this.frame;
11  
12 };
13  
14 Kinetic_2d.prototype.startAnimation = function(){
15  
16     this.animating = true;
17  
18     var date = new Date();
19  
20     this.startTime = date.getTime();
21  
22     this.lastTime = this.startTime;
23  
24  
25     if (this.drawStage !== undefined) {
26  
27         this.drawStage();
28  
29     }
30  
31  
32     this.animationLoop();
33  
34 };
35  
36 Kinetic_2d.prototype.stopAnimation = function(){
37  
38     this.animating = false;
39  
40 };
41  
42 Kinetic_2d.prototype.getTimeInterval = function(){
43  
44     return this.timeInterval;
45  
46 };
47  
48 Kinetic_2d.prototype.getTime = function(){
49  
50     return this.t;
51  
52 };
53  
54 Kinetic_2d.prototype.getFps = function(){
55  
56     return this.timeInterval > 0 ? 1000 / this.timeInterval : 0;
57  
58 };
59  
60 Kinetic_2d.prototype.animationLoop = function(){
61  
62     var that = this;
63  
64  
65     this.frame++;
66  
67     var date = new Date();
68  
69     var thisTime = date.getTime();
70  
71     this.timeInterval = thisTime - this.lastTime;
72  
73     this.t += this.timeInterval;
74  
75     this.lastTime = thisTime;
76  
77  
78     if (this.drawStage !== undefined) {
79  
80         this.drawStage();
81  
82     }
83  
84  
85     if (this.animating) {
86  
87         requestAnimFrame(function(){
88  
89             that.animationLoop();
90  
91         });
92  
93     }
94  
95 };

读者可以从以下地址下载源码进行测试学习:下载地址。
更多的例子,
包括Canvas 3D动画,下载地址,Canvas动画是HTML5游戏开发最重要的工具,更多关于Canvas的信息将会和接下来的CSS3一起介绍。
本篇完。
参考资料:"Pro. HTML5 Programing" http://www.kineticjs.com/ ...

转载于:https://www.cnblogs.com/webapplee/p/3771675.html

[HTML5]移动Web应用程序开发 HTML5篇 (四) 多媒体API相关推荐

  1. 移动Web应用程序开发HTML5篇

    https://software.intel.com/zh-cn/blogs/2012/03/09/webhtml5-offline-web-applications 转载于:https://www. ...

  2. 第一章 Web应用程序开发基础

    一.HTTP协议工作机制 HTTP协议(HyperText Transfer Protocol,超文本传输协议)是用于从WWW服务器传输超文本到本地浏览器的传送协议.它是一种主流B/S架构中应用的通信 ...

  3. 【JavaScript UI库和框架】上海道宁与Webix为您提供用于跨平台Web应用程序开发的JS框架及UI小部件

    Webix是Javascript库 一种软件产品 用于加速Web开发的 JavaScript UI库和框架 Webix用于跨平台Web应用程序开发的JS框架,为您提供102个UI小部件和功能丰富的CS ...

  4. 【使用Blazor构建web应用程序 .NET 6篇 上】

    Build web applications with Blazor 使用Blazor构建web应用程序 .NET 6篇 上 使用Blazor构建web应用程序 第一节Introduction (介绍 ...

  5. 【使用Blazor构建web应用程序 .NET 6篇 中】

    Build web applications with Blazor 使用Blazor构建web应用程序 .NET 6篇 中 使用Blazor构建web应用程序 第五节Exercise - Acces ...

  6. 【微信小程序开发学习篇】

    微信小程序开发学习篇 概述 相关信息 笔记制作时间:2022-9-25 参考视频:黑马视频 参考文档:微信小程序官方开发文档 文章目录 微信小程序开发学习篇 概述 相关信息 小程序基础 1.数据绑定与 ...

  7. ASP.NET Core Web 应用程序开发期间部署到IIS自定义主机域名并附加到进程调试

    想必大家之前在进行ASP.NET Web 应用程序开发期间都有用到过将我们的网站部署到IIS自定义主机域名并附加到进程进行调试. 那我们的ASP.NET Core Web 应用程序又是如何部署到我们的 ...

  8. Visual C#.Net网络程序开发-Tcp篇(1)

    Visual C#.Net 网络程序开发-Socket篇 Visual C#.Net网络程序开发-Tcp篇(1) Visual C#.Net网络程序开发-Tcp篇(2) Visual C#.Net网络 ...

  9. [渝粤教育] 商丘职业技术学院 Java Web应用程序开发 参考 资料

    教育 -Java Web应用程序开发-章节资料考试资料-商丘职业技术学院[] 简述B/S结构和C/S结构,各自的优缺点? web前端基础 第1单元 网上书店系统开发准备 1.[单选题]主流的动态网页技 ...

最新文章

  1. 佐治亚理工学院发文:不要迷信可解释性,小心被误导
  2. SpringMVC背景介绍及常见MVC框架比较
  3. SelectBox插件
  4. 关于C#(ASP.net)存取MySQL LongText字段的心得[转]
  5. 【Python】扫盲帖:关于在Windows、Linux和Mac上安装设置Python的问题
  6. 当滑动时隐藏Actionbar
  7. ubuntu切换JDK版本
  8. python格式字符_python格式字符
  9. 计算机主机组成部分和功能,电脑的组成部分及作用
  10. ectouch第六讲 之表常用链接
  11. “技术需求”与“技术成果”项目之间关联度计算模型top1
  12. 技术选型和知识点介绍(下)
  13. word表格三线表线宽度
  14. pandas 空值填充
  15. 按键精灵手机助手之以图找图
  16. 高级计算机器,高级计算器最新版
  17. 如何将瀚高数据库单机数据导入HGDW
  18. 华为畅享8plus停产了吗_华为畅享8和Plus哪个好? 华为畅享8 Plus与畅享8区别对比评测...
  19. 1419:SPFA(II)
  20. pythonwhile嵌套if_python中for、while循环、if嵌套的使用

热门文章

  1. PDA端的数据库一般采用的是sqlce数据库
  2. Android Jetpack组件之数据库Room详解(二)
  3. 【jQuery Demo】图片由下至上逐渐显示
  4. 码农和程序员的几个重要区别!
  5. 第9章 使用ssh服务管理远程主机。
  6. C/C++ 如何劫持别人家的命令||函数||程序(只能对于window而言)
  7. 2012网页设计趋势(下)
  8. ad从2003升级到2008总结
  9. 域嵌套太深_pyspark如何修改嵌套结构域
  10. 在Java里怎将字节数转换为我们可以读懂的格式?