HTML 5深入浅出教学篇之十

开发 前端
本文讲要的讲的是HTML 5画布(canvas)之转换:平移|translate();旋转|rotate();缩放|scale();矩阵转换|transform(a, b, c, d, e, f)

介绍

HTML 5: 画布(canvas)之转换(转换画布的用户坐标系)

平移 | translate()

旋转 | rotate()

缩放 | scale()

矩阵转换 | transform(a, b, c, d, e, f)

矩阵转换 | setTransform(a, b, c, d, e, f)

示例

1、平移 | translate()
canvas/transform/translate.html

  1. <!DOCTYPE HTML> 
  2. <html> 
  3. <head> 
  4.     <title>平移</title> 
  5. </head> 
  6. <body> 
  7.     <canvas id="canvas" width="400" height="400" style="background-color: rgb(222, 222, 222)"> 
  8.         您的浏览器不支持 canvas 标签  
  9.     </canvas> 
  10.     <br /> 
  11.     <button type="button" onclick="drawIt();">不断地点我看 Demo</button> 
  12.     <button type="button" onclick="clearIt();">清除画布</button> 
  13.     <script type="text/javascript"> 
  14.         var ctx = document.getElementById('canvas').getContext('2d');  
  15.         var canvasX = 0;  
  16.         var canvasY = 0;  
  17.         var stepX = 20;  
  18.         var stepY = 20;  
  19.         function drawIt() {  
  20.             if (canvasX == 0 && canvasY == 0)  
  21.                 ctx.strokeRect(0, 0, 100, 100);  
  22.             canvasX += stepX;  
  23.             canvasY += stepY;  
  24.             /*  
  25.              * context.translate(x, y) - 将当前的用户坐标系平移指定的距离  
  26.              *   x - x 轴方向上需要平移的像素数  
  27.              *   y - y 轴方向上需要平移的像素数  
  28.              */  
  29.             ctx.strokeStyle = "blue";  
  30.             ctx.translate(stepX, stepY);  
  31.             ctx.strokeRect(0, 0, 100, 100);  
  32.         }  
  33.         function clearIt() {  
  34.             ctx.translate(-canvasX, -canvasY);  
  35.             canvasX = 0;  
  36.             canvasY = 0;  
  37.             ctx.strokeStyle = "black";  
  38.             ctx.clearRect(0, 0, 400, 400);  
  39.         }  
  40.     </script> 
  41. </body> 
  42. </html> 

2、旋转 | rotate()
canvas/transform/rotate.html

  1. <!DOCTYPE HTML> 
  2. <html> 
  3. <head> 
  4.     <title>旋转</title> 
  5. </head> 
  6. <body> 
  7.     <canvas id="canvas" width="400" height="400" style="background-color: rgb(222, 222, 222)"> 
  8.         您的浏览器不支持 canvas 标签  
  9.     </canvas> 
  10.     <br /> 
  11.     <button type="button" onclick="drawIt();">不断地点我看 Demo</button> 
  12.     <button type="button" onclick="clearIt();">清除画布</button> 
  13.     <script type="text/javascript"> 
  14.         var ctx = document.getElementById('canvas').getContext('2d');  
  15.         var canvasRadian = 0;  
  16.         var stepRadian = 15 * Math.PI / 180;  
  17.         function drawIt() {  
  18.             if (canvasRadian == 0)  
  19.                 ctx.strokeRect(360, 0, 20, 60);  
  20.             canvasRadian += stepRadian;  
  21.             /*  
  22.              * context.rotate(radian) - 将当前的用户坐标系旋转指定的弧度,顺时针为正值,逆时针为负值  
  23.              *   radian - 弧度值  
  24.              */  
  25.             ctx.strokeStyle = "blue";  
  26.             ctx.rotate(stepRadian);  
  27.             ctx.strokeRect(360, 0, 20, 60);  
  28.         }  
  29.         function clearIt() {  
  30.             ctx.rotate(-canvasRadian);  
  31.             canvasRadian = 0;  
  32.             ctx.strokeStyle = "black";  
  33.             ctx.clearRect(0, 0, 400, 400);  
  34.         }  
  35.     </script> 
  36. </body> 
  37. </html> 

