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

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

開發(fā) 前端
呈現(xiàn)文本|font, textAlign, textBaseline, strokeStyle, fillStyle, fillText(), measureText,TextMetrics.width

介紹

HTML 5: 畫布(canvas)之承載媒體

呈現(xiàn)文本 - font, textAlign, textBaseline, strokeStyle, fillStyle, fillText(), measureText, TextMetrics.width

呈現(xiàn)圖片 - drawImage()

呈現(xiàn)視頻截圖 - drawImage()

呈現(xiàn)其他畫布 - drawImage()

示例

1、呈現(xiàn)文本 | font, textAlign, textBaseline, strokeStyle, fillStyle, fillText(), measureText, TextMetrics.width
canvas/media/text.html

  1. <!DOCTYPE HTML> 
  2. <html> 
  3. <head> 
  4.     <title>在 canvas 上呈現(xiàn)文本的 demo</title> 
  5. </head> 
  6. <body> 
  7.     <canvas id="canvas" width="600" height="600" style="background-color: rgb(222, 222, 222)"> 
  8.         您的瀏覽器不支持 canvas 標(biāo)簽  
  9.     </canvas> 
  10.     <br /> 
  11.     <button type="button" onclick="drawIt();">在畫布上呈現(xiàn)一些文本</button> 
  12.     <button type="button" onclick="clearIt();">清除畫布</button> 
  13.     <script type="text/javascript"> 
  14.         var canvas = document.getElementById('canvas');  
  15.         var ctx = canvas.getContext('2d');  
  16.         function drawIt() {  
  17.             clearIt();  
  18.             ctx.beginPath();  
  19.             ctx.moveTo(0, canvas.height / 2);  
  20.             ctx.lineTo(canvas.width, canvas.height / 2);  
  21.             ctx.moveTo(canvas.width / 2, 0);  
  22.             ctx.lineTo(canvas.width / 2, canvas.height);  
  23.             ctx.stroke();  
  24.             /*  
  25.              * context.font - 語(yǔ)法與 CSS 中的 font 屬性相同,默認(rèn)值為 10px sans-serif  
  26.              *  
  27.              * context.textAlign - 可能的值為:start, end, left, right, center;默認(rèn)值為:start  
  28.              *  
  29.              * context.textBaseline - 可能的值為:top, hanging, middle, alphabetic, ideographic, bottom;默認(rèn)值為:alphabetic  
  30.              */  
  31.             ctx.font = 'italic 24px 宋體';  
  32.             ctx.fillStyle = "blue";  
  33.             ctx.textBaseline = "top";  
  34.             // 不同 textAlign 的效果  
  35.             ctx.textAlign = "start";  
  36.             ctx.fillText("Horizontal", canvas.width / 2, 0);  
  37.             ctx.textAlign = "end";  
  38.             ctx.fillText("Horizontal", canvas.width / 2, 30);  
  39.             ctx.textAlign = "left";  
  40.             ctx.fillText("Horizontal", canvas.width / 2, 60);  
  41.             ctx.textAlign = "right";  
  42.             ctx.fillText("Horizontal", canvas.width / 2, 90);  
  43.             ctx.textAlign = "center";  
  44.             ctx.fillText("Horizontal", canvas.width / 2, 120);  
  45.             ctx.textAlign = "start";  
  46.             // 不同 textBaseline 的效果  
  47.             ctx.textBaseline = "top";  
  48.             ctx.fillText("Vertical", 0, canvas.height / 2);  
  49.             ctx.textBaseline = "hanging";  
  50.             ctx.fillText("Vertical", 100, canvas.height / 2);  
  51.             ctx.textBaseline = "middle";  
  52.             ctx.fillText("Vertical", 200, canvas.height / 2);  
  53.             ctx.textBaseline = "alphabetic";  
  54.             ctx.fillText("Vertical", 300, canvas.height / 2);  
  55.             ctx.textBaseline = "ideographic";  
  56.             ctx.fillText("Vertical", 400, canvas.height / 2);  
  57.             ctx.textBaseline = "bottom";  
  58.             ctx.fillText("Vertical", 500, canvas.height / 2);  
  59.             /*  
  60.              * context.strokeStyle - 調(diào)用 strokeText() 時(shí)字體的筆劃顏色  
  61.              *  
  62.              * context.fillStyle - 調(diào)用 fillText() 時(shí)字體的填充顏色  
  63.              *  
  64.              * context.fillText(text, x, y [, maxWidth]) - 以填充的方式繪制文本內(nèi)容  
  65.              *   text - 需要呈現(xiàn)的文本字符串  
  66.              *   x, y - 參考點(diǎn)坐標(biāo),textAlign 和 textBaseline 均會(huì)以此點(diǎn)為參考點(diǎn)  
  67.              *   maxWidth - 顯示區(qū)域的最大長(zhǎng)度,必要時(shí)會(huì)強(qiáng)制壓縮文本的長(zhǎng)度。可選參數(shù)  
  68.              *  
  69.              * context.strokeText(text, x, y [, maxWidth]) - 以筆劃的方式繪制文本內(nèi)容。參數(shù)說(shuō)明同 fillText()  
  70.              */  
  71.             ctx.font = '64px 宋體';  
  72.             ctx.strokeStyle = 'blue';  
  73.             ctx.strokeText("strokeText", 0, 400);  
  74.             ctx.strokeText("strokeText", 0, 500, 100);  
  75.             /*  
  76.              * context.measureText(text) - 返回指定文本內(nèi)容在當(dāng)前上下文環(huán)境中的度量對(duì)象,即 TextMetrics 類型的對(duì)象  
  77.              * TextMetrics.width - TextMetrics 對(duì)象有一個(gè)名為 width 的只讀屬性,用于獲取 TextMetrics 對(duì)象的長(zhǎng)度值  
  78.              */  
  79.             var metrics = ctx.measureText("strokeText");  
  80.             alert(metrics.width);  
  81.         }  
  82.         function clearIt() {  
  83.             ctx.clearRect(0, 0, 600, 600);  
  84.             ctx.textAlign = "start";  
  85.             ctx.textBaseline = "alphabetic";  
  86.         }  
  87.     </script> 
  88. </body> 
  89. </html> 

