HTML 5游戏制作之五彩连珠(预览)

开发 前端
ctx是canvas的绘制的类型2D的,以后会支持3D,那木,目前基于canvas的绘制都是调用2d context的方法。所以要了解绘制各种图形,得先看看他的api

HTML5推出也有很长一段时间了,一直没有学习过,闲来无事学习开发个游戏吧。  用javascript+canvas编写一个五彩连珠的游戏。

Canvas 画布

标签<canvas id="canvas" ></canvas>,很简单和普通的tag没区别。 关键在于js对他的操作。先看个示例代码:

  1. <canvas id="canvas" height="100" width="100"></canvas>  
  2. <script>  
  3.     var canvas = document.getElementById("canvas");  
  4.     var ctx  = canvas.getContext("2d");  
  5.     ctx.beginPath();  
  6.     ctx.moveTo(50,10);  
  7.     ctx.lineTo(50,90);  
  8.     ctx.moveTo(10,50);  
  9.     ctx.lineTo(90,50);  
  10.       
  11.     ctx.strokeStyle="red";  
  12.     ctx.stroke();  
  13. </script> 

你能看到想到我画的是什么吗?  ctx是canvas的绘制的类型2D的,以后会支持3D,那木,目前基于canvas的绘制都是调用2d context的方法。所以要了解绘制各种图形,得先看看他的api。

  1. interface CanvasRenderingContext2D {  
  2.  
  3.   // back-reference to the canvas  
  4.   readonly attribute HTMLCanvasElement canvas;  
  5.  
  6.   // state  
  7.   void save(); // push state on state stack  
  8.   void restore(); // pop state stack and restore state  
  9.   // transformations (default transform is the identity matrix)  
  10.   void scale(in double x, in double y);  
  11.   void rotate(in double angle);  
  12.   void translate(in double x, in double y);  
  13.   void transform(in double a, in double b, in double c, in double d, in double e, in double f);  
  14.   void setTransform(in double a, in double b, in double c, in double d, in double e, in double f);  
  15.   // compositing  
  16.            attribute double globalAlpha; // (default 1.0)  
  17.            attribute DOMString globalCompositeOperation; // (default source-over)  
  18.   // colors and styles  
  19.            attribute any strokeStyle; // (default black)  
  20.            attribute any fillStyle; // (default black)  
  21.   CanvasGradient createLinearGradient(in double x0, in double y0, in double x1, in double y1);  
  22.   CanvasGradient createRadialGradient(in double x0, in double y0, in double r0, in double x1, in double y1, in double r1);  
  23.   CanvasPattern createPattern(in HTMLImageElement image, in DOMString repetition);  
  24.   CanvasPattern createPattern(in HTMLCanvasElement image, in DOMString repetition);  
  25.   CanvasPattern createPattern(in HTMLVideoElement image, in DOMString repetition);  
  26.  
  27.   // line caps/joins  
  28.            attribute double lineWidth; // (default 1)  
  29.            attribute DOMString lineCap; // "butt", "round", "square" (default "butt")  
  30.            attribute DOMString lineJoin; // "round", "bevel", "miter" (default "miter")  
  31.            attribute double miterLimit; // (default 10)  
  32.  
  33.   // shadows  
  34.            attribute double shadowOffsetX; // (default 0)  
  35.            attribute double shadowOffsetY; // (default 0)  
  36.            attribute double shadowBlur; // (default 0)  
  37.            attribute DOMString shadowColor; // (default transparent black)  
  38.  
  39.   // rects  
  40.   void clearRect(in double x, in double y, in double w, in double h);  
  41.   void fillRect(in double x, in double y, in double w, in double h);  
  42.   void strokeRect(in double x, in double y, in double w, in double h);  
  43.  
  44.   // path API  
  45.   void beginPath();  
  46.   void closePath();  
  47.   void moveTo(in double x, in double y);  
  48.   void lineTo(in double x, in double y);  
  49.   void quadraticCurveTo(in double cpx, in double cpy, in double x, in double y);  
  50.   void bezierCurveTo(in double cp1x, in double cp1y, in double cp2x, in double cp2y, in double x, in double y);  
  51.   void arcTo(in double x1, in double y1, in double x2, in double y2, in double radius);  
  52.   void rect(in double x, in double y, in double w, in double h);  
  53.   void arc(in double x, in double y, in double radius, in double startAngle, in double endAngle, in optional boolean anticlockwise);  
  54.   void fill();  
  55.   void stroke();  
  56.   void clip();  
  57.   boolean isPointInPath(in double x, in double y);  
  58.  
  59.   // Focus management  
  60.   boolean drawFocusRing(in Element element, in optional boolean canDrawCustom);  
  61.  
  62.   // Caret and selection management  
  63.   long caretBlinkRate();  
  64.   boolean setCaretSelectionRect(in Element element, in double x, in double y, in double w, in double h);  
  65.  
  66.   // text  
  67.            attribute DOMString font; // (default 10px sans-serif)  
  68.            attribute DOMString textAlign; // "start", "end", "left", "right", "center" (default: "start")  
  69.            attribute DOMString textBaseline; // "top", "hanging", "middle", "alphabetic", "ideographic", "bottom" (default: "alphabetic")  
  70.   void fillText(in DOMString text, in double x, in double y, in optional double maxWidth);  
  71.   void strokeText(in DOMString text, in double x, in double y, in optional double maxWidth);  
  72.   TextMetrics measureText(in DOMString text);  
  73.  
  74.   // drawing images  
  75.   void drawImage(in HTMLImageElement image, in double dx, in double dy, in optional double dw, in double dh);  
  76.   void drawImage(in HTMLImageElement image, in double sx, in double sy, in double sw, in double sh, in double dx, in double dy, in double dw, in double dh);  
  77.   void drawImage(in HTMLCanvasElement image, in double dx, in double dy, in optional double dw, in double dh);  
  78.   void drawImage(in HTMLCanvasElement image, in double sx, in double sy, in double sw, in double sh, in double dx, in double dy, in double dw, in double dh);  
  79.   void drawImage(in HTMLVideoElement image, in double dx, in double dy, in optional double dw, in double dh);  
  80.   void drawImage(in HTMLVideoElement image, in double sx, in double sy, in double sw, in double sh, in double dx, in double dy, in double dw, in double dh);  
  81.  
  82.   // pixel manipulation  
  83.   ImageData createImageData(in double sw, in double sh);  
  84.   ImageData createImageData(in ImageData imagedata);  
  85.   ImageData getImageData(in double sx, in double sy, in double sw, in double sh);  
  86.   void putImageData(in ImageData imagedata, in double dx, in double dy, in optional double dirtyX, in double dirtyY, in double dirtyWidth, in double dirtyHeight);  
  87. };  
  88.  
  89. interface CanvasGradient {  
  90.   // opaque object  
  91.   void addColorStop(in double offset, in DOMString color);  
  92. };  
  93.  
  94. interface CanvasPattern {  
  95.   // opaque object  
  96. };  
  97.  
  98. interface TextMetrics {  
  99.   readonly attribute double width;  
  100. };  
  101.  
  102. interface ImageData {  
  103.   readonly attribute unsigned long width;  
  104.   readonly attribute unsigned long height;  
  105.   readonly attribute CanvasPixelArray data;  
  106. };  
  107.  
  108. interface CanvasPixelArray {  
  109.   readonly attribute unsigned long length;  
  110.   getter octet (in unsigned long index);  
  111.   setter void (in unsigned long index, in octet value);  
  112. }; 

