1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <style>5 #canvas{6 Background-color:#cccccc;7 8 }9 </style>Ten <Script> One window.onload=function(){ A varCanvas=document.getElementById ("Canvas"); - varCObj=Canvas.getcontext ("2d"); - //var num=150; the vararr=[]; - //var angle=0; - SetInterval (function(){ - Cobj.clearrect (0,0, -, -); + for(varI=0; I<Arr.length;i++) - { + A Cobj.save (); at Cobj.translate ( -, -); - Cobj.scale (Arr[i].scale,arr[i].scale); - cobj.rotate (Arr[i].angle*Math.PI/);//The center of the rotation is defined as the origin position of the canvas before the rotation, and the position of the origin is changed only to the subsequent drawing, and the following rectangle is where the origin is (50,50), and the center of rotation is at (100,100) - Cobj.beginpath (); - Cobj.fillstyle=Arr[i].color; - Cobj.rect (Arr[i].num,arr[i].num, -, -); in Cobj.fill (); - Cobj.restore (); to } + - the }, -) * SetInterval (function(){ $ for(varI=0; I<Arr.length;i++)Panax Notoginseng { - if(Arr[i].num<=0) the { + Arr.splice (i,1);//remove the current element; A Continue;//at the same time exit this cycle the + } - //changes in the storage settings are important, if the change of the storage is not set up, it may not be able to achieve the effect $ Arr[i].angle+=2; $ Arr[i].num-=0.2; - Arr[i].scale-=0.002; - if(Arr[i].scale<=0.2) the Arr[i].scale=0.2; - }Wuyi the }, -) - SetInterval (function(){ Wu varrect={angle:0, Num: Max, Scale:1, Color:"RGB ("+parseint (Math.random ()*255)+","+parseint (Math.random ()*255)+","+parseint (Math.random ()*255)+")"} ; - Arr.push (rect); About }, +) $ - } - </Script> - </Head> A <Body> + <CanvasID= "Canvas"width=600pxHeight=600px> the your browser is out of date! - </Canvas> $ the </Body> the </HTML>
Create Kaleidoscope animation effects with HTML5 's canvas