1.canvas Elemental Basics
(1) placing a canvas element on a page is equivalent to placing a "canvas" on the page, where you can write a script to paint it in JavaScript.
(2) placing the canvas element in the page
eg
1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 <Head>4 <MetaCharSet= "UTF-8">5 <title>Test</title>6 <Scripttype= "Text/javascript"src= "Script.js"CharSet= "gb2312"></Script>7 </Head>8 <Bodyonload= "Draw (' canvas ');">9 <H1>Element tags</H1>Ten <CanvasID= "Canvas"width= "The "Height= "+"></Canvas> One </Body> A </HTML>
1 functionDraw (id) {2 varcanvas=document.getElementById (ID);3 if(canvas==NULL){4 return false;5 }6 varContext=canvas.getcontext ("2d");7Context.fillstyle= "#eeeeff";//FillStyle: Fills the style, filling in the property with the fill color. 8Context.fillrect (0,0,400,300);//FillRect method, fills the rectangle. 9context.fillstyle= "Red"; TenContext.strokestyle= "Bule";//Strokestyle: The style of the graphic border, the color of the border. Onecontext.linewidth=1;//line width. AContext.fillrect (50,50,100,100); -Context.strokerect (50,50,100,100);//Strokerect: Draws a rectangular border. -Context.clearrect (60,60,50,50);//Clearrect: Erases the shape in the specified rectangular area to become transparent. the}
To draw a rectangle using canvas and JavaScript scripts, as in the previous procedure, proceed as follows:
(1) Get the canvas elements, and use document.getElementById and other methods to get the canvas object.
(2) Contextual (context)
When you are drawing, you need to use the graphics context, which is an object that encapsulates many of the drawing features. You need to use the GetContext method of the canvas object to get the context of the graph. In the draw function, set the parameter to 2d.
(3) Fill and draw the border.
(4) Use FillStyle and Strokestyle to set the style of the graphic and border.
(5) Draw rectangles and borders using the FillRect and Strokerect methods.
Note:
Context.fillrect (X,y,width,height)
Context.strokerect (X,y,width,height)
Context.clearrect (x,y,width,height) The parameters of the three methods are the same, x refers to the horizontal axis of the beginning of the rectangle, y refers to the vertical axis of the beginning of the rectangle, the coordinates origin is the top left corner of the canvas canvas, width refers to the length of the rectangle, Height refers to the width of the rectangle-the size of the rectangle can be determined at the same time by the 4 parameters.
H5 and CS3 Authority. 5 Drawing Graphics (1)