婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 實例講解使用HTML5 Canvas繪制陰影效果的方法

實例講解使用HTML5 Canvas繪制陰影效果的方法

熱門標簽:杭州營銷電銷機器人供應商 貸款電銷人工和機器人哪個好 百應電銷機器人產業 電話機器人如何 西寧智能外呼系統加盟 高德地圖標注賓館位置 聯通400電話申請 飛亞外呼系統 電視購物電銷外呼系統

創建陰影效果需要操作以下4個屬性:

1.context.shadowColor:陰影顏色。
2.context.shadowOffsetX:陰影x軸位移。正值向右,負值向左。
3.context.shadowOffsetY:陰影y軸位移。正值向下,負值向上。
4.context.shadowBlur:陰影模糊濾鏡。數據越大,擴散程度越大。
這四個屬性只要設置了第一個和剩下三個中的任意一個就有陰影效果。不過通常情況下,四個屬性都要設置。

例如,創建一個向右下方位移各5px的紅色陰影,模糊2px,可以這樣寫。

JavaScript Code復制內容到剪貼板
  1. context.shadowColor = "red";   
  2. context.shadowOffsetX = 5;   
  3. context.shadowOffsetY = 5;   
  4. context.shadowBlur= 2;  

需要注意的是,這里的陰影同其他屬性設置一樣,都是基于狀態的設置。因此,如果只想為某一個對象應用陰影而不是全局陰影,需要在下次繪制前重置陰影的這四個屬性。
運行結果:

陰影文字:

只要設置shadowOffsetX與shadowOffsetY的值,當值都正數時,陰影相對文字的右下

方偏移。當值都為負數時,陰影相對文字的左上方偏移。

3D拉影效果:

在同一位置不斷的重復繪制文字同時改變shadowOffsetX、shadowOffsetY、shadowBlur

的值,從小到大不斷偏移不斷增加,透明度也不斷增加。就得到了拉影效果文字。

邊緣模糊效果文字:

在3D拉影效果的基礎上在四個方向重復,就得到了邊緣羽化的文字效果。

運行效果:

程序代碼:

