The basic process of JS drawing is as follows:
- JS through the ID to find the canvas element, the following code:
var c=document.getElementById(‘myCanvas‘)
- Create a context object with the following code:
var cxt=c.getContext(‘2d‘)
2d is the only function parameter that might support 3D drawings in the future, and all the drawing work is done on the context object.
Canvas drawing-related objects
- Cxt.beginpath () Start drawing path
- Cxt.closepath () End drawing path
- Cxt.stroke () a real drawing
- Cxt.strokestyle= ' #fff ', set border color
- cxt.linewidth=2, setting the border size
- cxt.fillstyle= ' Red ' to set the content fill area color
- Cxt.arc (X,y,radius,startangle,endangle,true/false), draw a circular path with parameters of center coordinates (x, y), start and end angles, clockwise/counterclockwise
- Cxt.strokerect (X,y,width,height), drawing a rectangular path
- Cxt.fillrect (X,y,width,height), drawing a rectangular shape
- Cxt.createradialgradient (xstart,ystart,radiusstart,xend,yend,radiusend), Radial gradient range settings
- Cxt.createlineargradient (xstart,ystart,xend,yend), linear gradient range settings
HTML code:
<body> <p> <span onclick="Btn1_click ();" onmouseover="this.style.cursor= ' pointer ';" >Solid Circle</span> <span onclick="Btn2_click ();" onmouseover="this.style.cursor= ' pointer ';" >Border Circle</span> <span onclick="Btn3_click ();" onmouseover="this.style.cursor= ' pointer ';" >Connecting Circle</span> <span onclick="Btn4_click ();" onmouseover="this.style.cursor= ' pointer ';" >Gradient Circle</span> </P> <canvas id="MyCanvas" width= "400px" height=" 400px "></Canvas></body>
JS Code:
<script type="Text/javascript"> function $(ID) { returndocument.getElementById (ID); } function btn1_click(){ varcvs=$ (' MyCanvas ');varCxt=cvs.getcontext (' 2d '); Cxt.clearrect (0,0, -, -); Cxt.beginpath (); Cxt.arc ( -, -, -,0,Math. pi*2,true); Cxt.closepath (); cxt.fillstyle=' #eee '; Cxt.fill (); } function btn2_click(){ varcvs=$ (' MyCanvas ');varCxt=cvs.getcontext (' 2d '); Cxt.clearrect (0,0, -, -); Cxt.beginpath (); Cxt.arc ( -, -, -,0,Math. pi*2,true); Cxt.closepath (); cxt.strokestyle=' #666 '; Cxt.linewidth=2; Cxt.stroke (); } function btn3_click(){ varcvs=$ (' MyCanvas ');varCxt=cvs.getcontext (' 2d '); Cxt.clearrect (0,0, -, -); Cxt.beginpath (); Cxt.arc ( -, -, -,0,Math. pi*2,true); Cxt.closepath (); cxt.strokestyle=' #666 '; Cxt.linewidth=2; cxt.fillstyle=' #eee '; Cxt.fill (); Cxt.stroke (); Cxt.beginpath (); Cxt.arc ( the, -, -,0,Math. pi*2,true); Cxt.closepath (); cxt.strokestyle=' #666 '; Cxt.linewidth=2; Cxt.stroke (); } function btn4_click(){ varcvs=$ (' MyCanvas ');varCxt=cvs.getcontext (' 2d '); Cxt.clearrect (0,0, -, -); Cxt.beginpath (); Cxt.arc ( -, -, -,0,Math. pi*2,true); Cxt.closepath (); cxt.strokestyle=' #666 '; Cxt.linewidth=2;varGnt=cxt.createradialgradient ( -, -,0, -, -, -); Gnt.addcolorstop (0,' #f00 '); Gnt.addcolorstop (0.3,' #0f0 '); Gnt.addcolorstop (1,' #00f '); Cxt.fillstyle=gnt; Cxt.fill (); }</script>
:
Canvas Drawing Features