HTML5-Canvas labels use instance 1

Source: Internet
Author: User

Recently I was obsessed with HTML5, although HTML5 is rarely used in the current project (the reason is that we have to consider the compatibility of IE 6, 7, and 8 browsers ). The content here is purely personal interest.
1. What is a Canvas label? What is the purpose of the Canvas label?

There are too many conceptual explanations here, so I will not write more. You can simply find out more here by Baidu and Google. Canvas is an API label added in HTML5.

<Canvas> labels define images, such as tables and other images.

<Canvas> labels are only graphical containers. You must use scripts to draw images.


2. How to Use the Canvas API to draw a square and add a diagonal line?

1. First, set a Canvas label in HTML, and set its Height, Width, and Border attributes. The Code is as follows:

 
<! -- Canvas target -->
<Canvas id = "diagonal" height = "200px" width = "200px" style = "border: 1px solid #000000;">
</Canvas>
2. Draw images in the Canvas area with JS
 
Function drawDiagonal (){
// Obtain the canvas Element and Its Drawing Context
Var canvasObj = document. getElementById ("diagonal ");
Var context = canvasObj. getContext ("2d ");
// Use absolute coordinates to create a path
// Draw two diagonal lines
Context. beginPath ();
// Move the vertex to the origin
Context. moveTo (0, 0 );
// Dashes to the bottom right corner
Context. lineTo (200,200 );
// Draw the line to the canvas
Context. stroke ();
 
// Move the vertex to the bottom right corner
Context. moveTo (0,200 );
// Dashes to www.2cto.com in the upper right corner.
Context. lineTo (200, 0 );
// Draw the line to the canvas
Context. stroke ();
}
// Execute the drawing program during page Import
Window. addEventListener ("load", drawDiagonal, true );

3. After running the page



From the aganar Column

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.