主頁 > 知識(shí)庫 > canvas版人體時(shí)鐘的實(shí)現(xiàn)示例

canvas版人體時(shí)鐘的實(shí)現(xiàn)示例

熱門標(biāo)簽:通遼地圖標(biāo)注app 江西ai電銷機(jī)器人如何 地圖標(biāo)注員工作內(nèi)容 中國地圖標(biāo)注城市的 高德地圖標(biāo)注廁所 威海語音外呼系統(tǒng)平臺(tái) 智能語音電銷機(jī)器人客戶端 地圖標(biāo)注沿海城市房價(jià) 西安金倫外呼系統(tǒng)

不知道老網(wǎng)民們還記不記得這個(gè)魔性的時(shí)鐘插件:

作為網(wǎng)上沖浪十?dāng)?shù)載的網(wǎng)蟲,不久前看到這個(gè)圖瞬間破防,直接夢回10年前的QQ空間,感嘆一下歲月蹉跎、時(shí)光荏苒、青春不在、不勝唏噓。

十年后的今天,flash在各個(gè)瀏覽器都已經(jīng)不再支持,已經(jīng)是個(gè)不再那個(gè)只會(huì)用腳本打扮QQ空間的我,自然得用青春換來得技術(shù)還原一下自己的青春記憶。

首先感謝原作者提供的如此優(yōu)秀好玩的插件: http://chabudai.org/blog/?p=59

這次為了圖方便,就直接那pixi.js來上手做動(dòng)畫了,動(dòng)畫素材來源于油管視頻,拿到PS逐幀摳圖并導(dǎo)出,這個(gè)過程就不再詳細(xì)介紹了。合成后的精靈圖以及對(duì)應(yīng)的json文件我會(huì)放在文章末尾。

核心的API是PIXI.AnimatedSprite。

代碼也是很短,就直接放到下面了

<!doctype html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>HoneHoneClock</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }
    html, body {
      width: 100%;
      height: 100%;
    }
    body {
      background-color: lightcyan;
    }
    canvas {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      z-index: 1;
    }
  </style>
