HTML5 Canvas Canvases

Source: Internet
Author: User

First, what is canvas?

Canvas, a canvas that is used to draw graphics on a Web page.

Canvas has several ways to draw paths, rectangles, circles, characters, and add images.

Second, create a canvas element

Add basic attributes: class, width, and height

<class= "MyCanvas"  width:100px height:100px></  Canvas>

Third, draw the path

Using JavaScript elements to draw, the canvas province does not have the ability to draw. All the drawing must be done through JavaScript.

1. JavaScript uses ID to find the canvas element:

var C=document.getelementbyid ("MyCanvas");

GetContext ("2d") objects are built-in HTML5 objects that have a variety of drawing paths, rectangles, circles, characters, and methods for adding images.

2. Draw a red rectangle:

3, FillStyle method to dye it red, FillRect method specifies the shape, position and size.

Four, coordinates

The FillRect method above has parameters (0,0,150,75).

This means: Draw the 150x75 rectangle on the canvas, starting at the top left corner (0,0).

As shown, the X and Y coordinates of the canvas are used to position the drawing on the canvas.

1, Google browser (Chrome)

2. Under IE9 Browser

3, IE8 browser in (no pressure)

V. Code section

<!DOCTYPE HTML><HTML>    <Head>        <MetaCharSet= "Utf-8" />        <styletype= "Text/css">#box{float: Left;width:199px;Height:99px;Border:1px solid #c3c3c3;            }        </style>        <Scripttype= "Text/javascript">            functionXy_get (e) {x=E.clientx; Y=E.clienty; document.getElementById ("Xy_zuobiao"). InnerHTML= "Coordinates: (" +x+ "," +y+ ")"; }            functionxy_clear () {document.getElementById ("Xy_zuobiao"). InnerHTML= ""; }        </Script>    </Head>    <Body>        <P>Mouse Coordinates:</P>        <DivID= "box"onmousemove= "Xy_get (event)"onmouseout= "Xy_clear ()"></Div>        <DivID= "Xy_zuobiao"></Div>    </Body></HTML>

HTML5 Canvas Canvases

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.