niklasvh / html2canvas

Screenshots with JavaScript
https://html2canvas.hertzen.com/
MIT License
30.32k stars 4.78k forks source link

Svg deformed #2261

Open creale opened 4 years ago

creale commented 4 years ago

Svg deformed

I'm using the latest version and the non-minified version of html2canvas

I have two issues :

Thank you

Exemple

This is what i want to transform in canva

Capture d’écran 2020-06-10 à 14 10 55

An this is the result

Capture d’écran 2020-06-10 à 14 11 09

creale commented 4 years ago

No one ??

brainz80 commented 4 years ago

This worked for me.

in options add:

onclone (document) {
    const img = document.querySelector('img[src$="svg"]');

    if (img) {
        const canvas = document.createElement('canvas'), context = canvas.getContext('2d'), styles = window.getComputedStyle(img);

        context.drawImage(img, 0, 0);

        for (let key of styles) {
            const value = styles[key];
            if (value !== '') canvas.style[key] = value;
        }

        img.replaceWith(canvas);
    }
}

this is a bit hackish. What it basicly does is wraps the svg-image inside of an canvas and copies the styles of the image onto the canvas - hopefully then rendering it at least mostly right.