上面的内容是我粘贴的官方的,一目了然。 

既然我们知道了lineTo和moveTo的功能,那么我们先把游戏的格子棋盘先画出来:

  1. <canvas id="canvas" height="600" width="780" style="border:solid 1px #369;background:#333"></canvas> 
  2. <script> 
  3. var canvas = document.getElementById("canvas");  
  4.  
  5. var ctx  = canvas.getContext("2d");  
  6.  
  7. drawMap();  
  8.  
  9. function drawMap()  
  10. {  
  11.     var start = 10;  
  12.     ctx.beginPath();  
  13.     var cell = 30;  
  14.     var max = cell * 9 + start;  
  15.     //ctx.strokeRect(10,10,max,max);  
  16.     ctx.moveTo(start,start);  
  17.       
  18.     for(var i = 0;i <= 9 ;i++){       
  19.         var p = i * cell + start + 0.5;  
  20.         ctx.lineTo(p,max);  
  21.         ctx.moveTo(p+cell,start);  
  22.     }  
  23.       
  24.     for(var i = 0;i <= 9 ;i++){       
  25.         var p = i * cell + start + 0.5;  
  26.         ctx.moveTo(start,p);  
  27.         ctx.lineTo(max,p);  
  28.     }  
  29.       
  30.     ctx.strokeStyle="#567";  
  31.     ctx.stroke();  
  32. }  
  33.       
  34. </script> 

从运行效果可以看到我们的棋盘是从10像素的位置开始画的,画了个9*9格子的五彩连珠棋盘。

 

今天入门就到这里,下一节讲怎么画一个球。。。

原文链接:http://www.cnblogs.com/mad/archive/2012/03/10/2389519.html

 

 

责任编辑:张伟 来源: 君之蘭的博客
相关推荐

2012-05-17 14:45:34

HTML5

2012-05-18 14:05:53

HTML5

2012-05-18 13:07:04

HTML5

2012-05-18 13:11:09

HTML5

2012-05-18 13:59:45

HTML5

2010-08-12 22:35:24

IBM培训

2011-11-30 15:14:32

HTML 5

2019-09-11 15:20:21

华为

2012-06-07 15:29:31

HTML5

2012-04-24 09:48:49

HTML5

2021-03-26 07:06:40

Windows 10Windows操作系统

2012-05-15 13:57:41

HTML5

2012-01-10 16:37:46

乐团

2012-03-29 09:18:47

HTML5WEB

2020-04-22 10:01:26

Vim编辑器代码

2019-09-12 10:10:10

Vim编辑器代码

2013-08-27 14:20:09

游戏应用图标ASO应用商店优化

2014-12-30 17:13:51

HTML5

2012-05-30 13:49:52

HTML5

2011-12-16 11:11:36

HTML 5
点赞
收藏

51CTO技术栈公众号