0

是否可以在fabricjs画布中加载Lottie动画

我尝试了以下示例

    bodymovin.loadAnimation({
          wrapper: animateElement,       // div element
          loop: true,
          animType: 'canvas',            // fabricjs canvas
          animationData: dataValue,      // AE json
          rendererSettings: {
             scaleMode: 'noScale',
             clearCanvas: true, 
             progressiveLoad: false, 
             hideOnTransparent: true,
           }
       });
canvas.add(bodymovin);
canvas.renderAll();

我无法在织物 js 画布中添加动画。如果有人克服了这个问题,请对此发表评论

4

3 回答 3

3

我可能会迟到回答这个问题,但对于其他人来说,这支笔可以给你一些指示:https ://codepen.io/shkaper/pen/oEKEgG

首先,这里的想法是扩展 fabric.Image 类,覆盖其内部渲染方法,以渲染您自己提供的任意画布的内容:

fabric.AEAnimation = fabric.util.createClass(fabric.Image, {
  drawCacheOnCanvas: function(ctx) {
    ctx.drawImage(this._AECanvas, -this.width / 2, -this.height / 2);
  },
})

您可以将此画布作为构造函数参数,例如

initialize: function (AECanvas, options) {
  options = options || {}
  this.callSuper('initialize', AECanvas, options)
  this._AECanvas = AECanvas
},

然后您只需要使用lottie 的画布渲染器在画布上绘制动画并将其传递给您的新fabric.AEAnimation 对象。

于 2018-11-09T04:47:13.710 回答
0

我会这样假设,通过将您的代码与类似于https://itnext.io/video-element-serialization-and-deserialization-of-canvas-fc5dbf47666d的内容结合起来。根据您的情况,您可能可以使用类似http://fabricjs.com/interaction-with-objects-outside-canvas

于 2018-09-10T13:58:36.613 回答
0

如果有任何帮助,我创建了这个 Lottie 类,支持导出 toObject/JSON

import { fabric } from 'fabric'
import lottie from 'lottie-web'

const Lottie = fabric.util.createClass(fabric.Image, {
  type: 'lottie',
  lockRotation: true,
  lockSkewingX: true,
  lockSkewingY: true,
  srcFromAttribute: false,

  initialize: function (path, options) {
    if (!options.width) options.width = 480
    if (!options.height) options.height = 480

    this.path = path
    this.tmpCanvasEl = fabric.util.createCanvasElement()
    this.tmpCanvasEl.width = options.width
    this.tmpCanvasEl.height = options.height

    this.lottieItem = lottie.loadAnimation({
      renderer: 'canvas',
      loop: true,
      autoplay: true,
      path,
      rendererSettings: {
        context: this.tmpCanvasEl.getContext('2d'),
        preserveAspectRatio: 'xMidYMid meet',
      },
    })

    // this.lottieItem.addEventListener('DOMLoaded', () => {
    //   console.log('DOMLoaded')
    // })

    this.lottieItem.addEventListener('enterFrame', (e) => {
      this.canvas?.requestRenderAll()
    })

    this.callSuper('initialize', this.tmpCanvasEl, options)
  },

  play: function () {
    this.lottieItem.play()
  },
  stop: function () {
    this.lottieItem.stop()
  },
  getSrc: function () {
    return this.path
  },
})

Lottie.fromObject = function (_object, callback) {
  const object = fabric.util.object.clone(_object)
  fabric.Image.prototype._initFilters.call(object, object.filters, function (filters) {
    object.filters = filters || []
    fabric.Image.prototype._initFilters.call(object, [object.resizeFilter], function (resizeFilters) {
      object.resizeFilter = resizeFilters[0]
      fabric.util.enlivenObjects([object.clipPath], function (enlivedProps) {
        object.clipPath = enlivedProps[0]
        const fabricLottie = new fabric.Lottie(object.src, object)
        callback(fabricLottie, false)
      })
    })
  })
}

Lottie.async = true

export default Lottie

要创建 Lottie 元素,只需传递 JSON

const fabricImage = new fabric.Lottie('https://assets5.lottiefiles.com/private_files/lf30_rttpmsbc.json', {
          scaleX: 0.5,
        })
canvas.add(fabricImage)
于 2022-02-18T16:37:18.797 回答