Skip to content

Scaled SVGs render incorrectly #2498

Closed
Closed
@mgilfillan

Description

@mgilfillan

When drawing an SVG after setting the width and height to a larger value the drawn SVG is scaled incorrectly.

I've put together a minimal reproduction:

index.js

import {createCanvas, loadImage} from "canvas";
import fs from "fs";

function writeImage(image, size, fileName) {
    const canvas = createCanvas(size, size);
    const ctx = canvas.getContext('2d');

    image.width = size;
    image.height = size;

    ctx.drawImage(image, 0, 0, size, size);

    fs.writeFileSync(fileName, canvas.toBuffer());
}

writeImage(await loadImage('./image.svg'), 100, './no scaling.png');
writeImage(await loadImage('./image.svg'), 200, './2x scaling.png');

image.svg

<svg version="1.2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="100" height="100">
    <circle fill="#ff0000" cx="50" cy="50" r="50"/>
</svg>

I would expect both output images to be visually the same, other than being scaled up, however the scaled one renders the SVG much larger than it should. This works correctly in node-canvas 2.x

node-canvas version No Scaling 2x scaling
2.11.2 Image Image
3.1.0 Image Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions