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