3、缩放 | scale()
canvas/transform/scale.html

  1. <!DOCTYPE HTML> 
  2. <html> 
  3. <head> 
  4.     <title>缩放</title> 
  5. </head> 
  6. <body> 
  7.     <canvas id="canvas" width="400" height="400" style="background-color: rgb(222, 222, 222)"> 
  8.         您的浏览器不支持 canvas 标签  
  9.     </canvas> 
  10.     <br /> 
  11.     <button type="button" onclick="drawIt();">不断地点我看 Demo</button> 
  12.     <button type="button" onclick="clearIt();">清除画布</button> 
  13.     <script type="text/javascript"> 
  14.         var ctx = document.getElementById('canvas').getContext('2d');  
  15.         var canvasScaleX = 1;  
  16.         var canvasScaleY = 1;  
  17.         var stepScaleX = 1.1;  
  18.         var stepScaleY = 1.1;  
  19.         function drawIt() {  
  20.             if (canvasScaleX == 1 && canvasScaleY == 1)  
  21.                 ctx.strokeRect(0, 0, 60, 60);  
  22.             canvasScaleX *= stepScaleX;  
  23.             canvasScaleY *= stepScaleY;  
  24.             /*  
  25.              * context.scale(x, y) - 将当前的用户坐标系缩放指定的倍数  
  26.              *   x - 水平方向上的缩放倍数  
  27.              *   y - 垂直方向上的缩放倍数  
  28.              */  
  29.             ctx.strokeStyle = "blue";  
  30.             ctx.scale(stepScaleX, stepScaleY);  
  31.             ctx.strokeRect(0, 0, 60, 60);  
  32.         }  
  33.         function clearIt() {  
  34.             ctx.scale(1 / canvasScaleX, 1 / canvasScaleY);  
  35.             canvasScaleX = 1;  
  36.             canvasScaleY = 1;  
  37.             ctx.strokeStyle = "black";  
  38.             ctx.clearRect(0, 0, 400, 400);  
  39.         }  
  40.     </script> 
  41. </body> 
  42. </html> 

#p#

4、矩阵转换 | transform(a, b, c, d, e, f)
canvas/transform/transform.html

  1. <!DOCTYPE HTML> 
  2. <html> 
  3. <head> 
  4.     <title>矩阵转换 | transform(a, b, c, d, e, f)</title> 
  5. </head> 
  6. <body> 
  7.     <canvas id="canvas" width="400" height="400" style="background-color: rgb(222, 222, 222)"> 
  8.         您的浏览器不支持 canvas 标签  
  9.     </canvas> 
  10.     <br /> 
  11.     <button type="button" onclick="drawIt();">不断地点我看 Demo</button> 
  12.     <button type="button" onclick="clearIt();">清除画布</button> 
  13.     <script type="text/javascript"> 
  14.         var ctx = document.getElementById('canvas').getContext('2d');  
  15.         var canvasScaleX = 1;  
  16.         var canvasScaleY = 1;  
  17.         var stepScaleX = 1.1;  
  18.         var stepScaleY = 1.1;  
  19.         function drawIt() {  
  20.             if (canvasScaleX == 1 && canvasScaleY == 1)  
  21.                 ctx.strokeRect(0, 0, 60, 60);  
  22.             canvasScaleX *= stepScaleX;  
  23.             canvasScaleY *= stepScaleY;  
  24.             /*  
  25.              * context.transform(a, b, c, d, e, f) - 按指定的矩阵转换当前的用户坐标系  
  26.              *   相当于:context.transform(M11, M12, M21, M22, OffsetX, OffsetY)  
  27.              *  
  28.              * 关于仿射矩阵参考:http://www.cnblogs.com/webabcd/archive/2008/11/03/1325150.html  
  29.              *  
  30.              *   |X|             |M11(默认值 1)      M21(默认值 0)       0|  
  31.              *   |Y| = |x y 1| * |M12(默认值 0)      M22(默认值 1)       0|  
  32.              *   |1|             |OffsetX(默认值 0)  OffsetY(默认值 0)   1|  
  33.              *  
  34.              *   X = x * M11 + y * M12 + OffsetX  
  35.              *   Y = x * M21 + y * M22 + OffsetY  
  36.              */  
  37.             ctx.strokeStyle = "blue";  
  38.             ctx.transform(stepScaleX, 0, 0, stepScaleY, 0, 0);  
  39.             ctx.strokeRect(0, 0, 60, 60);  
  40.         }  
  41.         function clearIt() {  
  42.             ctx.transform(1 / canvasScaleX, 0, 0, 1 / canvasScaleY, 0, 0);  
  43.             canvasScaleX = 1;  
  44.             canvasScaleY = 1;  
  45.             ctx.strokeStyle = "black";  
  46.             ctx.clearRect(0, 0, 400, 400);  
  47.         }  
  48.     </script> 
  49. </body> 
  50. </html> 

