自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

HTML 5深入淺出教學(xué)篇之十

開(kāi)發(fā) 前端
本文講要的講的是HTML 5畫(huà)布(canvas)之轉(zhuǎn)換:平移|translate();旋轉(zhuǎn)|rotate();縮放|scale();矩陣轉(zhuǎn)換|transform(a, b, c, d, e, f)

介紹

HTML 5: 畫(huà)布(canvas)之轉(zhuǎn)換(轉(zhuǎn)換畫(huà)布的用戶坐標(biāo)系)

平移 | translate()

旋轉(zhuǎn) | rotate()

縮放 | scale()

矩陣轉(zhuǎn)換 | transform(a, b, c, d, e, f)

矩陣轉(zhuǎn)換 | 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 標(biāo)簽  
  9.     </canvas> 
  10.     <br /> 
  11.     <button type="button" onclick="drawIt();">不斷地點(diǎn)我看 Demo</button> 
  12.     <button type="button" onclick="clearIt();">清除畫(huà)布</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) - 將當(dāng)前的用戶坐標(biāo)系平移指定的距離  
  26.              *   x - x 軸方向上需要平移的像素?cái)?shù)  
  27.              *   y - y 軸方向上需要平移的像素?cái)?shù)  
  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、旋轉(zhuǎn) | rotate()
canvas/transform/rotate.html

  1. <!DOCTYPE HTML> 
  2. <html> 
  3. <head> 
  4.     <title>旋轉(zhuǎn)</title> 
  5. </head> 
  6. <body> 
  7.     <canvas id="canvas" width="400" height="400" style="background-color: rgb(222, 222, 222)"> 
  8.         您的瀏覽器不支持 canvas 標(biāo)簽  
  9.     </canvas> 
  10.     <br /> 
  11.     <button type="button" onclick="drawIt();">不斷地點(diǎn)我看 Demo</button> 
  12.     <button type="button" onclick="clearIt();">清除畫(huà)布</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) - 將當(dāng)前的用戶坐標(biāo)系旋轉(zhuǎn)指定的弧度,順時(shí)針為正值,逆時(shí)針為負(fù)值  
  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 標(biāo)簽  
  9.     </canvas> 
  10.     <br /> 
  11.     <button type="button" onclick="drawIt();">不斷地點(diǎn)我看 Demo</button> 
  12.     <button type="button" onclick="clearIt();">清除畫(huà)布</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) - 將當(dāng)前的用戶坐標(biāo)系縮放指定的倍數(shù)  
  26.              *   x - 水平方向上的縮放倍數(shù)  
  27.              *   y - 垂直方向上的縮放倍數(shù)  
  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、矩陣轉(zhuǎn)換 | transform(a, b, c, d, e, f)
canvas/transform/transform.html

  1. <!DOCTYPE HTML> 
  2. <html> 
  3. <head> 
  4.     <title>矩陣轉(zhuǎn)換 | 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 標(biāo)簽  
  9.     </canvas> 
  10.     <br /> 
  11.     <button type="button" onclick="drawIt();">不斷地點(diǎn)我看 Demo</button> 
  12.     <button type="button" onclick="clearIt();">清除畫(huà)布</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) - 按指定的矩陣轉(zhuǎn)換當(dāng)前的用戶坐標(biāo)系  
  26.              *   相當(dāng)于:context.transform(M11, M12, M21, M22, OffsetX, OffsetY)  
  27.              *  
  28.              * 關(guān)于仿射矩陣參考:http://www.cnblogs.com/webabcd/archive/2008/11/03/1325150.html  
  29.              *  
  30.              *   |X|             |M11(默認(rèn)值 1)      M21(默認(rèn)值 0)       0|  
  31.              *   |Y| = |x y 1| * |M12(默認(rèn)值 0)      M22(默認(rèn)值 1)       0|  
  32.              *   |1|             |OffsetX(默認(rèn)值 0)  OffsetY(默認(rèn)值 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、矩陣轉(zhuǎn)換 | setTransform(a, b, c, d, e, f)
canvas/transform/setTransform.html

  1. <!DOCTYPE HTML> 
  2. <html> 
  3. <head> 
  4.     <title>矩陣轉(zhuǎn)換 | 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 標(biāo)簽  
  9.     </canvas> 
  10.     <br /> 
  11.     <button type="button" onclick="drawIt();">Demo</button> 
  12.     <button type="button" onclick="clearIt();">清除畫(huà)布</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) - 首先重置用戶坐標(biāo)系,然后再按指定的矩陣轉(zhuǎn)換用戶坐標(biāo)系(translate, rotate, scale, transform 是針對(duì)當(dāng)前用戶坐標(biāo)系做轉(zhuǎn)換,而 setTransform 是針對(duì)重置后的用戶坐標(biāo)系做轉(zhuǎn)換)  
  21.              *   相當(dāng)于:context.setTransform(M11, M12, M21, M22, OffsetX, OffsetY)  
  22.              *  
  23.              * 關(guān)于仿射矩陣參考:http://www.cnblogs.com/webabcd/archive/2008/11/03/1325150.html  
  24.              *  
  25.              *   |X|             |M11(默認(rèn)值 1)      M21(默認(rèn)值 0)       0|  
  26.              *   |Y| = |x y 1| * |M12(默認(rèn)值 0)      M22(默認(rèn)值 1)       0|  
  27.              *   |1|             |OffsetX(默認(rèn)值 0)  OffsetY(默認(rèn)值 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

 

責(zé)任編輯:張偉 來(lái)源: webabcd的博客
相關(guān)推薦

2012-05-30 14:51:09

HTML5

2012-05-31 09:19:22

HTML5

2012-05-31 10:57:06

HTML5

2012-05-30 13:26:12

HTML5

2012-05-30 13:49:52

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

2021-07-20 15:20:02

FlatBuffers阿里云Java

2017-07-02 18:04:53

塊加密算法AES算法

2019-01-07 15:29:07

HadoopYarn架構(gòu)調(diào)度器

2012-05-21 10:06:26

FrameworkCocoa
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)