HTML5 new Element –canvas

Source: Internet
Author: User

Basic characteristics
<canvas> is a new element within HTML5 that allows you to paint, embed, and use JavaScript scripts to produce a piece of animation in this element.

<canvas> was first introduced in Mac OS X's dashboard and then supported by Apple's Safari browser, using gecko1.8+ The browser for the engine supports this tag (also supported by the WebKit engine in Safari and Chrome), where,<canvas> elements are introduced as part of the Web Application 1.0 standard in the standard of the consortium, We also call it HTML5.

This document is designed to guide you through the use of <canvas> elements in your HTML document, and provides some concise examples to help you better understand it.

Preparatory work
Before using <canvas>, you need to know some basic HTML and JavaScript knowledge.

Also, please make sure that you use the browser to support the correct <canvas> tag, we recommend Firefox 1.5+/opera 9/safari 3+/chrome, please do not debug the include <canvas> on IE The code for the label.

--------------------------------------------------------------------------------

Hello,canvas
Sample code:

<canvas id= "Tutorial" width= "height=" ></canvas> it looks very much like tags, the difference is that it does not have SRC and ALT attributes, Its height and width properties are optional, and you can specify a high width in the CSS properties, but in so doing, the image it paints inside may be distorted by a small scaling, so if the image you're drawing in <canvas> looks a little different, Try removing the high width in the CSS attribute, and specifying it in its HTML properties. When you do not give <canvas> specify a high width, it will have the default width of 300px, height 150px properties. As tested in Chrome and Safari, they all make <canvas> render 300x150px size when not high width, so this should be a guideline for the consortium rather than the private nature of Firefox. )

As with most HTML tags, you can also assign an id attribute to differentiate it from other elements.

<canvas> 's performance looks like a normal tag (it also has margin,border,background, etc.), but these properties do not affect the final painting output, and the example at the back of this article will help you understand this. And if you don't have any attributes defined for it,,<canvas> looks like a transparent GIF.

Smooth degradation
Because <canvas> is a newer label, it can't be parsed properly in some older browsers (like Firefox 1.5 and full range of ie--Yes, including IE8), we need to do some work to smooth the degradation to these old browsers-it's very simple , old browsers do not support <canvas> tags, they ignore them, and then parse the next thing, so we just need <canvas> to provide some alternative content, such as some text or pictures, to support <canvas> Browser will ignore the contents of the <canvas>, and the old browser will ignore the <CANVAS>

code example:

<canvas id= "Stockgraph" width= "height=" >//Support canvas browser to parse it
Current stock Price: $3.15 +0.15//Canvas Browser parsing it
</canvas>

<canvas id= "Clock" width= "height=" >/Ibid.
</canvas> end of Necessary </canvas>
,<canvas> is treated like a label in Safari, so </canvas> endings are not required, but,</canvas> is required on some old versions of Firefox, The correct writing is to make <canvas></canvas> pairs appear.

Rendering mode
<canvas> create a fixed sized plane with one or more layers in the browser (you can understand that the same size of the n piece of glass together, you can draw on each piece of glass), the current <canvas> Some of the available drawing methods are based on the 2D plane, and in the future, perhaps <canvas> can add some 3D of graphics, such as some new features based on OpenGL ES.

When the browser just loads <canvas>, it's blank, and then you can use JavaScript script to add a background image to it or paint it on it. <canvas> has a standard DOM method: GetContext, for referencing <canvas> internal content. GetContext () has a parameter that specifies the type of its drawing (2D or 3D).

var canvas = document.getElementById (' tutorial ');
var ctx = Canvas.getcontext (' 2d '); check if the browser supports
In the "Smooth degradation" section above we have some different content in HTML for browsers that do not support <canvas>, and also, in JavaScript, we can check the GetContext method to determine whether the browser supports < Canvas> The code is as follows:

var canvas = document.getElementById (' tutorial ');
if (Canvas.getcontext) {
var ctx = Canvas.getcontext (' 2d ');
Content prepared for browsers that support GetContext
} else {
Content to be prepared for browsers that do not support getcontext
} A basic framework
Here is a basic use of <canvas> framework code, the next few chapters of some examples we will use it, we recommend that you download this file to your computer.


 
    <title>canvas tutorial</title>
     <script type= "text/javascript"
      function Draw () {
         var canvas = document.getElementById (' tutorial ');
        if (canvas.getcontext) {
           var ctx = Canvas.getcontext (' 2d ');
       }
     }
    </script>
    <style type= "Text/css"
       Canvas {border:1px solid black;}
    </style>
 
  <body onload= "Draw ();" "
    <canvas id=" tutorial "width=" height= "" ></CANVAS>
  </body
Next we end this chapter with a simple example: weDraw two rectangles in <canvas>, one with 50% transparency.

View this example



<script type= "Application/x-javascript" >
function Draw () {
var canvas = document.getElementById ("Canvas");
if (Canvas.getcontext) {
var ctx = Canvas.getcontext ("2d");

Ctx.fillstyle = "RGB (200,0,0)";
Ctx.fillrect (10, 10, 55, 50);

        Ctx.fillstyle = "Rgba (0, 0, 0.5)";//rgba respectively corresponding to Red,green,blue,alpha
        Ctx.fillrect (30, 30, 55, 50);
     }
   }
  </SCRIPT>
 
 <body onload= "Draw ();" "
   <canvas id=" Canvas "width=" height= "" ></CANVAS>
 </body>

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.