gpt4 book ai didi

javascript - 使用 Raphael JS 将所​​有 SVG 文本节点转换为路径节点

转载 作者:可可西里 更新时间:2023-11-01 02:22:50 33 4
gpt4 key购买 nike

我正在尝试写一个 RaphaelJS将采用 Raphael paper 中现有文本节点的函数实例并将它们转换为路径。

目标是完全复制文本在页面上显示的位置、大小和属性,但使用路径而不是文本呈现文本。我最初无法使用 Raphael paper.print() 渲染文本功能,因为文本是动态更新的,并且需要基于“文本”的属性才能这样做。将现有文本节点转换为路径将作为过程中的“最后”步骤发生(在文本修改完成后)。

我这样做是为了消除安装字体以便稍后查看或处理 SVG 的需要。

我面临的挑战是:

  1. 文本节点可能包含带有 xdy 定义的 tspan。创建的路径必须与每个子节点字母 (tspan) 完美对齐。

  2. 检索文本节点的实际位置数据,以及每个 tspan。这是我遇到麻烦的地方,希望有更多经验的人可以帮助我。由于笔画宽度和其他属性会影响定位/边框值,我不确定获取文本正确定位数据的最有效方法是什么。

到目前为止我尝试了什么:

我的代码的简单分解。

我编写了一个自定义属性函数 textFormat,它以交错形式对文本进行格式化。此函数解析文本节点,将其拆分为每个字母添加新行 \n 字符,并调整位置以使其看起来交错。

textToPaths 函数是一个纸张函数,它应该循环遍历纸张节点,并使用 Raphael paper.print() 将所有找到的文本节点转换为路径。功能。这是我遇到麻烦的功能。

View the Complete JSFiddle Example Here

问题代码
我不确定如何获得准确且一致的 xy 值以传递给 paper.print() 函数。现在,我正在使用 getBoundingClientRect(),但它仍然处于关闭和倾斜状态。我的假设是笔划宽度会影响 x 和 y 的计算。

        //Loop through each tspan and print the path for each.
var i,
children = node.node.childNodes,
len = children.length;

for (i = 0; i < len; i++) {
var tspan = children[i],
tspanText = tspan.innerHTML,
x = tspan.getBoundingClientRect().left - node.node.getBoundingClientRect().left, //How do I get the correct x value?
y = tspan.getBoundingClientRect().top - node.node.getBoundingClientRect().top; //How do I get the correcy y value?

var path = paper.print(x, y, tspanText, font, fontSize),
attrs = node.attrs;
delete attrs.x;
delete attrs.y;
path.attr(attrs);
path.attr('fill', '#ff0000'); //Red, for testing purposes.
}

完整代码 View the JSFiddle Example

//Register Cufon Font
var paper = Raphael(document.getElementById('paper'), '600', '600');

var text1 = paper.text(100, 100, 'abc').attr({fill: 'none',stroke: '#000000',"stroke-width": '12',"stroke-miterlimit": '1',"font-family" : "Lobster", "font-size": '30px','stroke-opacity': '1'});
var text2 = paper.text(100, 100, 'abc').attr({fill: 'none',stroke: '#ffffff',"stroke-width": '8',"stroke-miterlimit": '1',"font-family" : "Lobster", "font-size": '30px','stroke-opacity': '1'});
var text3 = paper.text(100, 100, 'abc').attr({fill: '#000000',stroke: '#ffffff',"stroke-width": '0',"stroke-miterlimit": '1',"font-family" : "Lobster", "font-size": '30px','stroke-opacity': '1'});
var text = paper.set(text1, text2, text3);
text.attr('textFormat', 'stagger');

