当前位置:Gxlcms > html代码 > HTML5游戏框架cnGameJS开发实录-基本图形模块篇

HTML5游戏框架cnGameJS开发实录-基本图形模块篇

时间:2021-07-01 10:21:17 帮助过:17人阅读

1.功能

  该模块也很简单,主要包括三个基础图形的绘制:矩形 圆形 文字。我们把一个个图像以构造函数的模式封装,例如当我们需要绘制一个矩形对象,我们首先new出一个矩形对象,再调用对象的draw方法进行绘制。例如:

  1. var rect=new cnGame.shape.Rect();
  2. rect.draw();

2.实现

  该模块包括三个图形对象,因此我们建立三个构造函数,它们分别有自己的各种方法,包括绘制,移动,旋转,尺寸调整等等,由于三个对象的方法有较多相似,我们以矩形对象为例进行解释,首先看构造函数:

  1. /**
  2. *矩形对象
  3. **/
  4. var rect=function(options){
  5. if(!(this instanceof arguments.callee)){
  6. return new arguments.callee(options);
  7. }
  8. this.init(options);
  9. }

  需要注意的是,该函数如果不是以构造函数方式调用,则会return new 构造函数,使函数始终以构造函数方式调用,返回生成的矩形对象。之后调用init进行初始化。

