# Fill Style

# Color Fill

Canvas 中给图形设置颜色,我们需要使用 fillStyle 属性来设置,该属性接受三种形式的颜色值:颜色字符串,例如:red;十六进制值,例如:#FF0000;RGB 值,例如:rgb(255,0,0)。若不设置填充颜色,绘制的图形默认是黑色。

设置了颜色之后,我们可以使用 stroke()fill() 绘制图形。当这两个方法同时使用时,请注意在 stroke() 之前,使用 fill() ,否则图形的边框会被填充覆盖一半。

<!DOCTYPE HTML>
<html>
  <head>
    <title>Fill Style</title>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
  </head>
  <body>
    <canvas id="myCanvas" width="578" height="200"></canvas>
    <script>
      var canvas = document.getElementById('myCanvas');
      var context = canvas.getContext('2d');

      // begin custom shape
      context.beginPath();
      context.moveTo(170, 80);
      context.bezierCurveTo(130, 100, 130, 150, 230, 150);
      context.bezierCurveTo(250, 180, 320, 180, 340, 150);
      context.bezierCurveTo(420, 150, 420, 120, 390, 100);
      context.bezierCurveTo(430, 40, 370, 30, 340, 50);
      context.bezierCurveTo(320, 5, 250, 20, 250, 50);
      context.bezierCurveTo(200, 5, 150, 20, 170, 80);

      // complete custom shape
      context.closePath();
      context.lineWidth = 5;
      //// fill before stroke
      context.fillStyle = '#8ED6FF';
      context.fill();
      context.strokeStyle = 'blue';
      context.stroke();
      //// stroke before fill
      // context.strokeStyle = 'blue';
      // context.stroke();
      // context.fillStyle = '#8ED6FF';
      // context.fill();
    </script>
  </body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

# Linear Gradient

Canvas 中实现线性渐变的效果,我们需要使用 createLinearGradient() 方法,接受4个参数:x0 -> 起点的 x 坐标,y0 -> 起点的 y 坐标;x1 -> 终点的 x 坐标,y1 -> 终点的 y 坐标。创建了 gradient 对象后,可以使用 addColorStop() 方法加入颜色。

<!DOCTYPE HTML>
<html>
  <head>
    <title>Linear Gradient</title>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
  </head>
  <body>
    <canvas id="myCanvas" width="578" height="200"></canvas>
    <script>
      var canvas = document.getElementById('myCanvas');
      var context = canvas.getContext('2d');
      context.rect(0, 0, canvas.width, canvas.height);

      // add linear gradient
      var grd = context.createLinearGradient(0, 0, canvas.width, canvas.height);
      // light blue
      grd.addColorStop(0, '#8ED6FF');
      // dark blue
      grd.addColorStop(1, '#004CB3');
      context.fillStyle = grd;
      context.fill();
    </script>
  </body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

# Radial Gradient

Canvas 中实现径向渐变的效果,需要使用 createRadialGradient(),接受6个参数:x0 -> 开始圆的 x 坐标,y0 -> 开始圆的 y 坐标,r0 -> 开始圆的半径;x1 -> 终点圆的 x 坐标,y1 -> 终点圆的 y 坐标,r1 -> 终点圆的半径。

<!DOCTYPE HTML>
<html>
  <head>
    <title>Radial Gradient</title>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
  </head>
  <body>
    <canvas id="myCanvas" width="578" height="200"></canvas>
    <script>
      var canvas = document.getElementById('myCanvas');
      var context = canvas.getContext('2d');
      context.rect(0, 0, canvas.width, canvas.height);

      // create radial gradient
      var grd = context.createRadialGradient(238, 50, 0, 238, 50, 300);
      // light blue
      grd.addColorStop(0, '#8ED6FF');
      // dark blue
      grd.addColorStop(1, '#004CB3');

      context.fillStyle = grd;
      context.fill();
    </script>
  </body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

# Pattern

Canvas 使用指定的图像创建模式,需要使用 createPattern() 方法,接受2个参数:image -> 图像对象;repetition -> 如何重复图像。 repetition 属性可选值:repeatrepeat-xrepeat-yno-repeat,默认为 repeat

<!DOCTYPE HTML>
<html>
  <head>
    <title>Pattern</title>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
  </head>
  <body>
    <canvas id="myCanvas" width="578" height="200"></canvas>
    <script>
      var canvas = document.getElementById('myCanvas');
      var context = canvas.getContext('2d');

      var imageObj = new Image();
      imageObj.onload = function() {
        var pattern = context.createPattern(imageObj, 'repeat');

        context.rect(0, 0, canvas.width, canvas.height);
        context.fillStyle = pattern;
        context.fill();
      };
      imageObj.src = 'http://www.html5canvastutorials.com/demos/assets/wood-pattern.png';
    </script>
  </body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29