Use canvas technology to draw flowers on a Web page

Source: Internet
Author: User

After the drawing:

1. Create a canvas element

Add a canvas element to an HTML 5 page.

Specify the ID, width, and height of the element:

<canvas id= "Canvas" width= "1024x768" height= "768" style= "border:1px solid #aaa;d isplay:block;margin:50 auto;" ></canvas>

2. In the JS page on the line painting operation

var Context=canvas.getcontext ("2d");


3. Use JavaScript to draw petals

The canvas element itself is not capable of drawing. All the drawing work must be done inside JavaScript:

First, a petal is drawn, the center coordinates of the petals are (450,200), the petal's radius is 50, and the petal's fill color is pink. Other petals and stamens are plotted in the same way as the following code:

Context.beginpath ();

Context.arc (450,200,50,0,0.5*math.pi,true);

Context.stroke ();

Context.fill ();







Source:
<!DOCTYPE HTML><HTML><Head><title>Canvas to draw flowers</title></Head><Body><CanvasID= "Canvas"width= "1024x768"Height= "768"style= "border:1px solid #aaa;d isplay:block;margin:50 auto;"></Canvas><Script>varCanvas=document.getElementById ("Canvas");varContext=Canvas.getcontext ("2d"); Context.linewidth=2; Context.strokestyle="Black"; Context.fillstyle= "#ED6E91";//petals on top rightContext.beginpath (); Context.arc (550, $, -,0,2*Math.PI); Context.stroke (); Context.fill () ;//Petals Right DownContext.beginpath (); Context.arc (550, -, -,0,2*Math.PI); Context.stroke (); Context.fill () ;//petals on top leftContext.beginpath (); Context.arc ( the, $, -,0,0.5*Math.PI,true); Context.stroke (); Context.fill ();//Petals Lower leftContext.beginpath (); Context.arc ( the, -, -,0,1.5*Math.PI); Context.stroke (); Context.fill () ;//StamensContext.beginpath (); Context.fillstyle= "#f90"; Context.arc ( -, -, -,0,2*Math.PI); Context.stroke (); Context.fill () ;//Flower PathContext.beginpath (); Context.arc ( Max, -, -,0,0.3*Math.PI); Context.stroke ();//the leaves on the rightContext.beginpath (); Context.fillstyle= "Green"; Context.arc (468, -, -,0,0.5*Math.PI); Context.closepath (); Context.stroke (); Context.fill () ;//leaves on the leftContext.beginpath (); Context.fillstyle= "Green"; Context.arc (468, -, -,0.5*Math.pi,math.pi,false); Context.closepath (); Context.stroke (); Context.fill ();</Script></Body></HTML>










Use canvas technology to draw flowers on a Web page

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.