Quick Start Guide for Drawing Images by calling the Canvas API of HTML5, html5canvas

Source: Internet
Author: User

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
  1. <! DOCTYPE html>
  2. <Html>
  3. <Head>
  4. <Title> Canvas Quick Start </title>
  5. <Meta charset = "UTF-8"/>
  6. </Head>
  7. <Body>
  8. <Div>
  9. <Canvas id = "mainCanvas" width = "640" height = "480"> </canvas>
  10. </Div>
  11. </Body>
  12. </Html>

Use the following Javascript statement to access the canvas element:

Copy the content to the clipboard using JavaScript Code
  1. // DOM writing
  2. Window. onload = function (){
  3. Var canvas = document. getElementById ("mainCanvas ");
  4. Var context = canvas. getContext ("2d ");
  5. };
  6. // JQuery Writing Method
  7. $ (Document). ready (function (){
  8. Var canvas =$ ("# mainCanvas ");
  9. Var context = canvas. get (0). getContext ("2d ");
  10. });
  11. // 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
  1. // Draw a filled rectangle
  2. Context. fillRect (x, y, width, height)
  3. // Draw a border rectangle
  4. Context. strokeRect (x, y, width, height)
  5. // Clear a rectangle
  6. 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
  1. // Example
  2. Context. beginPath (); // start path
  3. Context. moveTo (40, 40); // move to a point (40, 40)
  4. Context. lineTo (300, 40); // draw a line to a point (, 30)
  5. Context. lineTo (40,300); // draw a line to point (40,300)
  6. Context. closePath (); // end path
  7. Context. stroke (); // draw the outline
  8. // 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
  1. Var color;
  2. // Specify the RGB Value
  3. Color = "rgb (255, 0, 0 )";
  4. // Specify the RGBA value (the last parameter is the alpha value; value range: 0.0 ~ 1.0)
  5. Color = "rgba (255, 0, 0, 1 )";
  6. // Specify the hexadecimal code
  7. Color = "# FF0000 ";
  8. // Specify with words
  9. Color = "red ";
  10. // Set the fill color
  11. Context. fillStyle = color;
  12. // Set the border color
  13. Context. strokeStyle = color;

2.3.2 modify the line width

Copy the content to the clipboard using JavaScript Code
  1. // Specify the line width.
  2. Var value = 3;
  3. // Set the border color
  4. Context. linewidth = value;

2.4 draw text

Copy the content to the clipboard using JavaScript Code
  1. // Specify the Font Style
  2. Context. font = "italic 30px ";
  3. // Draw text at a vertex (40, 40)
  4. 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.
  1. Var img = new Image ();
  2. Img. src = "myImage.png ";
  3. Img. onload = function (){
  4. // Image Loading completed
  5. };

The following is an explanation of the drawImage API:

Copy the content to the clipboard using JavaScript Code
  1. // Draw an image in (x, y)
  2. Context. drawImage (image, x, y)
  3. // In (x, y), the image that draws the width * height
  4. Context. drawImage (image, x, y, width, height)
  5. // Capture the sWidth * sHeight image at image (sx, sy), and draw the dWidth * dHeight image at (dx, dy ).
  6. 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.
  1. *{
  2. Margin: 0;
  3. Padding: 0;
  4. }
  5. Html, body {
  6. Height: 100%;
  7. Width: 100%;
  8. }
  9. Canvas {
  10. Display: block;
  11. }

Javascript code:

Copy the content to the clipboard using JavaScript Code
  1. Function resizeCanvas (){
  2. // Canvas is obtained by jQuery.
  3. Canvas. attr ("width", $ (window). get (0). innerWidth );
  4. Canvas. attr ("height", $ (window). get (0). innerHeight );
  5. Context. fillRect (0, 0, canvas. width (), canvas. height ());
  6. }
  7. $ (Window). resize (resizeCanvas );
  8. 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
  1. // Example
  2. Context. save ();
  3. Context. globalAlpha = 0.5;
  4. Context. fillRect (0, 0,200,100 );
  5. 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
  1. // Move the coordinate origin to (x, y)
  2. Context. translate (x, y)

3.3.2 Scaling

Copy the content to the clipboard using JavaScript Code
  1. // Scale the global horizontal and vertical dimensions to x and y (that is, multiply the multiplication factor by the original value)
  2. Context. scale (x, y)

3.3.3 Rotation

Copy the content to the clipboard using JavaScript Code
  1. // Rotate the radius radians of the canvas around the origin
  2. Context. rotate (radius)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.