【開源】小程序、小游戲和Web運動引擎 to2to 發布


簡單輕量跨平台的 Javascript 運動引擎

to2to 中文念 '兔兔兔',作為 cax 內置的運動套件獨立出一個package ,因為它本身可以和平台環境運動對象無關。既可運動 dom,也可運動 cax 內置對象,也可運動對象子面量。眾所周知,運動需要循環的 tick 去不斷執行偏移函數,小程序,小游戲和web各瀏覽器的 相應的 API 還是有差異,to2to 抹平了這些差異,讓你使用同樣的api可以在不同環境暢快運動。

特性

  • 超輕量級的代碼體積
  • 支持周期運動
  • 支持並行與串行運動
  • 運動一切(Canvas、DOM、WebGL、SVG、Object..)
  • 支持小程序、小游戲以及 Web 瀏覽器用相同簡介的 API 運動

一分鍾入門 to2to 使用

通過 npm 安裝或者 cdn 下載下來在 HTML 引用該腳本:

npm i to2to

使用:

import To from 'to2to'

const ele = document.querySelector('#animateEle')

To.get({ rotate: 0, x: 0, y: 0 })
    .to({ rotate: 720, x: 200, y: 200 }, 1600, To.easing.elasticInOut)
    .progress(function () {
        ele.style.transform = `translate(${this.x}px, ${this.y}px) rotate(${this.rotate}deg)`
    })
    .start()

在 cax 中使用 to2to

cax 內置了 to 的能力以連綴的方式寫運動效果:

const easing = cax.To.easing.elasticInOut

cax.To.get(bitmap)
    .to({ y: 340, rotation: 240 }, 2000, easing)
    .begin(() => {
        console.log("Task one has began!")
    })
    .progress(() => {
        console.log("Task one is progressing!")
    })
    .end(() => {
        console.log("Task one has completed!")
    })
    .wait(500)
    .to()
    .rotation(0, 1400, easing)
    .begin(() => {
        console.log("Task two has began!")
    })
    .progress(() => {
        console.log("Task two is progressing!")
    })
    .end(() => {
        console.log("Task two has completed!")
    })
    .start();
  • toto 之間的是並行
  • towait 之前的是串行
  • toto 之間的 與 下一個 toto 之間的是串行

有點繞,但是很直觀,慢慢體會。

如果想要循環播放的話可以使用 cycle 方法:

cax.To.get(bitmap)
    .to()
    .y(340, 2000, cax.easing.elasticInOut)
    .to
    .y(0, 2000, cax.easing.elasticInOut)
    .cycle()
    .start()

運動演示地址

自定義動畫

通過 animate 方法可以使用自定義的動畫:

const stage = new cax.Stage(300, 400, 'body')
const bitmap = new cax.Bitmap('./wepay-diy.jpg', function () {
    var eio = To.easing.elasticInOut
    To.get(bitmap).animate('rubber').start()
})

bitmap.x = 150
bitmap.y = 200
bitmap.originX = 40
bitmap.originY = 40
stage.add(bitmap)

cax.setInterval(() => {
    stage.update()
}, 16)

to2to 內置了少數幾個自定義動畫

  • rubber
  • bounceIn
  • flipInX
  • zoomOut

擴展自定義動畫

內置的不夠用?自己動手豐衣足食:

比如 customAnimation 就是通過下面實現的:

To.extend('customAnimation', [['to', ['scaleX', {
  '0': 0,
  '1': 400,
  '2': To.easing.elasticInOut
}], ['scaleY', {
  '0': 0,
  '1': 400
}]]])  

索引為 2 的 easing 可以傳可不傳,不傳代表線性勻速變化。

使用剛剛定義的自定義動畫:

To.get(obj).animate('customAnimation').start()

誰在使用?

Tencent Wechat Tencent QQ

License

MIT


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM