Reputation: 4393
I'm just trying to convert svg to html canvas, it works fine until i use image element in svg, if i use image elements means canvg
is not working.
This is the code which i used to convert svg to canvas
var svgCanvas, context, svgCanvasElement, link;
svgCanvas = $('<canvas id="svgCanvas" width="250px" height="25px" style="display:none;"></canvas>');
context = svgCanvas[0].getContext('2d');
svgCanvasElement = $(svgCanvas)[0];
var serializer = new XMLSerializer();
var svgElement = document.getElementsByTagName("svg");
var svg = serializer.serializeToString(svgElement[0]);
var canvgMethod = new canvg(svgCanvasElement, svg);
Here not working JSFiddle DEMO.
NOTE : This code working fine for other elements like circle,rect,polygon and etc. Here is working JSFiddle Demo with circle element.
How to overcome this issue and how can i convert this image element into canvas?
Any Suggestions will be appreciated.
Upvotes: 0
Views: 2307
Reputation: 9612
It's done using canvg
plugin Reference
//load a svg snippet in the canvas with id = 'drawingArea'
canvg(document.getElementById('canvas'), '<svg>...</svg>')
Upvotes: 0
Reputation: 123985
You'd need to convert the image into a data URL. What you pass to canvg must be complete in a single file it can't have external references.
I can show you steps to see an image work. Start from this...
<svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<image xlink:href="paste your data URL in here" height="75" width="75" y="27.5" x="29.5"></image>
</svg>
Upvotes: 1