#p#

2、呈現(xiàn)圖片 | drawImage()canvas/media/image.html

  1. <!DOCTYPE HTML> 
  2. <html> 
  3. <head> 
  4.     <title>在 canvas 上呈現(xiàn)圖片的 demo</title> 
  5. </head> 
  6. <body> 
  7.     <canvas id="canvas" width="800" height="600" style="background-color: rgb(222, 222, 222)"> 
  8.         您的瀏覽器不支持 canvas 標(biāo)簽  
  9.     </canvas> 
  10.     <br /> 
  11.     <button type="button" onclick="drawIt();">在畫布上呈現(xiàn)圖片</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.             clearIt();  
  17.             var img = new Image();  
  18.             img.onload = function () {  
  19.                 /*  
  20.                  * context.drawImage() 的可繪制對(duì)象包括:HTMLImageElement, HTMLVideoElement, HTMLCanvasElement  
  21.                  *  
  22.                  * context.drawImage(image, x, y) - 繪制圖像  
  23.                  *   image - 圖像對(duì)象,可以來(lái)自 img 標(biāo)簽  
  24.                  *   x - 圖像繪制到畫布后的左上角的 x 坐標(biāo)  
  25.                  *   y - 圖像繪制到畫布后的左上角的 y 坐標(biāo)  
  26.                  *  
  27.                  * context.drawImage(image, x, y, width, height) - 繪制圖像  
  28.                  *   image - 圖像對(duì)象,可以來(lái)自 img 標(biāo)簽  
  29.                  *   x - 圖像繪制到畫布后的左上角的 x 坐標(biāo)  
  30.                  *   y - 圖像繪制到畫布后的左上角的 y 坐標(biāo)  
  31.                  *   width - 圖像繪制到畫布后的寬  
  32.                  *   height - 圖像繪制到畫布后的高  
  33.                  *  
  34.                  * context.drawImage(image, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight) - 繪制圖像  
  35.                  *   image - 圖像對(duì)象,可以來(lái)自 img 標(biāo)簽  
  36.                  *   sourceX - 截取源圖像,相對(duì)于源圖像的左上角的 x 坐標(biāo),  
  37.                  *   sourceY - 截取源圖像,相對(duì)于源圖像的左上角的 y 坐標(biāo),  
  38.                  *   sourceWidth - 截取源圖像,需要截取的寬  
  39.                  *   sourceHeight - 截取源圖像,需要截取的高  
  40.                  *   destX - 圖像繪制到畫布后的左上角的 x 坐標(biāo)  
  41.                  *   destY - 圖像繪制到畫布后的左上角的 y 坐標(biāo)  
  42.                  *   destWidth - 圖像繪制到畫布后的寬  
  43.                  *   destHeight - 圖像繪制到畫布后的高  
  44.                  *  
  45.                  */  
  46.                 ctx.drawImage(img, 0, 0); // 按圖像原始大小顯示到畫布上  
  47.                 ctx.drawImage(img, 0, 0, 200, 200); // 顯示圖像到畫布上,并指定其寬高  
  48.                 ctx.drawImage(img, 50, 0, 400, 100, 0, 300, 200, 50); // 截取源圖像的一部分顯示到畫布上,并指定被截取部分顯示時(shí)的寬和高  
  49.             }  
  50.             img.src = "http://www.w3.org/html/logo/downloads/HTML5_Logo_512.png";  
  51.             // img.src = "http://www.cnblogs.com/assets/html5_logo.png";  
  52.         }  
  53.         function clearIt() {  
  54.             ctx.clearRect(0, 0, 800, 600);  
  55.         }  
  56.     </script> 
  57. </body> 
  58. </html> 

