cburgmer / rasterizeHTML.js

Renders HTML into the browser's canvas
http://cburgmer.github.io/rasterizeHTML.js
MIT License
2.45k stars 219 forks source link

Web worker support? #168

Open simonmysun opened 7 years ago

simonmysun commented 7 years ago

It seems taking very long time rendering large document especially those including MathJax rendered formulas. Since loading an image is asynchronously done by browser, this library seems to be the only thing stucking the browser. I've read the code and havn't found any part producing the data URL which need document object. I think it would be nice to support doing this inside a web worker.

P.S. I really love this project. If I were able to improve it I would really like to pull request. Thank you for providing it.

simonmysun commented 7 years ago

Actually I mean that the library should work in worker when it only receive HTML and produce Data URL. Or parsing HTML can be done with silently copying the element.

cburgmer commented 7 years ago

Hey, there actually is quite a bit of code that needs to have access to the document or window object:

1) calculating the required canvas size (https://github.com/cburgmer/rasterizeHTML.js/blob/master/src/browser.js#L175), 2) parsing HTML (https://github.com/cburgmer/rasterizeHTML.js/blob/master/src/browser.js#L241), 3) executing JavaScript that's included in a document (https://github.com/cburgmer/rasterizeHTML.js/blob/master/src/browser.js#L30),

only to name a few.

In fact web workers only have very limited access judging by https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Functions_and_classes_available_to_workers.

The only way that I currently see for moving towards web workers is to 1) be limited to a subset of features that this library currently exposes, and 2) replacing some of the dependencies on the browser with pure NodeJS libraries (like parsing HTML).

But then this already sounds like a bit of a rewrite/separate direction.

I haven't looked into this into more detail though, and I'm happy to be proven wrong. Might be worth giving it a try if you want :)

cburgmer commented 7 years ago

@chuyik If you need full server side rendering, PhantomJS, SlimerJS or headless Chrome might be an option.