主頁(yè) > 知識(shí)庫(kù) > Canvas 幀動(dòng)畫吃蘋果小游戲

Canvas 幀動(dòng)畫吃蘋果小游戲

熱門標(biāo)簽:南昌仁和怎么申請(qǐng)開通400電話 電話機(jī)器人黑斑馬免費(fèi) 如何獲取地圖標(biāo)注客戶 拓展地圖標(biāo)注 機(jī)器人外呼系統(tǒng)存在哪些能力 平?jīng)龅貓D標(biāo)注位置怎么弄 高德地圖標(biāo)注地點(diǎn)糾錯(cuò) 電話機(jī)器人電銷系統(tǒng)掙話費(fèi) 只辦理400電話

先看頁(yè)面效果。下面四個(gè)按鈕分別表示開始、結(jié)束、暫停、繼續(xù)

下面是幀動(dòng)畫圖片素材:

幀動(dòng)畫的實(shí)現(xiàn),關(guān)鍵是Canvas API ctx.drawImage() (9個(gè)參數(shù))和 setInterval 定時(shí)器。
設(shè)置圖片的視圖窗口,每次執(zhí)行定時(shí)任務(wù),位移展示下一幀圖片。

直接上代碼,Ctrl+C/V 即插即用

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>幀動(dòng)畫</title>
    </head>
    <body>
        <canvas id="canvas" width="400" height="300"></canvas>
        <div class="">
            <button class="start-btn" type="button">重新吃</button>
            <button class="end-btn" type="button">不吃了</button>
            <button class="pause-btn" type="button">歇一歇</button>
            <button class="continue-btn" type="button">繼續(xù)吃</button>
        </div>
        <script type="text/javascript">
            const canvas = document.getElementById("canvas")
            canvas.style.border = "1px solid black"
            const ctx = canvas.getContext("2d")

            const img = new Image() // 創(chuàng)建圖片對(duì)象
            let timer // 定時(shí)器標(biāo)識(shí)符
            let millisec = 300 // 執(zhí)行時(shí)間間隔
            let colIndex = 0 // 列數(shù)
            let rowIndex = 0 // 行數(shù)
            const timerFun = () => { // 聲明定時(shí)器執(zhí)行函數(shù)
                console.log("設(shè)置定時(shí)器");
                ctx.clearRect(0, 0, canvas.style.width, canvas.style.height) // 清除畫布

                if (rowIndex < 3) { // 如果是前5幀
                    ctx.drawImage(img, colIndex * 240, rowIndex * 240, 200, 200, 50, 50, 200, 200) // 圖片對(duì)象,x坐標(biāo),y坐標(biāo)(注:圖片上定位的坐標(biāo)),width,height(圖片上截取的大?。瑇坐標(biāo),y坐標(biāo)(注:圖片在畫布上的起點(diǎn),即左上角),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("點(diǎn)擊開始", timer)
                clearInterval(timer)
                colIndex = 0 // 列數(shù)
                rowIndex = 0 // 行數(shù)
                timer = setInterval(timerFun, millisec)
            })
            endBtn.addEventListener('click', () => {
                console.log("點(diǎn)擊結(jié)束", timer)
                clearInterval(timer)
                colIndex = 0
                rowIndex = 0
                ctx.drawImage(img, colIndex * 240, rowIndex * 240, 200, 200, 50, 50, 200, 200)
                timer = 0
            })
            pauseBtn.addEventListener('click', () => {
                console.log("點(diǎn)擊暫停", timer)
                clearInterval(timer)
                timer = 0
            })
            continueBtn.addEventListener('click', () => {
                if (timer) {
                    alert('吃著呢,別催')
                    return
                }
                console.log("點(diǎn)擊繼續(xù)", timer)
                timer = setInterval(timerFun, millisec)
            })
        </script>
    </body>
</html>

到此這篇關(guān)于Canvas 幀動(dòng)畫吃蘋果小游戲的文章就介紹到這了,更多相關(guān)Canvas 幀動(dòng)畫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!

標(biāo)簽:永州 棗莊 漯河 西藏 遼源 青島 新疆 池州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Canvas 幀動(dòng)畫吃蘋果小游戲》,本文關(guān)鍵詞  Canvas,幀,動(dòng)畫,吃,蘋果,小游戲,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Canvas 幀動(dòng)畫吃蘋果小游戲》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Canvas 幀動(dòng)畫吃蘋果小游戲的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章