#p#

3、呈現(xiàn)視頻截圖 | drawImage()canvas/media/video.html

  1. <!DOCTYPE HTML> 
  2. <html> 
  3. <head> 
  4.     <title>在 canvas 上呈現(xiàn)視頻截圖的 demo</title> 
  5. </head> 
  6. <body> 
  7.     <video id="video" width="400" height="240" src="http://ie.microsoft.com/testdrive/Graphics/VideoFormatSupport/big_buck_bunny_trailer_480p_high.mp4" controls preload="metadata"> 
  8.         您的瀏覽器不支持 video 標(biāo)簽  
  9.     </video>    
  10.     <br /> 
  11.     <canvas id="canvas" width="400" height="240" style="background-color: rgb(222, 222, 222)"> 
  12.         您的瀏覽器不支持 canvas 標(biāo)簽  
  13.     </canvas> 
  14.     <br /> 
  15.     <button type="button" onclick="drawIt();">在畫布上呈現(xiàn)視頻截圖</button> 
  16.     <button type="button" onclick="clearIt();">清除畫布</button> 
  17.     <script type="text/javascript"> 
  18.         var ctx = document.getElementById('canvas').getContext('2d');  
  19.         var video = document.getElementById('video');  
  20.         var timerId = -1;  
  21.         function drawIt() {  
  22.             clearIt();  
  23.             timerId = setInterval(drawVideo, 300);  
  24.             function drawVideo() {  
  25.                 if (!isNaN(video.duration)) {  
  26.                     /*  
  27.                      * context.drawImage() 的可繪制對(duì)象包括:HTMLImageElement, HTMLVideoElement, HTMLCanvasElement  
  28.                      *  
  29.                      * 呈現(xiàn) video 對(duì)象的當(dāng)前截圖,其他參數(shù)說(shuō)明詳見 image.html  
  30.                      */  
  31.                     ctx.drawImage(video, 0, 0, 400, 240);  
  32.                 }  
  33.             }   
  34.         }  
  35.         function clearIt() {  
  36.             ctx.clearRect(0, 0, 400, 240);  
  37.             clearInterval(timerId);  
  38.         }  
  39.     </script> 
  40. </body> 
  41. </html> 

