html2canvas icon indicating copy to clipboard operation
html2canvas copied to clipboard

Why is it necessary to clone the whole window?

Open mojimi opened this issue 7 years ago • 8 comments

Most of the processing time is due to cloning of window.

Specially slow if your window contains several script/css tags.

Why is this necessary? If this could be avoided it would be very much faster.

mojimi avatar Aug 16 '18 02:08 mojimi

Agree with you. It's really slow. Furthermore, the thread would be full to handle it, your application would stop a long time. It's a bad user experience! image From above figure, the clone spent about 1s (The screen would stop during 1s). It's almost the half of whole processing time. Is there any way to solve this problem?

chuckWu1989 avatar Aug 30 '18 02:08 chuckWu1989

I have an idea may fix this problem. In the clone.js, we can add a judgement in cloneNode method. If the element is body, head, script, or target element. it would be reserved, or it would be skipped. I tried to add this argument into here:

child.nodeType !== Node.ELEMENT_NODE || (!child.hasAttribute(IGNORE_ATTRIBUTE) && (typeof this.options.ignoreElements !== 'function' || // $FlowFixMe !this.options.ignoreElements(child))))

And the result looks good! image

There is an issue may happen in this case I can imagined. If we removed the element with relative position. The target locating may be wrong. But I think this issue is easy to solve.

Furthermore, I had tried a feature that we can remove some elements we don't want to show on snapshot and it worked. If you guys think this way is workable, I can delivery a MR to you.

chuckWu1989 avatar Aug 31 '18 06:08 chuckWu1989

Did you figure this out all the way? This would be a very welcome change.

rconstantine avatar Apr 30 '19 15:04 rconstantine

@d9767192 Could you please specify some details on how you achieved speeding it up?

TimoZikeli avatar Jul 01 '19 10:07 TimoZikeli

it's slow when the page is complex, any solution?

worldzhao avatar Oct 27 '20 06:10 worldzhao

same question, I just want to generate canvas from one child div, but the iframe clones the whole document which results in redundant requests of all the assets.

leejerry110 avatar Apr 20 '21 15:04 leejerry110

I've switched to html-to-image because of this issue.

It loads no excess assets except for fonts: it loads all the fonts mentioned in the @font-face rules even if they aren't used on the page. The font problem can be solved by adding the fontEmbedCSS: '' option.

Finesse avatar Nov 10 '21 09:11 Finesse

Same problem here, we are using it as part of PDF generation and this is by far the heaviest part of the whole process (about 90% of the time is spent cloning the DOM).

aaronclawrence avatar Nov 19 '24 20:11 aaronclawrence