Posts in current category
May 03, 2021 12:37 HTML5
Labels define graphics, such as charts and other images, and you must use scripts to draw graphs.
Draw a red rectangle, a gradient rectangle, a colored rectangle, and some colored text on the canvas.
Labels are just graphic containers, and you must use scripts to draw graphics.
There are several ways you can use Canva to draw paths, boxes, circles, characters, and add images.
Internet Explorer 9 Plus, Firefox, Opera, Chrome, and Safari support elements.
Note: Internet Explorer 8 and earlier versions of IE browsers do not support the element.
A canvas in a Web page is a rectangular box drawn by the elements of the .lt;canvas.
Note: By default, the element has no borders and content.
A simple example of the following is:
Note: Labels often need to specify an id property (frequently referenced in scripts), the width, and height properties define the size of the canvas.
Tip: You can use multiple elements in HTML pages.
Use the style property to add a border:
First, find the element of the .lt;canvas:
Then, create the context object:
The getContext ("2d") object is a built-in HTML5 object with multiple ways to draw paths, rectangles, circles, characters, and add images.
The following two lines of code draw a red rectangle:
Setting fillStyle properties can be CSS color, gradient, or pattern. The default setting for fillStyle is .000000 (black).
The fillRect (x,y, width, height) method defines how the rectangle is currently filled.
Canvas is a two-dimensional grid.
The upper-left coordinates of canvas are (0,0)
The fillRect method above has parameters (0,0,150,75).
To draw a 150x75 rectangle on the canvas, starting in the upper left corner (0,0).
The coordinate instance
As shown in the following image, the X and Y coordinates of the canvas are used to position the painting on the canvas.
To draw a line on Canvas, we'll use two methods:
MoveTo (x,y) defines the line start coordinates
LineTo (x,y) defines the line end coordinates
To draw lines we have to use the "ink" method, just likestroke().
Define the start coordinates (0,0), and the end coordinates (200,100). Then use the stroke() method to draw the line:
To draw a circle in canvas, we'll use the following method:
We actually use the "ink" method when drawing circles, such as stroke() or fill().
Draw a circle using the arc() method:
Drawing text with canvas, important properties and methods are as follows:
font - Define the font
fillText (text, x, y) - Draw solid text on canvas
StrokeText (text, x, y) - Draw hollow text on canvas
Draw a high 30px text (solid) on the canvas using the "Arial" font:
Draw a high 30px text (hollow) on the canvas using the "Arial" font:
Gradients can be filled in rectangles, circles, lines, text, etc., and various shapes can define different colors themselves.
There are two different ways to set up the Canvas gradient:
CreateLinearGradient (x, y, x1, y1) - Create a line gradient
CreateRadialGradient (x, y, r, x1, y1, r1) - create a radial/circular gradient
When we use gradient objects, we must use two or more stop colors.
The addColorStop() method specifies a color stop, and the parameters are described using coordinates, which can be 0 to 1.
Using a gradient, set the value of fillStyle orstrokeStyle to a gradient, and then draw shapes such as rectangles, text, or a line.
Create a linear gradient. Fill the rectangle with a gradient:
Create a radial/circular gradient. Fill the rectangle with a gradient:
Tip: The parameters in parentheses of context.createRadialGradient (x, y, r, x1, y1, r1) have the following meaning when creating radial gradients using the element:
Place an image on the canvas using the following method:
drawImage( image,x,y )
Place an image on the canvas:
The full properties of the label can be referred to the Canvas reference manual. .
HTML DOM Reference Manual: HTML DOM Canvas Object