gpt4 book ai didi

canvas - Fabricjs:使用导出和导入为 svg 时出现文本对齐问题

转载 作者:行者123 更新时间:2023-12-02 11:56:57 25 4
gpt4 key购买 nike

导出为 SVG 并导入 Canvas 后添加到 Canvas 上的文本与原始对齐方式不同

http://jsbin.com/ruluko/edit?html,js,output

使用fabric js v1.6.0-rc.1

最佳答案

这看起来像是 Fabric 的 svg 解析器中的错误。
如果不是将 svg 元素分组,而是将它们一一添加,您就可以看到它:

var canvas1 = new fabric.Canvas('c1');
var circle = new fabric.Circle({
radius: 50,
fill: '#eef'
});
canvas1.add(circle);
var text = new fabric.Text('fabric', { fill: 'red', left: 100, top: 100 });
canvas1.add(text);
var topText = new fabric.Text('Top', { fill: 'red', left: 100, top: 0 });
canvas1.add(topText);
var botText = new fabric.Text('Bot LL', { fill: 'red', left: 80, top: 163 });
canvas1.add(botText);

var canvas2 = new fabric.Canvas('c2');

var doAction = function() {
var mysvg = canvas1.toSVG();
canvas2.clear();
fabric.loadSVGFromString(mysvg, function(objects, options) {
for (var i = 0; i < objects.length; i++) {
canvas2.add(objects[i]);
}
});
};
.canvas-container{border:1px solid; display: inline-block; position: relative;}
<script src="//code.jquery.com/jquery.min.js"></script>
<script src="//cdn.bootcss.com/fabric.js/1.6.0-rc.1/fabric.min.js"></script>

<canvas id="c1" width="200" height="200"></canvas>
<canvas id="c2" width="200" height="200"></canvas>
<button onclick='doAction()'>Export & Import as SVG</button>

正如您在代码片段中看到的,每个元素的框都位于错误的坐标处。请注意,它也会与 loadSVGFromURL() 一起出现。方法。

<小时/>

如果您无论如何都要对这些元素进行分组,一个快速的解决方法是将 svg 绘制为图像:

var canvas1 = new fabric.Canvas('c1');

var circle = new fabric.Circle({
radius: 50,
fill: '#eef'
});
canvas1.add(circle);
var text = new fabric.Text('fabric', { fill: 'red', left: 100, top: 100 });
canvas1.add(text);
var topText = new fabric.Text('Top', { fill: 'red', left: 100, top: 0 });
canvas1.add(topText);
var botText = new fabric.Text('Bot LL', { fill: 'red', left: 80, top: 163 });
canvas1.add(botText);

var canvas2 = new fabric.Canvas('c2');


var doAction = function() {
var mysvg = canvas1.toSVG();
canvas2.clear();
var svgURL = 'data:image/svg+xml; charset=utf8, ' + encodeURIComponent(mysvg.split('svg11.dtd">')[1]);
fabric.Image.fromURL(svgURL, function(oImg) {
canvas2.add(oImg)
});
};
.canvas-container{border:1px solid; display: inline-block; position: relative;}
<script src="//code.jquery.com/jquery.min.js"></script>
<script src="//cdn.bootcss.com/fabric.js/1.6.0-rc.1/fabric.min.js"></script>

<canvas id="c1" width="200" height="200"></canvas>
<canvas id="c2" width="200" height="200"></canvas>
<button onclick='doAction()'>Export & Import as SVG</button>

此解决方法的主要警告是,一旦加载,您将无法修改组的每个路径。

<小时/>

由于出现问题是因为 <tspan>元素,更好的解决办法是首先通过替换所有这些 <tspan> 来清理您的 svg 字符串。至<text>元素:

var canvas1 = new fabric.Canvas('c1');

var circle = new fabric.Circle({
radius: 50,
fill: '#eef'
});
canvas1.add(circle);
var text = 'this is\na multiline\ntext';
var alignedRightText = new fabric.Text(text, {
textAlign: 'right'
});
canvas1.add(alignedRightText)
var topText = new fabric.Text('Top', { fill: 'red', left: 100, top: 0 });
canvas1.add(topText);
var botText = new fabric.Text('Bot LL', { fill: 'red', left: 80, top: 163 });
canvas1.add(botText);

var canvas2 = new fabric.Canvas('c2');


var doAction = function() {
var mysvg = canvas1.toSVG();
canvas2.clear();
fabric.loadSVGFromString(fixSVGText(mysvg), function(objects, options) {
options.selectable = false;
var obj = fabric.util.groupSVGElements(objects, options);
canvas2.add(obj).renderAll();
});
};

function fixSVGText(str) {
// parse our string as a DOM object and get the SVGElement
var svg = new DOMParser().parseFromString(str, "image/svg+xml").documentElement;

// get all <tspan> elements
var tspans = svg.querySelectorAll('tspan');
for (var i = 0; i < tspans.length; i++) {
var ts = tspans[i],
parent = ts.parentNode,
gParent = parent.parentNode;
var j = 0;

// create a new SVGTextElement to replace our tspan
var replace = document.createElementNS('http://www.w3.org/2000/svg', 'text');

var tsAttr = ts.attributes;
// set the 'x', 'y' and 'fill' attributes to our new element
for (j = 0; j < tsAttr.length; j++) {
replace.setAttributeNS(null, tsAttr[j].name, tsAttr[j].value);
}

// append the contentText
var childNodes = ts.childNodes;
for (j = 0; j < childNodes.length; j++) {
replace.appendChild(ts.childNodes[j]);
}

var tAttr = parent.attributes;
// set the original text attributes to our new one
for (j = 0; j < tAttr.length; j++) {
replace.setAttributeNS(null, tAttr[j].name, tAttr[j].value);
}
// append our new text to the grand-parent
gParent.appendChild(replace);

// if this is the last tspan
if (ts === parent.lastElementChild)
// remove the old, now empty, SVGTextElement
gParent.removeChild(parent)
}
// return a string version of our cleaned svg
return new XMLSerializer().serializeToString(svg);
}
.canvas-container{border:1px solid; display: inline-block; position: relative;}
<script src="//code.jquery.com/jquery.min.js"></script>
<script src="//cdn.bootcss.com/fabric.js/1.6.0-rc.1/fabric.min.js"></script>

<canvas id="c1" width="200" height="200"></canvas>
<canvas id="c2" width="200" height="200"></canvas>
<button onclick='doAction()'>Export & Import as SVG</button>

但最好的解决方案仍然是open a new issue to kangax ,希望在以后的版本中修复这个bug。

关于canvas - Fabricjs:使用导出和导入为 svg 时出现文本对齐问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33026069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com