/* paper.textToPaths
* Description: Converts all text nodes to paths within a paper.
*
* Example: paper.textToPaths();
*/
(function(R) {
R.fn.textToPaths = function() {
var paper = this;

//Loop all nodes in the paper.
for (var node = paper.bottom; node != null; node = node.next ) {
if ( node.node.style.display === 'none' || node.type !== "text" || node.attrs.opacity == "0") continue; //skip non-text and hidden nodes.

//Get the font config for this text node.
var text = node.attr('text'),
fontFamily = node.attr('font-family'),
fontSize = parseInt(node.attr('font-size')),
fontWeight = node.attr('font-weight'),
font = paper.getFont(fontFamily, fontWeight);

//Loop through each tspan and print the path for each.
var i,
children = node.node.childNodes,
len = children.length;

for (i = 0; i < len; i++) {
var tspan = children[i],
tspanText = tspan.innerHTML,
x = tspan.getBoundingClientRect().left - node.node.getBoundingClientRect().left, //How do I get the correct x value?
y = tspan.getBoundingClientRect().top - node.node.getBoundingClientRect().top; //How do I get the correcy y value?

var path = paper.print(x, y, tspanText, font, fontSize),
attrs = node.attrs;
delete attrs.x;
delete attrs.y;
path.attr(attrs);
path.attr('fill', '#ff0000'); //Red, for testing purposes.
}

}

};
})(window.Raphael);

textToPaths = function() {
//Run textToPaths
paper.textToPaths();
};


/* Custom Element Attribute: textFormat
* Description: Formats a text element to either staggered or normal text.
*
* Example: element.attr('textFormat, 'stagger');
*/
paper.customAttributes.textFormat = function( value ) {
// Sets the SVG dy attribute, which Raphael doesn't control
var selector = Raphael.svg ? 'tspan' : 'v:textpath',
has = "hasOwnProperty",
$node = $(this.node),
text = $node.text(),
$tspans = $node.find(selector);

console.log('format');

switch(value)
{
case 'stagger' :
var stagger = function(el) {
var R = Raphael,
letters = '',
newline = '\n';

for (var c=0; c < text.length; c++) {
var letter = text[c],
append = '';

if(c < text.length - 1)
append = newline;
letters += letter+append;
}
el.attr('text', letters);
var children = el.node.childNodes;

var i,
a = el.attrs,
node = el.node,
len = children.length,
letterOffset = 0,
tspan,
tspanHeight,
tspanWidth,
tspanX,
prevTspan,
prevTspanRight = 0,
tspanDiff = 0,
tspanTemp,
fontSize,
leading = 1.2,
tempText;
for (i = 0; i < len; i++) {
tspan = children[i];
tspanHeight = tspan.getComputedTextLength();
tspanWidth = tspan.getComputedTextLength();
tspanX = tspan.getAttribute('x'),
prevTspanRight = tspan.getBoundingClientRect().right

if(tspanX !== null)
{
tspanDiff = tspanDiff + prevTspanRight - tspan.getBoundingClientRect().left;
var setX = parseInt(tspanX) + parseInt(tspanDiff);
tspan.setAttribute('x', setX);
tspan.setAttribute('dy', 15);
}
prevTspan = tspan;
}
}
stagger(this);
break;
case 'normal' :
this.attr('text', text);
break;
default :
this.attr('text', text);
break;
}

eve("raphael.attr.textFormat." + this.id, this, value);

// change no default Raphael attributes
return {};
};

staggerText = function() {
//Run textToPaths
text.attr('textFormat', 'stagger');
};

如果有人能帮我解决这个问题,我将不胜感激。谢谢!

最佳答案

您可以使用 Opentype.js 将字体转换为 SVG/Canvas 路径命令.

库会返回给你一系列的路径绘制命令;这些用于在 HTML5 上绘图 <canvas>元素。

但是,使用这些命令构建 SVG 路径很简单,因为字体转换不包括任何与 Canvas 路径绘图兼容的命令,而这些命令与 SVG 路径命令不兼容。

关于javascript - 使用 Raphael JS 将所​​有 SVG 文本节点转换为路径节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23877271/

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