#p#

4、呈現(xiàn)其他畫布 | drawImage()canvas/media/canvas.html

  1. <!DOCTYPE HTML> 
  2. <html> 
  3. <head> 
  4.     <title>在 canvas 上呈現(xiàn)其他 canvas 的 demo</title> 
  5. </head> 
  6. <body> 
  7.     <canvas id="canvas" width="300" height="300" style="background-color: rgb(222, 222, 222)"> 
  8.         您的瀏覽器不支持 canvas 標(biāo)簽  
  9.     </canvas> 
  10.     <br /> 
  11.     <canvas id="canvas2" width="300" height="300" style="background-color: rgb(222, 222, 222)"> 
  12.         您的瀏覽器不支持 canvas 標(biāo)簽  
  13.     </canvas> 
  14.     <br /> 
  15.     <button type="button" onclick="drawIt();">在畫布上呈現(xiàn)其他畫布</button> 
  16.     <button type="button" onclick="clearIt();">清除畫布</button> 
  17.     <script type="text/javascript"> 
  18.         var ctx = document.getElementById('canvas').getContext('2d');  
  19.         var ctx2 = document.getElementById('canvas2').getContext('2d');  
  20.         function drawIt() {  
  21.             clearIt();  
  22.             var img = new Image();  
  23.             img.onload = function () {  
  24.                 ctx.drawImage(img, 0, 0, 300, 300);  
  25.                 /*  
  26.                  * context.drawImage() 的可繪制對(duì)象包括:HTMLImageElement, HTMLVideoElement, HTMLCanvasElement  
  27.                  *  
  28.                  * 呈現(xiàn)指定的 canvas 的當(dāng)前圖像,其他參數(shù)說(shuō)明詳見 image.html  
  29.                  */  
  30.                 ctx2.drawImage(document.getElementById('canvas'), 0, 0);  
  31.             }  
  32.             img.src = "http://www.w3.org/html/logo/downloads/HTML5_Logo_512.png";  
  33.             // img.src = "http://www.cnblogs.com/assets/html5_logo.png";  
  34.         }  
  35.         function clearIt() {  
  36.             ctx.clearRect(0, 0, 300, 300);  
  37.             ctx2.clearRect(0, 0, 300, 300);  
  38.         }  
  39.     </script> 
  40. </body> 
  41. </html> 

[源碼下載]

原文鏈接:http://www.cnblogs.com/webabcd/archive/2012/02/16/2353563.html

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

2012-05-30 15:17:54

HTML5

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:49:52

HTML5

2012-05-30 13:26:12

HTML5

2012-05-31 09:54:13

HTML5

2012-05-30 11:11:42

HTML5

2012-05-30 13:17:46

HTML5

2012-05-30 10:52:09

HTML5

2009-11-17 17:31:58

Oracle COMM

2022-02-25 08:54:50

setState異步React

2009-11-18 13:30:37

Oracle Sequ

2011-07-04 10:39:57

Web

2021-03-16 08:54:35

AQSAbstractQueJava

2013-11-14 15:53:53

AndroidAudioAudioFlinge

2017-06-05 14:50:33

大數(shù)據(jù)數(shù)據(jù)庫(kù)壓縮

2017-06-06 15:34:41

物聯(lián)網(wǎng)數(shù)據(jù)庫(kù)壓縮

2022-09-26 09:01:15

語(yǔ)言數(shù)據(jù)JavaScript

2012-02-07 15:29:17

Android核心組件Service
點(diǎn)贊
收藏

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