HTML5 Canvas - 模式和陰影



建立模式

在畫布上建立模式需要以下方法:

序號 方法和描述
1

createPattern(image, repetition)

此方法將使用影像來建立模式。第二個引數可以是包含以下值之一的字串:repeat、repeat-x、repeaty 和 no-repeat。如果指定空字串或 null,則將假定為 repeat

示例

以下是一個簡單的示例,它使用上面提到的方法來建立一個漂亮的模式。

<!DOCTYPE HTML>

<html>
   <head>
      
      <style>
         #test {
            width:100px;
            height:100px;
            margin: 0px auto;
         }
      </style>
      
      <script type = "text/javascript">
         function drawShape() {
            
            // get the canvas element using the DOM
            var canvas = document.getElementById('mycanvas');
            
            // Make sure we don't execute when canvas isn't supported
            if (canvas.getContext) {
            
               // use getContext to use the canvas for drawing
               var ctx = canvas.getContext('2d');
               
               // create new image object to use as pattern
               var img = new Image();
               
               img.src = 'images/pattern.jpg';
               img.onload = function() {
                  
                  // create pattern
                  var ptrn = ctx.createPattern(img,'repeat');
                  ctx.fillStyle = ptrn;
                  ctx.fillRect(0,0,150,150);
               }
            } else {
               alert('You need Safari or Firefox 1.5+ to see this demo.');
            }
         }
      </script>
   </head>
    
   <body id = "test" onload = "drawShape();">
      <canvas id = "mycanvas"></canvas>
   </body>
   
</html>

假設我們有以下模式 images/pattern.jpg

Pattern

以上示例將繪製以下結果:

建立陰影

HTML5 canvas 提供了在圖形周圍建立漂亮陰影的功能。所有繪圖操作都受四個全域性陰影屬性的影響。

序號 屬性和描述
1

shadowColor [ = value ]

此屬性返回當前陰影顏色,並且可以設定,以更改陰影顏色。

2

shadowOffsetX [ = value ]

此屬性返回當前陰影偏移 X,並且可以設定,以更改陰影偏移 X。

3

shadowOffsetY [ = value ]

此屬性返回當前陰影偏移 Y,並且可以設定,以更改陰影偏移 Y。

4

shadowBlur [ = value ]

此屬性返回應用於陰影的當前模糊級別,並且可以設定,以更改模糊級別。

示例

以下是一個簡單的示例,它使用上面提到的屬性來繪製陰影。

<!DOCTYPE HTML>

<html>
   <head>
      
      <style>
         #test {
            width: 100px;
            height:100px;
            margin: 0px auto;
         }
      </style>
      
      <script type = "text/javascript">
         function drawShape() {
            
            // get the canvas element using the DOM
            var canvas = document.getElementById('mycanvas');
            
            // Make sure we don't execute when canvas isn't supported
            if (canvas.getContext) {
            
               // use getContext to use the canvas for drawing
               var ctx = canvas.getContext('2d');
               
               ctx.shadowOffsetX = 2;   
               ctx.shadowOffsetY = 2;   
               
               ctx.shadowBlur = 2;   
               ctx.shadowColor = "rgba(0, 0, 0, 0.5)";
               
               ctx.font = "20px Times New Roman";
               ctx.fillStyle = "Black";
               
               ctx.fillText("This is shadow test", 5, 30);
            } else {
               alert('You need Safari or Firefox 1.5+ to see this demo.');
            }
         }
      </script>
   </head>
   
   <body id = "test" onload = "drawShape();">
      <canvas id = "mycanvas"></canvas>
   </body>
</html>

以上示例將產生以下結果:

html5_canvas.htm
廣告