Magics-Group / wcjs-renderer

Renderer JS API for WebChimera.js
http://webchimera.org/
MIT License
40 stars 24 forks source link

options object / preserveDrawingBuffer #7

Closed shenanigans closed 8 years ago

shenanigans commented 8 years ago

I've been generating thumbnails for video files with webchimera and needed to support the preserveDrawingBuffer option for getContext. This lets you work with the canvas' frame data using normal canvas methods.

This pull request shouldn't break any code but adds the following invocation option:

var chimera = require ('wcjs-renderer');
chimera.init (myCanvas, [], {
  fallbackRenderer: false,
  preserveDrawingBuffer: true
});
RSATom commented 8 years ago

Thanks! I'll look soon.

@Ivshti, do you use wcjs-renderer and fallbackRenderer option? If yes, this patch could break your code.

Ivshti commented 8 years ago

@RSATom I don't use wcjs-renderer at all because I implemented it before that.

You have to be careful with preserveDrawingBuffer - it's a big performance penalty

shenanigans commented 8 years ago

Passing a Boolean for fallbackRenderer is detected and handled so it should be safe for users of that option as long as they aren't abusing truthy values. I'm comfortable with the performance penalty of preserveDrawingBuffer when generating thumbnails as I only let frameReady go off twice. File loading and seeking are the real costs there. I don't want to put up with it anywhere else, hence bothering to make it optional.

Ivshti commented 8 years ago

@shenanigans I agree, and you make a good point. What I'm doing personally is creating a new canvas for snapshotting, then using the last value from wcjs onFrameReady and rendering in, by re-using some of the renderer code. It's a bit extra work, but it uses preserveDrawingBuffer only when completely needed - therefore eliminating any performance penalty.

RSATom commented 8 years ago

Is it not possible create image from js array without using canvas?

Ivshti commented 8 years ago

Of course it is, but why write logic & implement image compression if canvas provides that for you?

RSATom commented 8 years ago

I just think it could have some overhead.

Ivshti commented 8 years ago

I bet it would be faster than having V8 do your rendering logic and png/jpg compression, since is implemented natively after all.

Doesn't matter though, I doubt there's a usecase where performance matters much for taking snapshots

shenanigans commented 8 years ago

If you want a PNG out your best performance option will probably involve a canvas at some point. I found it convenient to let canvas and wcjs-renderer take care of certain crucial tasks, such as having a clue what I420 is. That frame conversion step is certainly going to be faster in webgl than a javascript loop, probably enough to make wrangling a canvas worth it. By the way, I've been reusing the same canvas instance forever without any problems.

RSATom commented 8 years ago

Applied, thanks!