目录

图案和阴影(Pattern and Shadow)

创建模式

在画布上创建模式需要以下方法 -

Sr.No. 方法和描述
1

createPattern(image, repetition)

此方法将使用image来创建模式。 第二个参数可以是具有以下值之一的字符串:repeat,repeat-x,repeaty和no-repeat。 如果指定了空字符串或null,则重复将。 假设

例子 (Example)

下面是一个简单的例子,它利用上面提到的方法来创建一个漂亮的模式。

<!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

图案

上面的例子将得出以下结果 -

新页面打开

创建阴影

HTML5画布提供了在绘图周围创建漂亮阴影的功能。 所有绘图操作都受四个全局阴影属性的影响。

Sr.No. 属性和描述
1

shadowColor [ = value ]

此属性返回当前阴影颜色并可以设置,以更改阴影颜色。

2

shadowOffsetX [ = value ]

此属性返回当前阴影偏移X并可以设置,以更改阴影偏移X.

3

shadowOffsetY [ = value ]

此属性返回当前阴影偏移Y并可以设置,更改阴影偏移Y.

4

shadowBlur [ = value ]

此属性返回应用于阴影的当前模糊级别,可以设置,以更改模糊级别。

例子 (Example)

以下是一个使用上述属性绘制阴影的简单示例。

<!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>

上面的例子会产生以下结果 -

新页面打开
↑回到顶部↑
WIKI教程 @2018