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>