Learn how to eat a ball on canvas
Finally, export the image and image encoding.
<! Doctype HTML>
<HTML>
<Head>
<Meta charset = "UTF-8">
<Title> untitled document </title>
<Style>
* {Margin: 0; padding: 0}
</Style>
</Head>
<Body>
<Canvas id = "mycanvas" width = "800" Height = "500" style = "border: 1px solid # c3c3c3;"> </canvas>
<SCRIPT type = "text/JavaScript">
VaR canvas = Document. getelementbyid ('mycanvas ');
VaR c = Document. getelementbyid ("mycanvas ");
VaR cxt = C. getcontext ("2D ");
VaR start1 = 60, radius1 = 50, size = 3, start2= 250, radius2 = 20;
FN ();
Function FN ()
{
Cxt. clearrect (0, 0, canvas. Width, canvas. Height );
Cxt. fillstyle = "# ff0000 ";
Cxt. beginpath (); // start a path or reset the current path.
Cxt. ARC (start1, 200, radius1, math. Pi * 0.2, math. Pi * 1.8, false); // create an ARC/curve (used to create a circle or partial circle)
Cxt. lineto (start1, 200 );
Cxt. closepath (); // create the path from the current point to the start point
Cxt. Fill (); // fill the current drawing (PATH)
For (VAR I = 0; I <size; I ++)
{
Getcircle (start2 + I * 200,200, radius2, 0, 'blue ')
};
Function getcircle (X, Y, R, S, color ){
Cxt. fillstyle = color;
Cxt. beginpath ()
Cxt. ARC (X, Y, R, S, math. Pi * 2 );
Cxt. Fill ();
};
Start1 + = 10;
If (start1 + radius1> start2 + 10)
{
Size --;
Start2 ++ = 200;
SetTimeout (function (){
Cxt. clearrect (0, 0, canvas. Width, canvas. Height );
Cxt. fillstyle = 'red ';
Cxt. beginpath ();
Cxt. ARC (start1, 200, radius1, 0, math. Pi * 2 );
Cxt. closepath ();
Cxt. Fill ();
For (VAR I = 0; I <size; I ++)
{
Getcircle (start2 + I * 200,200, radius2, 0, 'blue ')
};
Start1 + = 10;
If (size> 0)
{
SetTimeout (FN, 30)
} Else
{
VaR type = 'png ';
VaR imgdata = canvas. todataurl (type );
Document. Body. innerhtml = imgdata;
VaR oimg = Document. createelement ('img ');
Oimg. src = imgdata;
Document. Body. appendchild (oimg );
};
},100 );
}
Else
{
SetTimeout (FN, 30 );
};
};
</SCRIPT>
</Body>
</Html>
Learn how to eat a ball on canvas