Lottie Animation in fabricjs canvas

Is it possible to load the Lottie animation in fabricjs canvas

I have tried the following samples

    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();

I cant able to add the animation in the fabric js canvas. if any one overcome this issue kindly do comments on it

Upvotes: 0

Views: 2102

Answers (3)

sasha.pianist
sasha.pianist

Reputation: 1

If of any help, I've created this Lottie class with the support of exporting 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

To create Lottie element just pass JSON

const fabricImage = new fabric.Lottie('https://assets5.lottiefiles.com/private_files/lf30_rttpmsbc.json', {
          scaleX: 0.5,
        })
canvas.add(fabricImage)

Upvotes: 0

shkaper
shkaper

Reputation: 4998

I might be late to answer this, but for anyone else looking, this pen could give you some pointers: https://codepen.io/shkaper/pen/oEKEgG

The idea here, first of all, is to extend fabric.Image class overriding its internal render method to render the contents of an arbitrary canvas that you yourself provide:

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

You can make this canvas a constructor argument, e.g.

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

Then you'll just need to use lottie's canvas renderer to draw animation on a canvas and pass it to your new fabric.AEAnimation object.

Upvotes: 3

Filipe Silva
Filipe Silva

Reputation: 220

I would assume so, by combining your code with something similar to https://itnext.io/video-element-serialization-and-deserialization-of-canvas-fc5dbf47666d. Depending on your scenario you might be able to get away with using something like http://fabricjs.com/interaction-with-objects-outside-canvas

Upvotes: 0

Related Questions