Everything is very simple, as long as the basic work is solid
The so-called industry is a pile of simple things
What's more, we can only start from the most basic, Iterative Construction of knowledge system??
First look at the picture:
2. The code is as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>A simple way to write frame animation</title> <style> #c1{ border: 1px solid; } </style> <script> window.onload = function(){ var c1 = document.getElementById('c1') var ctx= c1.getContext('2d') var canvasWidth = ctx.canvas.width; var canvasHeigh = ctx.canvas.height; var img = new Image(); img.onload = function(){ var imgWidth = img.width; var imgHeight = img.height; var personWidth = imgWidth/4; var personHeight = imgHeight/4; var x0 = canvasWidth/2 - personWidth/2; var y0 = canvasHeigh/2 - personHeight/2; var timer =null; var index = 0; c1.onmouseover = function(){ ctx.drawImage(img,0,0,personWidth,personHeight,x0,y0,personWidth,personHeight) clearInterval(timer) timer = setInterval(function(){ ctx.clearRect(0,0,canvasWidth,canvasHeigh); index++; ctx.drawImage(img,index*personWidth,0,personWidth,personHeight,x0,y0,personWidth,personHeight); if(index>=3){ index = 0; } },300) } c1.onmouseout = function(){ alert('sdf') } } img.src = "image/04.png" } </script> </head> <body> <canvas id="c1" width="500" height="500"></canvas> </body> </html>
Effect:
Draw the villain in the middle of the canvas, and then, when the mouse moves in, start a timer to change the picture constantly
Remove and end the timer!