Automattic / Automattic/node-canvas
SVGs with small dimensions are rendered pixelated
- Dominant language
- JavaScript
- Stars
- 10.7k
- Forks
- 1.2k
- Avg merge
- 4d 8h
- Merged PRs (30d)
- 1
Description
## Issue or Feature
node-canvas renders SVGs with small dimensions very pixelated while browsers (Tested with Chrome and Firefox) do not care about the SVG size and renders a high quality image. Maybe because node-canvas rasterizes the image on load (and when width/height properties are changed) while browsers rasterizes the image on draw when the actual output size is known?
This issue is related to https://github.com/Automattic/node-canvas/issues/957 and maybe also to https://github.com/Automattic/node-canvas/issues/1474.
## Steps to Reproduce
### test.svg
```svg
```
### test.js
```js
const path = require("path");
const fs = require("fs");
const { createCanvas, loadImage } = require("canvas")
const canvas = createCanvas(100, 100);
const context = canvas.getContext("2d");
context.imageSmoothingEnabled = false;
loadImage("test.svg").then(image => {
// The following line (which isn't needed in browsers) fixes the problem
// with node-canvas:
//image.width = image.height = 100;
context.drawImage(image, 0, 0, 100, 100);
const buf = canvas.toBuffer();
fs.writeFileSync("test.png", buf);
});
```
### test.html
```html
const canvas = document.querySelector("canvas");
const context = canvas.getContext("2d");
context.imageSmoothingEnabled = false;
const image = new Image();
image.onload = () => {
context.drawImage(image, 0, 0, 100, 100);
};
image.src = "test.svg";
```
* Open **test.html** in Chrome or Firefox and you get a high quality output:

* Run `npm install canvas` to install node-canvas and then run `node test.js`. Result is written to **test.png** which looks like this:

* As a workaround uncomment the `image.width = image.height = 100` line in **test.js**. Then the output is ok. But calculating the needed image size manually is annoying, node-canvas should do this transparently like the browser canvas implementations.
## Your Environment
* Version of node-canvas: 2.6.1
* Environment: Node 12.16.2 on Debian Linux 10
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.