成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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 13:07:04

HTML5

2012-05-18 14:05:53

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

華為

2021-03-26 07:06:40

Windows 10Windows操作系統

2012-04-24 09:48:49

HTML5

2012-06-07 15:29:31

HTML5

2012-05-15 13:57:41

HTML5

2012-01-10 16:37:46

樂團

2019-09-12 10:10:10

Vim編輯器代碼

2012-03-29 09:18:47

HTML5WEB

2020-04-22 10:01:26

Vim編輯器代碼

2013-08-27 14:20:09

游戲應用圖標ASO應用商店優化

2012-05-30 13:49:52

HTML5

2014-12-30 17:13:51

HTML5

2021-03-29 15:07:19

AI 數據人工智能
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产视频1区 | 久久精点视频 | 亚洲免费在线 | 亚洲福利在线观看 | 中文在线播放 | 国产精品1区 | 国产精品av久久久久久久久久 | 日韩中文字幕一区二区 | 免费人成激情视频在线观看冫 | 国产精品福利网站 | h片在线播放 | 久久精点视频 | 成人精品视频免费 | 久久久久久久久久久国产 | 性做久久久久久免费观看欧美 | 黄色一级大片在线免费看产 | 欧美激情精品久久久久久 | 久久精品免费观看 | 国产专区在线 | 日本三级电影在线免费观看 | 亚洲一区 中文字幕 | 超碰520 | 一道本不卡 | 亚洲精品免费在线观看 | 一区二区中文字幕 | 国产精品久久av | 欧美一级视频免费看 | 国产高清免费视频 | 亚洲国产成人精品久久 | 精品国产乱码一区二区三区a | 精品国产不卡一区二区三区 | 中日韩毛片| 欧美freesex黑人又粗又大 | 男人的天堂中文字幕 | 亚洲欧美一区二区三区国产精品 | 国内久久 | 亚洲精品国产电影 | 国产精品成人品 | 人人澡人人爱 | 日韩欧美综合 | 日韩视频一区在线观看 |