问题描述
我已经从Fabric js创建了svg.创建卡片时,我使用以下代码将文本对齐方式应用于对象:
I have created svg from fabric js. While creating card i have applied text-align to object using below code:
activeObj.set({
textAlign : 'center',
});
canvas.renderAll();
所以我的svg如下图所示:
So my svg is look like below:
https://codepen.io/dhavalsisodiya/pen/BrRbRG
现在,当我将{company_address}地址变量更改为原始值时,该文本应居中对齐,但不是.
Now when i changed my {company_address} address variable with original value, that text should be align center but it is not.
https://codepen.io/dhavalsisodiya/pen/VXbRzy
然后我手动修改了SVG,我在SVG的tspan标签中添加了text-anchor="middle"
(这是我的变量的位置)
Then i have modified SVG manually, I have added text-anchor="middle"
in tspan tag of SVG (that is the placing of my varaible)
https://codepen.io/dhavalsisodiya/pen/ZxKPab
添加我的文字后,该文字显示在SVG中.
After adding that my text is appearing center in SVG.
那么有没有一种方法可以使用fabric js,所以当我应用文本对齐方式时它包含text-anchor="middle"
?
So is there a way to do that using fabric js, so it contain text-anchor="middle"
when i apply text alignment?
我也应用了textAnchor,但是它没有转换为svg.
I have applied the textAnchor also, but it is not converting to svg.
https://codepen.io/dhavalsisodiya/pen/gvQooL
推荐答案
感谢@AndreaBogazzi,我能够使用以下方法解决该问题:
Thanks to @AndreaBogazzi, i was able to resolve it using below method:
我用以下内容修改了Fabric js文件:
I have modified fabric js file with below:
按照@AndreaBogazzi的建议在_wrapSVGTextAndBg: function(markup, textAndBg) {
中添加了'text-anchor="' + this.textAnchor + '" ',
.
Added 'text-anchor="' + this.textAnchor + '" ',
in _wrapSVGTextAndBg: function(markup, textAndBg) {
as @AndreaBogazzi suggested.
/**
* @private
*/
_wrapSVGTextAndBg: function(markup, textAndBg) {
var noShadow = true, filter = this.getSvgFilter(),
style = filter === '' ? '' : ' style="' + filter + '"',
textDecoration = this.getSvgTextDecoration(this);
markup.push(
'\t<g ', this.getSvgId(), 'transform="', this.getSvgTransform(), this.getSvgTransformMatrix(), '"',
style, '>\n',
textAndBg.textBgRects.join(''),
'\t\t<text xml:space="preserve" ',
'text-anchor="' + this.textAnchor + '" ',
(this.fontFamily ? 'font-family="' + this.fontFamily.replace(/"/g, '\'') + '" ' : ''),
(this.fontSize ? 'font-size="' + this.fontSize + '" ' : ''),
(this.fontStyle ? 'font-style="' + this.fontStyle + '" ' : ''),
(this.fontWeight ? 'font-weight="' + this.fontWeight + '" ' : ''),
(textDecoration ? 'text-decoration="' + textDecoration + '" ' : ''),
'style="', this.getSvgStyles(noShadow), '"', this.addPaintOrder(), ' >',
textAndBg.textSpans.join(''),
'</text>\n',
'\t</g>\n'
);
},
然后我在下面进行了修改:
Then i have modified below:
_createTextCharSpan: function(_char, styleDecl, left, top) {
var styleProps = this.getSvgSpanStyles(styleDecl, _char !== _char.trim()),
fillStyles = styleProps ? 'style="' + styleProps + '"' : '';
//Addded to support text-anhor middle
if(this.textAnchor==='middle')
{
return [
'\t\t\t<tspan ',
fillStyles, '>',
fabric.util.string.escapeXml(_char),
'</tspan>\n'
].join('');
}
else
{
return [
'\t\t\t<tspan x="', toFixed(left, NUM_FRACTION_DIGITS), '" y="',
toFixed(top, NUM_FRACTION_DIGITS), '" ',
fillStyles, '>',
fabric.util.string.escapeXml(_char),
'</tspan>\n'
].join('');
}
},
扩展toObjectTo以在json中添加textAnchor,请参见此处:扩展toObject :
Extend the toObjectTo to add textAnchor in json, see here: extend toObject :
$('.text_alignment').click(function(){
var cur_value = $(this).attr('data-action');
var activeObj = canvas.getActiveObject();
if (cur_value != '' && activeObj) {
activeObj.set({
textAlign : cur_value,
textAnchor : 'middle'
});
//To modify Object
activeObj.toObject = (function(toObject) {
return function() {
return fabric.util.object.extend(toObject.call(this), {
textAnchor: 'middle'
});
};
})(activeObj.toObject);
//activeObj.textAnchor = cur_value;
canvas.renderAll();
}
else
{
alert('Please select text');
return false;
}
});
通过进行上述更改,现在即使我修改了文字,我的变量仍处于中间位置.
By making above changes, now my variable stays middle even if i modified text.
这篇关于如何使用Text-anchor:Fabric JS中的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!