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