5、矩阵转换 | setTransform(a, b, c, d, e, f)
canvas/transform/setTransform.html

  1. <!DOCTYPE HTML> 
  2. <html> 
  3. <head> 
  4.     <title>矩阵转换 | setTransform(a, b, c, d, e, f)</title> 
  5. </head> 
  6. <body> 
  7.     <canvas id="canvas" width="400" height="400" style="background-color: rgb(222, 222, 222)"> 
  8.         您的浏览器不支持 canvas 标签  
  9.     </canvas> 
  10.     <br /> 
  11.     <button type="button" onclick="drawIt();">Demo</button> 
  12.     <button type="button" onclick="clearIt();">清除画布</button> 
  13.     <script type="text/javascript"> 
  14.         var ctx = document.getElementById('canvas').getContext('2d');  
  15.         function drawIt() {  
  16.             ctx.strokeStyle = "red";  
  17.             ctx.scale(2, 2);  
  18.             ctx.strokeRect(0, 0, 60, 60);  
  19.             /*  
  20.              * context.setTransform(a, b, c, d, e, f) - 首先重置用户坐标系,然后再按指定的矩阵转换用户坐标系(translate, rotate, scale, transform 是针对当前用户坐标系做转换,而 setTransform 是针对重置后的用户坐标系做转换)  
  21.              *   相当于:context.setTransform(M11, M12, M21, M22, OffsetX, OffsetY)  
  22.              *  
  23.              * 关于仿射矩阵参考:http://www.cnblogs.com/webabcd/archive/2008/11/03/1325150.html  
  24.              *  
  25.              *   |X|             |M11(默认值 1)      M21(默认值 0)       0|  
  26.              *   |Y| = |x y 1| * |M12(默认值 0)      M22(默认值 1)       0|  
  27.              *   |1|             |OffsetX(默认值 0)  OffsetY(默认值 0)   1|  
  28.              *  
  29.              *   X = x * M11 + y * M12 + OffsetX  
  30.              *   Y = x * M21 + y * M22 + OffsetY  
  31.              */  
  32.             ctx.strokeStyle = "blue";  
  33.             ctx.setTransform(1, 0, 0, 1, 0, 0);  
  34.             ctx.strokeRect(0, 0, 60, 60);  
  35.         }  
  36.         function clearIt() {  
  37.             ctx.clearRect(0, 0, 400, 400);  
  38.         }  
  39.     </script> 
  40. </body> 
  41. </html> 

[源码下载]

原文链接:http://www.cnblogs.com/webabcd/archive/2012/02/22/2362505.html

 

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

2012-05-31 10:57:06

HTML5

2012-05-30 13:26:12

HTML5

2012-05-30 13:49:52

HTML5

2012-05-30 14:51:09

HTML5

2012-05-31 09:19:22

HTML5

2012-05-31 09:35:43

HTML5

2012-05-30 15:17:54

HTML5

2012-05-30 11:11:42

HTML5

2012-05-30 13:17:46

HTML5

2012-05-30 10:52:09

HTML5

2009-11-18 13:30:37

Oracle Sequ

2009-11-17 17:31:58

Oracle COMM

2022-02-25 08:54:50

setState异步React

2021-03-16 08:54:35

AQSAbstractQueJava

2011-07-04 10:39:57

Web

2013-11-14 15:53:53

AndroidAudioAudioFlinge

2017-06-06 15:34:41

物联网数据库压缩

2017-06-05 14:50:33

大数据数据库压缩

2019-01-07 15:29:07

HadoopYarn架构调度器

2017-07-02 18:04:53

块加密算法AES算法
点赞
收藏

51CTO技术栈公众号