Beginner canvas-Draw Basic graphics

Source: Internet
Author: User

First, use jquery to access the canvas elements in the DOM

var canvas = $ ("#myCanvas"); var context = Canvas.get (0). GetContext ("2d");

One draw a rectangle

Context.fillrect (X,y,width,height);

or for

Context.strokerect (X,y,width,height);

X, y is the horizontal ordinate that draws the starting point

FillRect draws a fill-color rectangle (the default black), and Strokerect draws a rectangle with a border and no padding.


Two draw lines

Context.beginpath ();  Start path Context.moveto (x, y);  Starting point Context.lineto (x, y);  End Context.closepath ();   End Path Context.stroke (); Drawing path Outlines

Three Draw round

Context.beginpath (); Context.arc (x,y,radius,startangle,endangle,anticlockwise); Context.closepath (); Context.fill ();

It means: Starting from a position with a radius from the origin and an angle of the starting angle, the end point falls at a position where the distance from the origin is one radius and the angle is the end angle, thus drawing a pie-shaped sector.

The starting angle and end angle Here are the angle between the radius and the x-axis, and the Radian system is used.

The last parameter is a Boolean value, True is counterclockwise, and false represents a clockwise drawing.

So draw a circle with a center coordinate of (100,100) and a radius of 50 can be written as:

Context.arc (100,100,50,0,math.pi*2,false);


Beginner canvas-Draw Basic graphics

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.