Learn how to eat a ball on canvas

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.