deno-canvas icon indicating copy to clipboard operation
deno-canvas copied to clipboard

Svg support?

Open scientific-dev opened this issue 4 years ago • 5 comments

I am using deno-canvas for making a test image and i was using a svg image url to load image but whenever i try to load image it throws me a error Invalid Image. But when using nodejs's canvas it works fine. Is the problem with svg or something? Here is my code to reproduce the error:

import Canvas, { dataURLtoFile, loadImage } from 'https://deno.land/x/[email protected]/mod.ts';

ctx.fillStyle = '#7298da';
ctx.fillRect(0, 0, 800 - 20, 800 - 20);
ctx.fillStyle = 'white';

await ctx.drawImage(await loadImage('https://weboverhauls.github.io/demos/svg/checkmark.svg'), 0, 0);
Deno.writeFileSync("test.png", dataURLtoFile(canvas.toDataURL()))

scientific-dev avatar Apr 05 '21 06:04 scientific-dev

SVG isn't supported at the moment. I'll look into that.

DjDeveloperr avatar Apr 05 '21 06:04 DjDeveloperr

Thanks :)

scientific-dev avatar Apr 05 '21 06:04 scientific-dev

It looks like it's available in CanvasKit.Path.MakeFromSVGString, at least in new builds of canvaskit. I will work on a PR.

konsumer avatar Sep 12 '21 23:09 konsumer

Hmm, nevermind, seems like that just creates image-path.

I tried this:

console.log(canvas.Path.MakeFromSVGString(s))

where s is a SVG string, and got null. This seems doable, but I don't think it's directly related, so I'm not sure how.

konsumer avatar Sep 13 '21 00:09 konsumer

It seems to transform SVG <path> to canvas Path, not actually parse SVG:

const path = Skia.Path.MakeFromSVGString(
  `M0 18C0 8.059 8.059 0 18 0s18 8.059 18 18-8.059 18-18 18S0 27.941 0 18`,
);

but I think it should help in adding SVG support, I'll give this a try

DjDeveloperr avatar Sep 13 '21 08:09 DjDeveloperr