ccapture.js icon indicating copy to clipboard operation
ccapture.js copied to clipboard

How to use with fabric.js?

Open let4be opened this issue 9 years ago • 13 comments

I'm trying to use this library with fabric.js

Capturing like this

canvas.on('after:render', () => {
    capturer.capture( $canvas );
});

it works and saves the data, but it's low fps, I specified 120 fps in ccapture but always get ~40

let4be avatar Mar 29 '16 18:03 let4be

Interesting. Could you post a working online example of what you're trying? It'll help me debug and figure out what's going on.

spite avatar Mar 29 '16 18:03 spite

any updates on this?

galacto avatar Apr 11 '17 09:04 galacto

Not really, but as long as you render to a canvas with non-frame dependant animations, and pass the canvas to CCapture, you should be good.

spite avatar Apr 11 '17 23:04 spite

I have used this library to save a fabric.js animations and videos as a video file. It worked perfectly for me. I used the following code:

const canvas = document.getElementById('canvas');

fabric.util.requestAnimFrame(function render() {
    fabric.util.requestAnimFrame(render);
    capturer.capture(canvas);
});

MoSofi avatar Jun 26 '19 19:06 MoSofi

@DevelopSmith can u help me with some working example as i am unable to make it work

myselfgaurav avatar Oct 03 '19 09:10 myselfgaurav

I think ccapture can't capture fabric js. I looked into fabric's source and it's using new Date() for lots of things and I believe that's just not being overwritten by ccapture:

Methods supported so far:

Date.now, Date.prototype.getTime
setTimeout, clearTimeout, setInterval (clearInterval pending)
requestAnimationFrame
performance.now
HTMLVideoElement.prototype.currentTime, HTMLAudioElement.prototype.currentTime

but please, correct me if I'm wrong because I need this to work

Mat-thieu avatar Oct 31 '19 14:10 Mat-thieu

It might be similar to https://twitter.com/thespite/status/733254647922753536 Can you post code that i can run to try to reproduce the issue?

spite avatar Nov 01 '19 00:11 spite

To solve it I used a custom implementation using https://github.com/schickling/timemachine to overwrite the Date constructor

Edit: I was wrong, I used this for a backend implementation sorry

Mat-thieu avatar Nov 01 '19 08:11 Mat-thieu

It would be really nice if anyone could give an example on Fiddle, cos I'm really lost here. Here's my starting point: https://jsfiddle.net/BrandApp_io/cgno71r9/64/ But it skips way too many frames.

KristofferBerg avatar Nov 15 '19 00:11 KristofferBerg

@KristofferBerg Does this solve it? I find it hard to test

https://jsfiddle.net/93bkform/

I moved ccapture script load above fabric, removed the defer attribute. Then made sure to launch ccapture first so it might be able to work its magic on fabric before fabric starts. I also moved your onChange on the .animate() function to the render loop canvas.renderAll.

If this doesn't solve it, I believe fabric uses a rendering loop in .animate() that ccapture maybe can't overwrite.

Mat-thieu avatar Nov 18 '19 12:11 Mat-thieu

@KristofferBerg sorry it doesn't solve it, ran a few tests and it's inconsistent.. :/

Mat-thieu avatar Nov 18 '19 12:11 Mat-thieu

@Mat-thieu Thanks for testing. Yeah, I'm starting to suspect this line https://github.com/fabricjs/fabric.js/blob/master/src/util/animate.js#L73 have something to do with it :/

KristofferBerg avatar Nov 18 '19 12:11 KristofferBerg

The issue seems to be here. It's using the initial window.requestAnimationFrame, not the "patched" one.

This code should "proxy" the call, just add it before loading fabric.js:

<script>
  (function() {
    var __ = window.requestAnimationFrame;

    window.requestAnimationFrame = function() {
      var args = arguments;
      (args.callee === args.callee.caller ? __ : window.requestAnimationFrame).apply(window, args);
    };
  })();
</script>

dan-bruma avatar Jun 13 '21 05:06 dan-bruma