JavaScript Code復制內容到剪貼板
  1. <!DOCTYPE html>     
  2. <html>     
  3. <head>     
  4. <meta http-equiv="X-UA-Compatible" content="chrome=IE8">     
  5. <meta http-equiv="Content-type" content="text/html;charset=UTF-8">     
  6. <title>Canvas Clip Demo</title>     
  7. <link href="default.css" rel="stylesheet" />     
  8.     <script>     
  9.         var ctx = null// global variable 2d context     
  10.         var imageTexture = null;     
  11.         window.onload = function() {     
  12.             var canvas = document.getElementById("text_canvas");     
  13.             console.log(canvas.parentNode.clientWidth);     
  14.             canvas.width = canvas.parentNode.clientWidth;     
  15.             canvas.height = canvas.parentNode.clientHeight;     
  16.                  
  17.             if (!canvas.getContext) {     
  18.                 console.log("Canvas not supported. Please install a HTML5 compatible browser.");     
  19.                 return;     
  20.             }     
  21.             var context = canvas.getContext('2d');     
  22.                  
  23.             // section one - shadow and blur     
  24.             context.fillStyle="black";     
  25.             context.fillRect(0, 0, canvas.width, canvas.height/4);     
  26.             context.font = '60pt Calibri';     
  27.                  
  28.             context.shadowColor = "white";     
  29.             context.shadowOffsetX = 0;     
  30.             context.shadowOffsetY = 0;     
  31.             context.shadowBlur = 20;     
  32.             context.fillText("Blur Canvas", 40, 80);     
  33.             context.strokeStyle = "RGBA(0, 255, 0, 1)";     
  34.             context.lineWidth = 2;     
  35.             context.strokeText("Blur Canvas", 40, 80);     
  36.                  
  37.             // section two - shadow font     
  38.             var hh = canvas.height/4;     
  39.             context.fillStyle="white";     
  40.             context.fillRect(0, hh, canvas.width, canvas.height/4);     
  41.             context.font = '60pt Calibri';     
  42.                  
  43.             context.shadowColor = "RGBA(127,127,127,1)";     
  44.             context.shadowOffsetX = 3;     
  45.             context.shadowOffsetY = 3;     
  46.             context.shadowBlur = 0;     
  47.             context.fillStyle = "RGBA(0, 0, 0, 0.8)";     
  48.             context.fillText("Blur Canvas", 40, 80+hh);     
  49.                  
  50.             // section three - down shadow effect     
  51.             var hh = canvas.height/4 + hh;     
  52.             context.fillStyle="black";     
  53.             context.fillRect(0, hh, canvas.width, canvas.height/4);     
  54.             for(var i = 0; i < 10; i++)     
  55.             {     
  56.                 context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")";     
  57.                 context.shadowOffsetX = i*2;     
  58.                 context.shadowOffsetY = i*2;     
  59.                 context.shadowBlur = i*2;     
  60.                 context.fillStyle = "RGBA(127, 127, 127, 1)";     
  61.                 context.fillText("Blur Canvas", 40, 80+hh);     
  62.             }     
  63.                  
  64.             // section four -  fade effect     
  65.             var hh = canvas.height/4 + hh;     
  66.             context.fillStyle="green";     
  67.             context.fillRect(0, hh, canvas.width, canvas.height/4);     
  68.             for(var i = 0; i < 10; i++)     
  69.             {     
  70.                 context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")";     
  71.                 context.shadowOffsetX = 0;     
  72.                 context.shadowOffsetY = -i*2;     
  73.                 context.shadowBlur = i*2;     
  74.                 context.fillStyle = "RGBA(127, 127, 127, 1)";     
  75.                 context.fillText("Blur Canvas", 40, 80+hh);     
  76.             }     
  77.             for(var i = 0; i < 10; i++)     
  78.             {     
  79.                 context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")";     
  80.                 context.shadowOffsetX = 0;     
  81.                 context.shadowOffsetY = i*2;     
  82.                 context.shadowBlur = i*2;     
  83.                 context.fillStyle = "RGBA(127, 127, 127, 1)";     
  84.                 context.fillText("Blur Canvas", 40, 80+hh);     
  85.             }     
  86.             for(var i = 0; i < 10; i++)     
  87.             {     
  88.                 context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")";     
  89.                 context.shadowOffsetX = i*2;     
  90.                 context.shadowOffsetY = 0;     
  91.                 context.shadowBlur = i*2;     
  92.                 context.fillStyle = "RGBA(127, 127, 127, 1)";     
  93.                 context.fillText("Blur Canvas", 40, 80+hh);     
  94.             }     
  95.             for(var i = 0; i < 10; i++)     
  96.             {     
  97.                 context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")";     
  98.                 context.shadowOffsetX = -i*2;     
  99.                 context.shadowOffsetY = 0;     
  100.                 context.shadowBlur = i*2;     
  101.                 context.fillStyle = "RGBA(127, 127, 127, 1)";     
  102.                 context.fillText("Blur Canvas", 40, 80+hh);     
  103.             }     
  104.         }     
  105.              
  106.     </script>     
  107. </head>     
  108. <body>     
  109.     <h1>HTML5 Canvas Clip Demo - By Gloomy Fish</h1>     
  110.     <pre>Fill And Stroke Clip</pre>     
  111.     <div id="my_painter">     
  112.         <canvas id="text_canvas"></canvas>     
  113.     </div>     
  114. </body>     
  115. </html>    

標簽:邯鄲 玉溪 煙臺 撫州 牡丹江 晉中 安慶 內蒙古

巨人網絡通訊聲明:本文標題《實例講解使用HTML5 Canvas繪制陰影效果的方法》,本文關鍵詞  實例,講解,使用,HTML5,Canvas,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《實例講解使用HTML5 Canvas繪制陰影效果的方法》相關的同類信息!
  • 本頁收集關于實例講解使用HTML5 Canvas繪制陰影效果的方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 图片| 宁晋县| 额敏县| 古蔺县| 沙湾县| 开远市| 灵石县| 武安市| 武乡县| 弥渡县| 根河市| 宁德市| 佛山市| 沙坪坝区| 正镶白旗| 柏乡县| 乌拉特中旗| 都江堰市| 德安县| 庄浪县| 韶山市| 内丘县| 林周县| 澳门| 赤峰市| 辰溪县| 白玉县| 冀州市| 松滋市| 庐江县| 盘山县| 紫阳县| 孟村| 三门峡市| 孙吴县| 迭部县| 原阳县| 台北市| 荥阳市| 高平市| 三台县|