另外虽然每个对象有不同的属性,但是我们也应该为对象设定默认对象。这里就需要使用到之前在core模块extend函数,使用户设定的参数和默认对象的参数融合:

  1. /**
  2. *默认值对象
  3. **/
  4. var defaultObj={
  5. x:0,
  6. y:0,
  7. width:100,
  8. height:100
  9. };
  10. options=options||{};
  11. options=cg.core.extend(defaultObj,options);

  对于矩形,有一个特别之处是它有四个顶点,因此我们可以在保存x,y坐标之余,保存right顶点,和bottom顶点,方便以后矩形碰撞的检测,该函数也很简单,就是根据宽高和xy计算right和bottom:

  1. /**
  2. *更新right和bottom
  3. **/
  4. var resetRightBottom=function(elem){
  5. elem.right=elem.x+elem.width;
  6. elem.bottom=elem.y+elem.height;
  7. }

  当矩形都有了它的位置和尺寸参数后,我们就可以 根据之前的参数把它绘制出来(分别有填充和描边两种模式):

  1. /**
  2. *绘制矩形
  3. **/
  4. draw:function(style,isFill){
  5. var context=cg.context;
  6. (cg.core.isUndefined(isFill))&&(isFill=true);
  7. if(isFill){
  8. context.fillStyle = style;
  9. context.fillRect(this.x, this.y, this.width, this.height);
  10. }
  11. else{
  12. context.strokeStyle = style;
  13. context.strokeRect(this.x, this.y, this.width, this.height);
  14. }
  15. return this;
  16. }

  另外,为了方便开发或测试,对象也提供其他各种改变自己参数的方法:

  1.move:使矩形移动一定距离

  2.moveTo:使矩形移动到特定距离

  3.resize:改变矩形一定尺寸

  4.resizeTo:把矩形改变到特定尺寸

  这些方法最后都return this;使方法都支持链式调用。

  该模块也比较简单,就不再详述。最后给出该模块所有的源码:

  1. /**
  2. *
  3. *canvas基本形状对象
  4. *
  5. **/
  6. cnGame.register("cnGame.shape",function(cg){
  7. /**
  8. *更新right和bottom
  9. **/
  10. var resetRightBottom=function(elem){
  11. elem.right=elem.x+elem.width;
  12. elem.bottom=elem.y+elem.height;
  13. }
  14. /**
  15. *矩形对象
  16. **/
  17. var rect=function(options){
  18. if(!(this instanceof arguments.callee)){
  19. return new arguments.callee(options);
  20. }
  21. this.init(options);
  22. }
  23. rect.prototype={
  24. /**
  25. *初始化
  26. **/
  27. init:function(options){
  28. /**
  29. *默认值对象
  30. **/
  31. var defaultObj={
  32. x:0,
  33. y:0,
  34. width:100,
  35. height:100,
  36. style:"red",
  37. isFill:true
  38. };
  39. options=options||{};
  40. options=cg.core.extend(defaultObj,options);
  41. this.setOptions(options);
  42. resetRightBottom(this);
  43. },
  44. /**
  45. *绘制矩形
  46. **/
  47. setOptions:function(options){
  48. this.x=options.x;
  49. this.y=options.y;
  50. this.width=options.width;
  51. this.height=options.height;
  52. this.style=options.style;
  53. this.isFill=this.isFill;
  54. },
  55. /**
  56. *绘制矩形
  57. **/
  58. draw:function(){
  59. var context=cg.context;
  60. if(this.isFill){
  61. context.fillStyle = this.style;
  62. context.fillRect(this.x, this.y, this.width, this.height);
  63. }
  64. else{
  65. context.strokeStyle = this.style;
  66. context.strokeRect(this.x, this.y, this.width, this.height);
  67. }
  68. return this;
  69. },
  70. /**
  71. *将矩形移动一定距离
  72. **/
  73. move:function(dx,dy){
  74. dx=dx||0;
  75. dy=dy||0;
  76. this.x+=dx;
  77. this.y+=dy;
  78. resetRightBottom(this);
  79. return this;
  80. },
  81. /**
  82. *将矩形移动到特定位置
  83. **/
  84. moveTo:function(x,y){
  85. x=x||this.x;
  86. y=y||this.y;
  87. this.x=x;
  88. this.y=y;
  89. resetRightBottom(this);
  90. return this;
  91. },
  92. /**
  93. *将矩形改变一定大小
  94. **/
  95. resize:function(dWidth,dHeight){
  96. dWidth=dWidth||0;
  97. dHeight=dHeight||0;
  98. this.width+=dWidth;
  99. this.height+=dHeight;
  100. resetRightBottom(this);
  101. return this;
  102. },
  103. /**
  104. *将矩形改变到特定大小
  105. **/
  106. resizeTo:function(width,height){
  107. width=width||this.width;
  108. height=height||this.height;
  109. this.width=width;
  110. this.height=height;
  111. resetRightBottom(this);
  112. return this;
  113. }
  114. }
  115. /**
  116. *圆形对象
  117. **/
  118. var circle=function(options){
  119. if(!(this instanceof arguments.callee)){
  120. return new arguments.callee(options);
  121. }
  122. this.init(options);
  123. }
  124. circle.prototype={
  125. /**
  126. *初始化
  127. **/
  128. init:function(options){
  129. /**
  130. *默认值对象
  131. **/
  132. var defaultObj={
  133. x:100,
  134. y:100,
  135. r:100,
  136. startAngle:0,
  137. endAngle:Math.PI*2,
  138. antiClock:false,
  139. style:"red",
  140. isFill:true
  141. };
  142. options=options||{};
  143. options=cg.core.extend(defaultObj,options);
  144. this.setOptions(options);
  145. },
  146. /**
  147. *设置参数
  148. **/
  149. setOptions=function(options){
  150. this.x=options.x;
  151. this.y=options.y;
  152. this.r=options.r;
  153. this.startAngle=options.startAngle;
  154. this.endAngle=options.endAngle;
  155. this.antiClock=options.antiClock;
  156. this.isFill=options.isFill;
  157. this.style=options.style;
  158. },
  159. /**
  160. *绘制圆形
  161. **/
  162. draw:function(){
  163. var context=cg.context;
  164. context.beginPath();
  165. context.arc(this.x,this.y,this.r,this.startAngle,this.endAngle,this.antiClock);
  166. context.closePath();
  167. if(this.isFill){
  168. context.fillStyle=this.style;
  169. context.fill();
  170. }
  171. else{
  172. context.strokeStyle=this.style;
  173. context.stroke();
  174. }
  175. },
  176. /**
  177. *将圆形移动一定距离
  178. **/
  179. move:function(dx,dy){
  180. dx=dx||0;
  181. dy=dy||0;
  182. this.x+=dx;
  183. this.y+=dy;
  184. return this;
  185. },
  186. /**
  187. *将圆形移动到特定位置
  188. **/
  189. moveTo:function(x,y){
  190. x=x||this.x;
  191. y=y||this.y;
  192. this.x=x;
  193. this.y=y;
  194. return this;
  195. },
  196. /**
  197. *将圆形改变一定大小
  198. **/
  199. resize:function(dr){
  200. dr=dr||0;
  201. this.r+=dr;
  202. return this;
  203. },
  204. /**
  205. *将圆形改变到特定大小
  206. **/
  207. resizeTo:function(r){
  208. r=r||this.r;
  209. this.r=r;
  210. return this;
  211. }
  212. }
  213. /**
  214. *将圆形改变到特定大小
  215. **/
  216. var text=function(text,options){
  217. if(!(this instanceof arguments.callee)){
  218. return new arguments.callee(text,options);
  219. }
  220. this.init(text,options);
  221. }
  222. text.prototype={
  223. /**
  224. *初始化
  225. **/
  226. init:function(text,options){
  227. /**
  228. *默认值对象
  229. **/
  230. var defaultObj={
  231. x:100,
  232. y:100,
  233. style:"red",
  234. isFill:true
  235. };
  236. options=options||{};
  237. options=cg.core.extend(defaultObj,options);
  238. this.setOptions(options);
  239. this.text=text;
  240. },
  241. /**
  242. *绘制
  243. **/
  244. draw:function(){
  245. var context=cg.context;
  246. (!cg.core.isUndefined(this.font))&&(context.font=this.font);
  247. (!cg.core.isUndefined(this.textBaseline))&&(context.textBaseline=this.textBaseline);
  248. (!cg.core.isUndefined(this.textAlign))&&(context.textAlign=this.textAlign);
  249. (!cg.core.isUndefined(this.maxWidth))&&(context.maxWidth=this.maxWidth);
  250. if(this.isFill){
  251. context.fillStyle=this.style;
  252. this.maxWidth?context.fillText(this.text,this.x,this.y,this.maxWidth):context.fillText(this.text,this.x,this.y);
  253. }
  254. else{
  255. context.strokeStyle=this.style;
  256. this.maxWidth?context.strokeText(this.text,this.x,this.y,this.maxWidth):context.strokeText(this.text,this.x,this.y);
  257. }
  258. },
  259. /**
  260. *设置参数
  261. **/
  262. setOptions:function(options){
  263. this.x=options.x||this.x;
  264. this.y=options.y||this.y;
  265. this.maxWidth=options.maxWidth||this.maxWidth;
  266. this.font=options.font||this.font;
  267. this.textBaseline=options.textBaseline||this.textBaseline;
  268. this.textAlign=options.textAlign||this.textAlign;
  269. this.isFill=options.isFill||this.isFill;
  270. this.style=options.style||this.style;
  271. }
  272. }
  273. this.Text=text;
  274. this.Rect=rect;
  275. this.Circle=circle;
  276. });

以上就是HTML5游戏框架cnGameJS开发实录-基本图形模块篇的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行