1 <!DOCTYPE HTML>2 <HTML>3 <HeadLang= "en">4 <MetaCharSet= "UTF-8">5 <title></title>6 7 <style>8 Body{background:#fff;}9 Div{width:800px;Height:600px;margin:0 Auto;}Ten Canvas{background:#000;} One </style> A <Scriptsrc= "Drawstars.js"></Script> - </Head> - <Body> the <Div> - <CanvasID= "CANVAS1"width= "$"Height= "All"></Canvas> - </Div> - </Body> + </HTML>
1Window.onload =function(){2 3 varCVS = document.getElementById (' canvas1 ');4 varCXT = Cvs.getcontext (' 2d ');5 varR;6 7 varLinegrad = Cxt.createradialgradient (cvs.width/2,cvs.height,0,cvs.width/2, cvs.height,cvs.width);8Linegrad.addcolorstop (0, ' #191970 ');9Linegrad.addcolorstop (1, ' #000 ');TenCxt.fillstyle =Linegrad; OneCxt.fillrect (0,0, cvs.width,cvs.height); A - //Star - for(vari=0;i<100;i++){ theR = 2+5*math.random (); -Drawstar (Cxt,cvs.width*math.random (), Cvs.height*0.7*math.random (), R, ' #FFFF00 '); - } - + //Moon -Drawmoon (cxt,600,100,40,3,30, ' #FFF68F ') + A //Lawn at Cxt.save (); - - Cxt.beginpath (); -Cxt.moveto (0,400); -Cxt.beziercurveto (200,300,500,500,800,450); -Cxt.lineto (800,600); inCxt.lineto (0,800); - Cxt.closepath (); to +Cxt.fillstyle = ' green '; - Cxt.fill (); the * Cxt.restore (); $ Panax Notoginseng - the } + A functionDrawmoon (cxt,x,y,r,d,rot,fillcolor) { the Cxt.save (); + - cxt.translate (x, y); $ Cxt.scale (r,r); $Cxt.rotate (rot*math.pi/180); - - Moonpath (cxt,d); the -Cxt.fillstyle = ' Yellow ' | |FillColor;Wuyi the Cxt.fill (); - Wu Cxt.restore (); - } About $ functionMoonpath (cxt,d) { - Cxt.beginpath (); -Cxt.arc (0,0,1,0.5*math.pi,1.5*math.pi,true); -Cxt.moveto (0,-1); ACxt.arcto (D,0,0,1,MATH.SQRT (1+d*d)/d); + } the - functionDrawstar (cxt,x,y,r,fillcolor) { $ Cxt.save (); the the the cxt.translate (x, y); theCxt.rotate (360*math.random () *math.pi/180); - Cxt.scale (r,r); in the Starpath (CXT); the AboutCxt.fillstyle =FillColor; the the Cxt.fill (); the Cxt.restore (); + - the Bayi } the functionStarpath (CXT) { the Cxt.beginpath (); - for(vari=0;i<5;i++){ -Cxt.lineto (Math.Cos (18+72*i) *math.pi/180),-math.sin ((18+72*i) *math.pi/180)); theCxt.lineto (Math.Cos ((54+72*i) *math.pi/180) *0.5,-math.sin ((54+72*i) *math.pi/180) *0.5); the } the Cxt.closepath (); the}
This example comes from the learning of Mu-class network, interested friends can go to study oh ~
HTML5 Self-study note []canvas Plot of the Starry meadow