In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use Canvas frame animation to eat Apple Mini Game in html5. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The code is as follows
Whether the frame animation ate again and had a rest and continued to eat const canvas = document.getElementById ("canvas") canvas.style.border = "1px solid black" const ctx = canvas.getContext ("2d") const img = new Image () / / create picture object let timer / / timer identifier let millisec = 300 / / execution interval let colIndex = 0 / / number of columns let rowIndex = 0 / / number of rows const timerFun = () = > {/ / declare timer execution function console.log ("set timer") Ctx.clearRect (0,0, canvas.style.width, canvas.style.height) / / clear canvas if (rowIndex
< 3) { // 如果是前5帧 ctx.drawImage(img, colIndex * 240, rowIndex * 240, 200, 200, 50, 50, 200, 200) // 图片对象,x坐标,y坐标(注:图片上定位的坐标),width,height(图片上截取的大小),x坐标,y坐标(注:图片在画布上的起点,即左上角),width,height(缩放,不是裁剪) colIndex++ // 下一帧 if (colIndex >4) {colIndex = 0 rowIndex++}} else {colIndex = 0 rowIndex = 0}} img.onload = () = > {timer = setInterval (timerFun) Millisec)} img.src = "image/apple.jpg" const startBtn = document.getElementsByClassName ('start-btn') [0] const endBtn = document.getElementsByClassName (' end-btn') [0] const pauseBtn = document.getElementsByClassName ('pause-btn') [0] const continueBtn = document.getElementsByClassName (' continue-btn') [0] startBtn.addEventListener ('click') () = > {console.log ("Click start", timer) clearInterval (timer) colIndex = 0 / / number of columns rowIndex = 0 / / number of rows timer = setInterval (timerFun, millisec)}) endBtn.addEventListener ('click', () = > {console.log ("Click to end") Timer) clearInterval (timer) colIndex = 0 rowIndex = 0 ctx.drawImage (img, colIndex * 240,200,200,50,50,200,200) timer = 0}) pauseBtn.addEventListener ('click', () = > {console.log ("Click pause") Timer) clearInterval (timer) timer = 0}) continueBtn.addEventListener ('click', () = > {if (timer) {alert (' eating) Don't rush') return} console.log ("Click to continue", timer) timer = setInterval (timerFun, millisec)}) Thank you for reading! This is the end of the article on "how to eat Apple Mini Game with Canvas frame animation in html5". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.