canvas教程

HTML5 Canvas编写五彩连珠(2):画图

字号+ 作者:H5之家 来源:H5之家 2015-10-04 16:33 我要评论( )

好吧,新的一天来了,我才开始动笔,真够懒得:)昨天说了今天我们要画一个球,在canvas上。好吧,这是游戏的入门的第一步,只是昨天没写完,所以。。。[html]lt

文章详细内容

HTML5 Canvas编写五彩连珠(2):画图

发布日期:2014年05月16日   来源:PHP1.CN     点击:65467

摘要:好吧,新的一天来了,我才开始动笔,真够懒得:)昨天说了今天我们要画一个球,在canvas上。好吧,这是游戏的入门的第一步,只是昨天没写完,所以。。。[html]<!DOCTYPEhtml><htmlxmlns" Sy...

好吧,新的一天来了,我才开始动笔,真够懒得:)昨天说了今天我们要画一个球,在canvas上。好吧,这是游戏的入门的第一步,只是昨天没写完,所以。。。

[html]
<!DOCTYPE html> <html xmlns=""> <head> <title></title> </head> <body> <canvas id="canvas" height="400" width="600" style="background: #fff;"></canvas> <script type="text/javascript"> var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.beginPath(); ctx.arc(110, 110, 40, 0, Math.PI * 2); ctx.strokeStyle = "red"; ctx.fillStyle = "yellow"; ctx.fill(); ctx.stroke(); </script> </body> </html> 

<!DOCTYPE html> <html xmlns=""> <head> <title></title> </head> <body> <canvas id="canvas" height="400" width="600" style="background: #fff;"></canvas> <script type="text/javascript"> var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.beginPath(); ctx.arc(110, 110, 40, 0, Math.PI * 2); ctx.strokeStyle = "red"; ctx.fillStyle = "yellow"; ctx.fill(); ctx.stroke(); </script> </body> </>
  上面的代码是在VS11 beta上写的,实在是太舒服了,vs是非常强大的编辑器。上面的代码中我们绘制了一个大大的圆,并且着色了,红边和黄心。
看下 arc (弧度)方法,昨天的文章里有他的链接地址,我在这里粘贴下。
 The arc(x, y, radius, startAngle, endAngle, anticlockwise) method draws an arc.
arc(x,y,弧度,开始角度点,结束角度点, 顺时针),角度点你可能不是很清楚如何表示,这里是用Math.PI 圆周率来表示 6.28是周长。 想画圆的一部分也就是一段弧线,可以取开始的角度点和结束的角度点。

   那么下一步就是要把圆能够画到我们棋盘上。其实,这个很简单,只要我们把x,y和radius的值调整下就会绘制出来。我把昨天代码写的更“专业”了一点。所以,今天的代码会在新的代码基础上增加了。先看下改动过的代码。

 

[javascript]
var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var g = { cellCount: 9, lineCount: 5, }; var map = { startX: 20.5, startY: 60.5, cellWidth: 30, getEndX: function () { return g.cellCount * this.cellWidth + this.startX; }, getEndY: function () { return g.cellCount * this.cellWidth + this.startY; }, draw: function () { ctx.beginPath(); ctx.moveTo(this.startX, this.startY); for (var i = 0; i <= g.cellCount; i++) { var p1 = i * this.cellWidth + this.startX; ctx.moveTo(p1, this.startY); ctx.lineTo(p1, this.getEndY()); var p2 = i * this.cellWidth + this.startY; ctx.moveTo(this.startX, p2); ctx.lineTo(this.getEndX(), p2); } ctx.strokeStyle = "#456"; ctx.stroke(); }, }; map.draw(); 

 var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var g = { cellCount: 9, lineCount: 5, }; var map = { startX: 20.5, startY: 60.5, cellWidth: 30, getEndX: function () { return g.cellCount * this.cellWidth + this.startX; }, getEndY: function () { return g.cellCount * this.cellWidth + this.startY; }, draw: function () { ctx.beginPath(); ctx.moveTo(this.startX, this.startY); for (var i = 0; i <= g.cellCount; i++) { var p1 = i * this.cellWidth + this.startX; ctx.moveTo(p1, this.startY); ctx.lineTo(p1, this.getEndY()); var p2 = i * this.cellWidth + this.startY; ctx.moveTo(this.startX, p2); ctx.lineTo(this.getEndX(), p2); } ctx.strokeStyle = "#456"; ctx.stroke(); }, }; map.draw();
是吧,更专业了吧,这样就不会定义一坨的function,到时候没出找,而是定义在一个对象里,这种封装也能避免命名冲突。而且,棋盘起始的位置我也做了调整,只要修改起始的x y值,棋盘就会在这个点开始画。  那,现在我们要在第五行,第六列画一个黄色的球,该怎么画呢?只需要给map增加一个方法,再调用这个方法就行啦

[javascript]
drawBubble: function (x, y) { var px = this.startX + this.cellWidth * x - this.cellWidth / 2; var py = this.startY + this.cellWidth * y - this.cellWidth / 2; ctx.beginPath(); ctx.arc(px, py, 12, 0, Math.PI * 2); ctx.strokeStyle = "white"; ctx.fillStyle = "yellow"; ctx.fill(); ctx.stroke(); }, 

 drawBubble: function (x, y) { var px = this.startX + this.cellWidth * x - this.cellWidth / 2; var py = this.startY + this.cellWidth * y - this.cellWidth / 2; ctx.beginPath(); ctx.arc(px, py, 12, 0, Math.PI * 2); ctx.strokeStyle = "white"; ctx.fillStyle = "yellow"; ctx.fill(); ctx.stroke(); },
画出来刷新下,居然是第六行,第五列,我们搞错了吗?代码没有,只是我们误认为x是行y是列 按顺序叫顺口了,其实是反过来的:)

泡泡既然能画出来,也要能清除才是,不然没法玩,所以再增加一个清除泡泡的方法。
 

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • kphp框架分享:html5知识学习之html5中的canvas元素的简单介绍。

    kphp框架分享:html5知识学习之html5中的canvas元素的简单介绍。

    2017-04-28 17:02

  • 传智播客PHP培训 韩顺平 PHP视频教程 Ajax技术 课堂源代码.zip

    传智播客PHP培训 韩顺平 PHP视频教程 Ajax技术 课堂源代码.zip

    2017-04-26 16:00

  • 传智播客PHP培训 韩忠康 PHP视频教程 Mysql 第34讲 备份还原.wmv

    传智播客PHP培训 韩忠康 PHP视频教程 Mysql 第34讲 备份还原.wmv

    2017-04-24 11:01

  • 基于thinkphp5开发的通用版restful接口框架

    基于thinkphp5开发的通用版restful接口框架

    2017-04-22 14:00

网友点评
=