Automattic / Automattic/node-canvas

node-canvas fails to convert between image types

Open
#1,473 0 comments 0 reactions 0 assignees View on GitHub
SVG
Dominant language
JavaScript
Stars
10.7k
Forks
1.2k
Avg merge
4d 8h
Merged PRs (30d)
1

Description

## Issue or Feature

I am trying to load an svg image into a canvas context and then use toDataURL to convert it into a png. When I run toDataURL() on the canvas after adding the image i get a data url that looks like this `data:,`
## Steps to Reproduce

```js
const { createCanvas, loadImage, Image } = require('canvas');

var canvas = createCanvas(900, 900);
var ctx = canvas.getContext('2d');
var img = new Image();

// when image is loaded
img.onload = () =>{
console.log("Image Loaded Successfully");
ctx.drawImage(img,0,0);
canvas.toDataURL("image/png",(err,png)=>{
if(err){
console.log("Error: " + err);
}else{
console.log("dataURL: "+ png);
}
});
}
// catch any error on image load
img.onerror = (err) => {
console.log("Error Occured: " + err);
response.status(500);
response.send("

Download Failed

");
}
img.src = "data:image/svg+xml;base64,...";
```
The image loads successfully and logging the 'img' in the onload function returns an image object with the correct size of the svg image so it's not librsvg that is failing. When I log the dataURL from the toDataURL callback I get "data:,".

I need to know if this is even possible to convert between types like this and if it is then is there something wrong in my understanding of how to use node-canvas.
## Your Environment
* Version of node-canvas (output of `npm list canvas` or `yarn list canvas`): 2.6.0
* Environment (e.g. node 4.2.0 on Mac OS X 10.8):Node 8.10.0 on Ubuntu 18.04

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.