Reputation: 1543
I am creating SVG shape and exporting it to database. The jsfiddle for this is here - http://jsfiddle.net/rafi_ccj/MASeK/1/
And the code is below.
HTML part:
<button id="make_svg">toSVG</button>
<div id="canvas-wrapper">
<canvas id="canvas" width="400px" height="200px"></canvas>
</div>
Javascript part:
var canvas = new fabric.Canvas('canvas');
canvas.add(new fabric.Rect({
left: 100,
top: 100,
angle: 0,
fill: 'rgba(23,23,125,0.5)',
strokeWidth: 0.1,
stroke: '#FF0000',
width: 200,
height: 200,
opacity: 1
}));
canvas.add(new fabric.Circle({
left: 100,
top: 100,
fill: 'rgba(45, 255, 34, 0.5)',
strokeWidth: 0.1,
stroke: '#FF0000',
radius: 100,
opacity: 1
}));
$("#make_svg").click(function () {
canvas_data = canvas.toSVG();
console.log(canvas_data);
});
and then i am importing that svg into canvas but it is not same i did create. the jsfiddle is here - http://jsfiddle.net/rafi_ccj/2vtz2/
and the code is below---
html part
<div id="canvas-wrapper">
<canvas id="canvas" width="900px" height="800px"></canvas>
</div>
javascript part is here -
var canvas = new fabric.Canvas('canvas');
var canvas_data = '<?xml version="1.0" standalone="no" ?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="400" height="200" xml:space="preserve"><desc>Created with Fabric.js 1.1.8</desc><defs></defs><rect x="-100" y="-100" rx="0" ry="0" width="200" height="200" style="stroke: #FF0000; stroke-width: 0.1; stroke-dasharray: ; fill: rgba(23,23,125,0.5); opacity: 1;" transform="translate(100 100)"/><circle cx="0" cy="0" r="100" style="stroke: #FF0000; stroke-width: 0.1; stroke-dasharray: ; fill: rgba(45, 255, 34, 0.5); opacity: 1;" transform="translate(100 100)"/></svg> ';
fabric.loadSVGFromString(canvas_data, function (objects, options) {
var loadedObject = fabric.util.groupSVGElements(objects, options);
loadedObject.set({
left: 400,
top: 200
});
loadedObject.setCoords();
canvas.add(loadedObject);
canvas.calcOffset();
});
i also have noticed that if i use triangle, it is not present after import, it is just vanished.
can anyone please help me in this problem?
Upvotes: 1
Views: 3443
Reputation: 1543
I have solved this problem by making all objects a group element, and after creating the group with these objects existed in the canvas, i have saved it with svg extension in device and also a json format in the database. Code is below-
var group = new fabric.Group([],{
left: 200,
top: 200
});
var canvas_item = canvas.item(0), x = 0;
while (canvas_item != undefined) {
group.addWithUpdate(canvas_item);
x += 1;
canvas_item = canvas.item(x);
}
canvas.clear();
canvas.add(group);
//then use canvas.toDatalessJSON(); and save it to database. while importing this data //use following codes
var canvas_data = JSON.stringify(canvas.toDatalessJSON());
canvasJSONdata = JSON.stringify(canvas.toDatalessJSON());
var stringifiedCanvasJSONdata = JSON.parse(canvasJSONdata,'');
var base_64 = canvas.toDataURL('png');
and now it is working perfect! Though i have faced many problems, i have learned a lot, i have enjoyed it! Hope this solution will help someone someday.
Upvotes: 4