</head>
<body>
<canvas class="canvas"></canvas>
<script src="pixi_5.3.4.min.js"></script>
<script src="Stats.min.js"></script>
<script>

  (async function () {
    const stats = new Stats()
    document.body.appendChild(stats.domElement)

    let pageWidth = 0
    let pageHeight = 0

    let clockHour1, clockHour2
    let clockMin1, clockMin2
    let clockSec1, clockSec2
    const $canvas = document.querySelector('canvas')
    const renderer = new PIXI.Renderer({
      view: $canvas,
      width: pageWidth,
      height: pageHeight,
      transparent: true,
      autoDensity: true,
      antialias: true
    })

    // 人體時(shí)鐘
    class Clock extends PIXI.Container {
      constructor (name) {
        super()
        const textures = loader.resources[honeHoneClockJson].textures
        let frames = []
        let aniData = []
        if (this.frames) {
          frames = this.frames
          aniData = this.aniData
        }
        else {
          aniData = [
            {
              prefix: '0',
              count: 6,
              frameFrom: -1,
              frameTo: -1,
            },
            {
              prefix: '1',
              count: 9,
              frameFrom: -1,
              frameTo: -1,
            },
            {
              prefix: '2',
              count: 7,
              frameFrom: -1,
              frameTo: -1,
            },
            {
              prefix: '3',
              count: 6,
              frameFrom: -1,
              frameTo: -1,
            },
            {
              prefix: '4',
              count: 9,
              frameFrom: -1,
              frameTo: -1,
            },
            {
              prefix: '5',
              count: 14,
              frameFrom: -1,
              frameTo: -1,
            },
            {
              prefix: '6',
              count: 7,
              frameFrom: -1,
              frameTo: -1,
            },
            {
              prefix: '7',
              count: 10,
              frameFrom: -1,
              frameTo: -1,
            },
            {
              prefix: '8',
              count: 7,
              frameFrom: -1,
              frameTo: -1,
            },
            {
              prefix: '9',
              count: 9,
              frameFrom: -1,
              frameTo: -1,
            },
          ]
          let k = 0
          for (let i = 0; i < aniData.length; i++) {
            const data = aniData[i]
            data.frameFrom = k
            for (let j = 1; j <= data.count; j++) {
              k++
              frames.push(textures[`${data.prefix}(${j}).png`])
            }
            data.frameTo = k - 1
          }
          this.frames = frames
          this.aniData = aniData
        }
        const ani = new PIXI.AnimatedSprite(frames)
        ani.anchor.set(0.5, 1)
        ani.animationSpeed = 0.4

        this.stopAt = -1
        ani.onFrameChange = () => {
          if (ani.currentFrame === this.stopAt) {
            ani.stop()
          }
        }
        this.addChild(ani)
        this.name = name
        this.ani = ani
        this.num = -1
      }

      set number (number) {
        if (this.num !== number) {
          this.num = number
          this.stopAt = this.aniData[number].frameTo
          this.ani.gotoAndPlay(this.aniData[number].frameFrom)
        }
      }
    }

    const stage = new PIXI.Container()
    stage.name = 'stage'
    let clockWrap

    const ticker = new PIXI.Ticker()
    let now = new Date()
    let lastTime = now.getTime()
    const loop = function () {
      stats.begin()
      now = new Date()
      if (now.getTime() - lastTime >= 1000) {
        let hours = now.getHours()
        if (hours > 9) {
          clockHour1.number = Math.floor(hours / 10)
          clockHour2.number = hours % 10
        }
        else {
          clockHour1.number = 0
          clockHour2.number = hours
        }

        let minutes = now.getMinutes()
        if (minutes > 9) {
          clockMin1.number = Math.floor(minutes / 10)
          clockMin2.number = minutes % 10
        }
        else {
          clockMin1.number = 0
          clockMin2.number = minutes
        }

        let seconds = now.getSeconds()
        if (seconds > 9) {
          clockSec1.number = Math.floor(seconds / 10)
          clockSec2.number = seconds % 10
        }
        else {
          clockSec1.number = 0
          clockSec2.number = seconds
        }
        lastTime = now.getTime()
      }
      renderer.render(stage)
      stats.end()
    }

    ticker.add(loop)

    const honeHoneClockJson = 'HoneHoneClock.json'
    const loader = new PIXI.Loader()
    loader.add([honeHoneClockJson])
    loader.onComplete.add(async (res) => {
      clockWrap = new PIXI.Container()
      clockWrap.position.set((pageWidth - 630) * 0.5, (pageHeight + 150) * 0.5)

      clockHour1 = new Clock('hour')
      clockHour2 = new Clock('hour')
      clockMin1 = new Clock('min')
      clockMin2 = new Clock('min')
      clockSec1 = new Clock('sec')
      clockSec2 = new Clock('sec')
      clockHour1.position.set(0, 0)
      clockHour2.position.set(100, 0)
      clockMin1.position.set(250, 0)
      clockMin2.position.set(350, 0)
      clockSec1.position.set(500, 0)
      clockSec2.position.set(600, 0)
      clockWrap.addChild(clockHour1)
      clockWrap.addChild(clockHour2)
      clockWrap.addChild(clockMin1)
      clockWrap.addChild(clockMin2)
      clockWrap.addChild(clockSec1)
      clockWrap.addChild(clockSec2)
      stage.addChild(clockWrap)

      // 開始動(dòng)畫循環(huán)
      ticker.start()
    })
    loader.load()

    const onResize = (e) => {
      pageWidth = document.body.clientWidth
      pageHeight = document.body.clientHeight
      if (clockWrap) {
        clockWrap.position.set((pageWidth - 630) * 0.5, (pageHeight + 150) * 0.5)
      }
      renderer.resize(pageWidth, pageHeight)
    }

    onResize()

    window.onresize = onResize
  })()
</script>
</body>
</html>

完整代碼戳這里

在線演示1 、 在線演示2

到此這篇關(guān)于canvas版人體時(shí)鐘的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)canvas人體時(shí)鐘內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!

標(biāo)簽:眉山 崇左 青海 北海 晉中 阜陽 營口 河池

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《canvas版人體時(shí)鐘的實(shí)現(xiàn)示例》,本文關(guān)鍵詞  canvas,版,人體,時(shí)鐘,的,實(shí)現(xiàn),;如發(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版人體時(shí)鐘的實(shí)現(xiàn)示例》相關(guān)的同類信息!
  • 本頁收集關(guān)于canvas版人體時(shí)鐘的實(shí)現(xiàn)示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章