The implementation of fill pattern in HTML5 canvas basic drawing

Time:2021-4-16

<canvas></canvas>It is a new tag added in HTML5 for drawing graphics. In fact, like other tags, this tag is special in that it can obtain a canvasrenderingcontext2d object, which can be controlled by JavaScript script for drawing.

<canvas></canvas>It’s just a container for drawing graphics. In addition to ID, class, style and other attributes, there are also height and width attributes. There are three main steps in drawing on < canvas > > elements

1. Get the DOM object corresponding to the < canvas > element, which is a canvas object;
2. Call getcontext () method of canvas object to get a canvasrenderingcontext2d object;
3. Call canvasrenderingcontext2d object to draw.

Fill patterns

In addition to setting colors, fillStyle and strokestyle can also set other filling styles. Here, fillStyle is taken as an example

· linear gradient

Use steps
(1)var grd = context.createLinearGradient (xstart, ystart, xend, yend) create a linear gradient and set the start and end coordinates;
(2) grd.addColorStop (stop, color) adds color to the linear gradient, and stop is a value from 0 to 1;
(3) context.fillStyle=grd Assign a value to context.

· radial gradient
This method is similar to the linear gradient method, except that the parameters received in the first step are different
var grd = context.createRadialGradient (x0, Y0, R0, x1, Y1, R1); receive the coordinates and radius of the start center and the end center.

· bitmap fill
Create pattern (IMG, repeat style) uses image filling, and repeat style can take repeat, repeat-x, repeat-y and no repeat.  

JavaScript CodeCopy content to clipboard
  1. var canvas = document.getElementById(“canvas”);   
  2.     var context = canvas.getContext(“2d”);   
  3.   
  4.     //Linear gradient  
  5.     var grd = context.createLinearGradient( 10 , 10, 100 , 350 );   
  6.     grd.addColorStop(0,“#1EF9F7”);   
  7.     grd.addColorStop(0.25,“#FC0F31”);   
  8.     grd.addColorStop(0.5,“#ECF811”);   
  9.     grd.addColorStop(0.75,“#2F0AF1”);   
  10.     grd.addColorStop(1,“#160303”);   
  11.     context.fillStyle = grd;   
  12.     context.fillRect(10,10,100,350);   
  13.   
  14.     //Radial gradient  
  15.     var grd = context.createRadialGradient(325 , 200, 0 , 325 , 200 , 200 );   
  16.     grd.addColorStop(0,“#1EF9F7”);   
  17.     grd.addColorStop(0.25,“#FC0F31”);   
  18.     grd.addColorStop(0.5,“#ECF811”);   
  19.     grd.addColorStop(0.75,“#2F0AF1”);   
  20.     grd.addColorStop(1,“#160303”);   
  21.     context.fillStyle = grd;   
  22.     context.fillRect(150,10,350,350);   
  23.   
  24.     //Bitmap fill  
  25.     var bgimg = new Image();   
  26.     bgimg.src = “background.jpg”;   
  27.     bgimg.onload=function(){   
  28.         var pattern = context.createPattern(bgimg, “repeat”);   
  29.         context.fillStyle = pattern;   
  30.         context.strokeStyle=“#F20B0B”;   
  31.         context.fillRect(600, 100, 200,200);   
  32.         context.strokeRect(600, 100, 200,200);   
  33.     };   
  34.   

The results are as follows

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.