Quick Start Guide for Drawing Images by calling the Canvas API of HTML5, html5canvas
1. Canvas Element
The following html code defines a canvas element.
Copy XML/HTML Code to clipboard
- <! DOCTYPE html>
- <Html>
- <Head>
- <Title> Canvas Quick Start </title>
- <Meta charset = "UTF-8"/>
- </Head>
- <Body>
- <Div>
- <Canvas id = "mainCanvas" width = "640" height = "480"> </canvas>
- </Div>
- </Body>
- </Html>
Use the following Javascript statement to access the canvas element:
Copy the content to the clipboard using JavaScript Code
- // DOM writing
- Window. onload = function (){
- Var canvas = document. getElementById ("mainCanvas ");
- Var context = canvas. getContext ("2d ");
- };
- // JQuery Writing Method
- $ (Document). ready (function (){
- Var canvas =$ ("# mainCanvas ");
- Var context = canvas. get (0). getContext ("2d ");
- });
- // You can call the context method to call the drawing API.
2. Basic API
2.1 Coordinate System
The Canvas 2D rendering context uses the Cartesian coordinate system of the plane. The origin is (0, 0) in the upper left corner. one unit of the coordinate system is equivalent to one pixel on the screen. For example:
2.2 draw basic Images
2.2.1 rectangle
Copy the content to the clipboard using JavaScript Code
- // Draw a filled rectangle
- Context. fillRect (x, y, width, height)
- // Draw a border rectangle
- Context. strokeRect (x, y, width, height)
- // Clear a rectangle
- Context. clearRect (x, y, width, height)
2.2.2 line
There are some differences between drawing a line and drawing a graph. A line is actually called a path. To draw a simple path, you must first call the beginPath method, then call moveTo to set the start coordinate of the path, and then call lineTo to set the end coordinate of the line segment (which can be set multiple times ), call closePath to complete the path painting. Finally, call stroke to draw the outline (or call the fill path ). The following is an example:
Copy the content to the clipboard using JavaScript Code
- // Example
- Context. beginPath (); // start path
- Context. moveTo (40, 40); // move to a point (40, 40)
- Context. lineTo (300, 40); // draw a line to a point (, 30)
- Context. lineTo (40,300); // draw a line to point (40,300)
- Context. closePath (); // end path
- Context. stroke (); // draw the outline
- // Or fill in with context. fill ();
2.2.3 circle
Canvas does not actually have a method to draw a circle. You can draw an arc to simulate a circle. Because an arc is a path, the API for drawing an arc should be included between beginPath and closePath.
2.3 Style
2.3.1 modify the line color
Copy the content to the clipboard using JavaScript Code
- Var color;
- // Specify the RGB Value
- Color = "rgb (255, 0, 0 )";
- // Specify the RGBA value (the last parameter is the alpha value; value range: 0.0 ~ 1.0)
- Color = "rgba (255, 0, 0, 1 )";
- // Specify the hexadecimal code
- Color = "# FF0000 ";
- // Specify with words
- Color = "red ";
- // Set the fill color
- Context. fillStyle = color;
- // Set the border color
- Context. strokeStyle = color;
2.3.2 modify the line width
Copy the content to the clipboard using JavaScript Code
- // Specify the line width.
- Var value = 3;
- // Set the border color
- Context. linewidth = value;
2.4 draw text
Copy the content to the clipboard using JavaScript Code
- // Specify the Font Style
- Context. font = "italic 30px ";
- // Draw text at a vertex (40, 40)
- Context. fillText ("Hello world! ", 40, 40 );
2.5 draw images
Before creating an image, you must define the image and load it.
Copy the content to the clipboard using CSS Code.
- Var img = new Image ();
- Img. src = "myImage.png ";
- Img. onload = function (){
- // Image Loading completed
- };
The following is an explanation of the drawImage API:
Copy the content to the clipboard using JavaScript Code
- // Draw an image in (x, y)
- Context. drawImage (image, x, y)
- // In (x, y), the image that draws the width * height
- Context. drawImage (image, x, y, width, height)
- // Capture the sWidth * sHeight image at image (sx, sy), and draw the dWidth * dHeight image at (dx, dy ).
- Context. drawImage (image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)
3. Advanced functions
3.1 fill the Canvas with the browser window
The simplest way is to precisely set the width and height of the canvas element to the width and height of the browser window, and eliminate white gaps with CSS.
CSS code:
Copy the content to the clipboard using CSS Code.
- *{
- Margin: 0;
- Padding: 0;
- }
- Html, body {
- Height: 100%;
- Width: 100%;
- }
- Canvas {
- Display: block;
- }
Javascript code:
Copy the content to the clipboard using JavaScript Code
- Function resizeCanvas (){
- // Canvas is obtained by jQuery.
- Canvas. attr ("width", $ (window). get (0). innerWidth );
- Canvas. attr ("height", $ (window). get (0). innerHeight );
- Context. fillRect (0, 0, canvas. width (), canvas. height ());
- }
- $ (Window). resize (resizeCanvas );
- ResizeCanvas ();
3.2 drawing status
In a canvas, a drawing refers to the complete set of attributes that describe the 2D rendering context appearance at a certain time point, including: transform matrix, crop area, globalAlpha, struct, strokeStyle, fillStyle, lineWidth, lineCap, lineJoin, miterLimit, shadowOffsetX, shadowOffsetY, shadowBlur, shadowColor, font, textAlign, and textBaseline.
To change the global status of the canvas, save the current status first-call the save method to push the status to the drawing state stack). After the operation, call the restore method to restore the drawing status.
Copy the content to the clipboard using JavaScript Code
- // Example
- Context. save ();
- Context. globalAlpha = 0.5;
- Context. fillRect (0, 0,200,100 );
- Context. restore ();
Deformation 3.3
3.3.1 Translation
Moves the origin of the 2D rendering context from one position to another. Note that the coordinates are moved to the global drawing position. The API is as follows:
Copy the content to the clipboard using JavaScript Code
- // Move the coordinate origin to (x, y)
- Context. translate (x, y)
3.3.2 Scaling
Copy the content to the clipboard using JavaScript Code
- // Scale the global horizontal and vertical dimensions to x and y (that is, multiply the multiplication factor by the original value)
- Context. scale (x, y)
3.3.3 Rotation
Copy the content to the clipboard using JavaScript Code
- // Rotate the radius radians of the canvas around the origin
- Context. rotate (radius)