H5 and CS3 Authority. 5 Drawing Graphics (1)

Source: Internet
Author: User

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)

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.