Use canvas to draw an animated image. Thank you for your advice.

Source: Internet
Author: User

Use canvas to draw an animated image. Thank you for your advice.

<! DOCTYPE html>
<Html lang = "en">
<Head>
<Meta charset = "UTF-8">
<Title> draw a villain animation </title>
<Style>
Canvas {
Border: 1px solid green;
}
</Style>
</Head>
<Body>
<! -- Draw a villain animation -->
<Canvas width = "400" height = "600" id = "canvas"> </canvas>
</Body>
<Script>
Var canvas = document. getElementById ('canvas ');
Var ctx = canvas. getContext ('2d ');
Function toAngle (radian ){
Return radian * 180/Math. PI;
}
Function toRadian (angle ){
Return angle * Math. PI/180;
}
Function img (ctx, element, x0, y0 ){
Var x = x0,
Y = y0;
Element. onload = function (){
Var width = element. width/4,
Height = element. height/4;
Var I = 0,
J = 0,
A = 0;
SetInterval (function (){
// X = x +;
Ctx. clearRect (x, y, width, height );
Ctx. drawImage (element, width * I, height * j, width, height, x, y, width, height );
I ++;
If (I = 4 ){
SetTimeout (function (){
I = 0;
If (j = 0 ){
J = 1;
// A-= 10;
} Else if (j = 1 ){
J = 3;
} Else if (j = 2 ){
J = 0;
} Else if (j = 3 ){
J = 2;
// A + = 10;
}
}, 20)
}
}, 200)
}
};
Var img1 = new Image ();
Img1.src = 'imgs/game1.png ';
Img (ctx, img1, 0, 0 );

// Here we encapsulate a function. In fact, from the perspective of performance optimization, We can first create a canvas label in the memory and place the picture in the canvas in the memory, then place the canvas created in the memory on the canvas label on the page. This is just an idea. I hope you can consider this.
</Script>
</Html>

Related Article

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.