SVG 自动换行 - 显示停止符?

发布于 2024-07-12 06:34:38 字数 217 浏览 5 评论 0原文

为了好玩,我尝试看看在业余时间为 RIA 实现 SVG 浏览器客户端能走多远。

但遇到了似乎是一个巨大的绊脚石。 没有自动换行!!

有谁知道有什么解决办法吗(我在想某种我不知道的 JavaScript 或特殊标签)?

如果不是,我要么必须走 xhtml 路线并开始在我的 SVG 中粘贴 HTML 元素(哎呀),要么十年后 SVG 1.2 准备好时再回来。

For fun I am trying to see how far I can get at implementing an SVG browser client for a RIA I'm messing around with in my spare time.

But have hit what appears to be a HUGE stumbling block. There is no word wrap!!

Does anyone know of any work around (I'm thinking some kind of JavaScript or special tag I don't know)?

If not I'm either going to have to go the xhtml route and start sticking HTML elements in my SVG (ouch), or just come back again in ten years when SVG 1.2 is ready.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

拿命拼未来 2024-07-19 06:34:39

SVGT 1.2 引入了 textArea 元素 http://www.w3.org/TR/SVGTiny12 /text.html#TextInAnArea ,但目前仅 Opera 10 实验性支持。 我不知道其他浏览器是否会计划实现它,但我希望他们会这样做。

SVGT 1.2 introduces the textArea element http://www.w3.org/TR/SVGTiny12/text.html#TextInAnArea , but it is only experimentally supported by Opera 10 at the moment. I don't know if other browsers will ever plan on implementing it, though I hope they will.

亚希 2024-07-19 06:34:39

svg.js 库有一个 svg.textflow.js 插件。 它不是超快,但它确实有效。 它甚至将溢出文本存储在数据属性中,以便您可以使用它来创建连续流动的列。 这里是文本流示例页面

The svg.js library has a svg.textflow.js plugin. It's not ultra fast but it does the trick. It even stores overflowing text in a data attribute so you can use it to create continuously flowing columns. Here the text flow example page.

她比我温柔 2024-07-19 06:34:39

另一种方法是使用 Andreas Neuman 的 文本框 对象。

An alternative method is to use Andreas Neuman's text box object.

浅沫记忆 2024-07-19 06:34:39

如今,flowPara 可以自动换行,但我还没有找到正确支持它的浏览器。

These days, flowPara can do word wrapping, but I have yet to find a browser that supports it properly.

成熟的代价 2024-07-19 06:34:39

我一直在寻找有关 svg 中自动换行的解决方案几个小时(或很多天)。
如果您可以在您的应用程序中编辑代码以添加一些 tspan 或任何其他方法,请加入其中。

文本换行将在 1.2 版本中实现,但除了 Opera 之外,还没有浏览器完全实现它(4 年了,规范在 W3 上......)。

因为我必须使用一些对齐设置,所以我无法使用许多论坛可以提供的任何代码(没有异物,没有 carto 脚本或任何东西)。

如果我发布此消息,只是为了在谷歌搜索自动换行 svg 时对其他人有用,因为这篇文章位于顶部结果中,并且在许多情况下,这篇文章没有帮助。

这是一个很酷、简单且轻便的解决方案:
http://dev.w3。 org/SVG/profiles/1.1F2/test/svg/text-dom-01-f.svg

I've been looking for a solution about word wrapping in svg so many hours (or many days).
If you can in your app, edit your code to put some tspan, or any other method, go in it.

Text wrapping will be implement in the 1.2 version but except opera, no browser fully implement it yet (4 years, the specification are on the W3 ...).

Because I had to use some alignment settings, i couldn't use any of the code that many forum can provide (no foreign object, no carto script or anything).

If I post this message, it's just in order to be usefull to some other people when googling word wrapping svg because this post on the top result and in many case, this post doesn't help.

Here is a cool, easy and light solution :
http://dev.w3.org/SVG/profiles/1.1F2/test/svg/text-dom-01-f.svg

忘你却要生生世世 2024-07-19 06:34:38

这个 SVG 东西令人困惑,不是吗?

值得庆幸的是,您可以取得一些好的结果,但这比使用 HTML 5 需要更多的工作。

这是我的 ASP.Net / SVG 应用程序的屏幕截图,其中包含一些“伪造”的自动换行。

在此处输入图像描述

以下函数将为您创建一个 SVG text 元素,分为 < tspan 片段,每行长度不超过 20 个字符。

<text x="600" y="400" font-size="12" fill="#FFFFFF" text-anchor="middle">
    <tspan x="600" y="400">Here a realy long </tspan>
    <tspan x="600" y="416">title which needs </tspan>
    <tspan x="600" y="432">wrapping </tspan>
</text>

它并不完美,但它简单、快速,而且用户永远不会知道其中的区别。

我的 createSVGtext() JavaScript 函数采用三个参数:x 位置、y 位置和要显示的文本。 字体、每行最大字符数和文本颜色都在我的函数中进行了硬编码,但这可以轻松更改。

要显示上面屏幕截图中显示的右侧标签,您可以使用以下方式调用该函数:

var svgText = createSVGtext("Here a realy long title which needs wrapping", 600, 400);
$('svg').append(svgText);

这是 JavaScript 函数:

function createSVGtext(caption, x, y) {
    //  This function attempts to create a new svg "text" element, chopping 
    //  it up into "tspan" pieces, if the caption is too long
    //
    var svgText = document.createElementNS('http://www.w3.org/2000/svg', 'text');
    svgText.setAttributeNS(null, 'x', x);
    svgText.setAttributeNS(null, 'y', y);
    svgText.setAttributeNS(null, 'font-size', 12);
    svgText.setAttributeNS(null, 'fill', '#FFFFFF');         //  White text
    svgText.setAttributeNS(null, 'text-anchor', 'middle');   //  Center the text

    //  The following two variables should really be passed as parameters
    var MAXIMUM_CHARS_PER_LINE = 20;
    var LINE_HEIGHT = 16;

    var words = caption.split(" ");
    var line = "";

    for (var n = 0; n < words.length; n++) {
        var testLine = line + words[n] + " ";
        if (testLine.length > MAXIMUM_CHARS_PER_LINE)
        {
            //  Add a new <tspan> element
            var svgTSpan = document.createElementNS('http://www.w3.org/2000/svg', 'tspan');
            svgTSpan.setAttributeNS(null, 'x', x);
            svgTSpan.setAttributeNS(null, 'y', y);

            var tSpanTextNode = document.createTextNode(line);
            svgTSpan.appendChild(tSpanTextNode);
            svgText.appendChild(svgTSpan);

            line = words[n] + " ";
            y += LINE_HEIGHT;
        }
        else {
            line = testLine;
        }
    }

    var svgTSpan = document.createElementNS('http://www.w3.org/2000/svg', 'tspan');
    svgTSpan.setAttributeNS(null, 'x', x);
    svgTSpan.setAttributeNS(null, 'y', y);

    var tSpanTextNode = document.createTextNode(line);
    svgTSpan.appendChild(tSpanTextNode);

    svgText.appendChild(svgTSpan);

    return svgText;
}

自动换行的逻辑基于 此 HTML5 Canvas 教程

我希望您发现这很有用!

迈克

http://www.MikesKnowledgeBase.com

更新

我忘记提及一件事。

我上面显示的“工作流程图”屏幕最初只是使用 HTML 5 canvas 编写的。 它工作得很好,图标可以拖动,点击图标时会出现弹出菜单,甚至 IE8 似乎也对此很满意。

但我发现,如果图表变得“太大”(例如 4000 x 4000 像素),那么将无法在所有浏览器中初始化,不会出现任何内容 - 但是 - 就 JavaScript 代码而言担心,一切都很好。

因此,即使进行了错误检查,我的图表仍显示为空白,并且我无法检测到此问题何时发生。

var canvasSupported = !!c.getContext;
if (!canvasSupported) {
    //  The user's browser doesn't support HTML 5 <Canvas> controls.
    prompt("Workflow", "Your browser doesn't support drawing on HTML 5 canvases.");
    return;
}

var context = c.getContext("2d");
if (context == null) {
    //  The user's browser doesn't support HTML 5 <Canvas> controls.
    prompt("Workflow", "The canvas isn't drawable.");
    return;
}

//  With larger diagrams, the error-checking above failed to notice that
//  the canvas wasn't being drawn.

因此,这就是为什么我必须重写 JavaScript 代码以使用 SVG 的原因。 它似乎可以更好地处理更大的图表。

This SVG stuff is baffling, isn't it ?

Thankfully, you can achieve some good results, but it takes more work than using the HTML 5 .

Here's a screenshot of my ASP.Net / SVG app, featuring a bit of "faked" word wrapping.

enter image description here

The following function will create an SVG text element for you, broken into tspan pieces, where each line is no longer than 20 characters in length.

<text x="600" y="400" font-size="12" fill="#FFFFFF" text-anchor="middle">
    <tspan x="600" y="400">Here a realy long </tspan>
    <tspan x="600" y="416">title which needs </tspan>
    <tspan x="600" y="432">wrapping </tspan>
</text>

It's not perfect, but it's simple, fast, and the users will never know the difference.

My createSVGtext() JavaScript function takes three parameters: an x-position, y-position and the text to be displayed. The font, maximum-chars-per-line and text color are all hardcoded in my function, but this can be easily changed.

To display the right-hand label shown in the screenshot above, you would call the function using:

var svgText = createSVGtext("Here a realy long title which needs wrapping", 600, 400);
$('svg').append(svgText);

And here's the JavaScript function:

function createSVGtext(caption, x, y) {
    //  This function attempts to create a new svg "text" element, chopping 
    //  it up into "tspan" pieces, if the caption is too long
    //
    var svgText = document.createElementNS('http://www.w3.org/2000/svg', 'text');
    svgText.setAttributeNS(null, 'x', x);
    svgText.setAttributeNS(null, 'y', y);
    svgText.setAttributeNS(null, 'font-size', 12);
    svgText.setAttributeNS(null, 'fill', '#FFFFFF');         //  White text
    svgText.setAttributeNS(null, 'text-anchor', 'middle');   //  Center the text

    //  The following two variables should really be passed as parameters
    var MAXIMUM_CHARS_PER_LINE = 20;
    var LINE_HEIGHT = 16;

    var words = caption.split(" ");
    var line = "";

    for (var n = 0; n < words.length; n++) {
        var testLine = line + words[n] + " ";
        if (testLine.length > MAXIMUM_CHARS_PER_LINE)
        {
            //  Add a new <tspan> element
            var svgTSpan = document.createElementNS('http://www.w3.org/2000/svg', 'tspan');
            svgTSpan.setAttributeNS(null, 'x', x);
            svgTSpan.setAttributeNS(null, 'y', y);

            var tSpanTextNode = document.createTextNode(line);
            svgTSpan.appendChild(tSpanTextNode);
            svgText.appendChild(svgTSpan);

            line = words[n] + " ";
            y += LINE_HEIGHT;
        }
        else {
            line = testLine;
        }
    }

    var svgTSpan = document.createElementNS('http://www.w3.org/2000/svg', 'tspan');
    svgTSpan.setAttributeNS(null, 'x', x);
    svgTSpan.setAttributeNS(null, 'y', y);

    var tSpanTextNode = document.createTextNode(line);
    svgTSpan.appendChild(tSpanTextNode);

    svgText.appendChild(svgTSpan);

    return svgText;
}

The logic for word-wrapping is based on this HTML5 Canvas tutorial

I hope you find this useful !

Mike

http://www.MikesKnowledgeBase.com

UPDATE

One thing I forgot to mention.

That "Workflow diagram" screen that I've shown above was originally just written using an HTML 5 canvas. It worked beautifully, the icons could be dragged, popup menus could appear when you clicked on them, and even IE8 seemed happy with it.

But I found that if the diagram became "too big" (eg 4000 x 4000 pixels), then the would fail to initialise in all browsers, nothing would appear - but - as far as the JavaScript code was concerned, everything was working fine.

So, even with error-checking, my diagram was appearing blank, and I was unable to detect when this showstopper problem was occurring.

var canvasSupported = !!c.getContext;
if (!canvasSupported) {
    //  The user's browser doesn't support HTML 5 <Canvas> controls.
    prompt("Workflow", "Your browser doesn't support drawing on HTML 5 canvases.");
    return;
}

var context = c.getContext("2d");
if (context == null) {
    //  The user's browser doesn't support HTML 5 <Canvas> controls.
    prompt("Workflow", "The canvas isn't drawable.");
    return;
}

//  With larger diagrams, the error-checking above failed to notice that
//  the canvas wasn't being drawn.

So, this is why I've had to rewrite the JavaScript code to use SVG instead. It just seems to cope better with larger diagrams.

雨的味道风的声音 2024-07-19 06:34:38

还有foreignObject标签。 然后您可以将 HTML 嵌入到 SVG 中,这提供了最大的灵活性。 HTML 非常适合文档布局,并且已经被不断地修改以支持应用程序布局、绘图以及我们开发人员想要的一切。 但它的优势在于自动换行和文档布局。 让 HTML 做它最擅长的事情,让 SVG 做它最擅长的事情。

http://www.w3.org/TR/SVG/extend.html

这适用于大多数浏览器 FireFox、Opera、Webkit,IE 除外(自 IE11 起)。 :-( 网络故事不是吗?

There is also foreignObject tag. Then you can embed HTML in SVG which gives the greatest flexibility. HTML is great for document layout and has been hacked to no end to support application layout, drawing, and everything us developers want. But it's strength is word wrapping and document layout. Let HTML do what it does best, and let SVG do what it does best.

http://www.w3.org/TR/SVG/extend.html

This works for most browsers FireFox, Opera, Webkit, except IE (as of IE11). :-( Story of the web ain't it?

黑白记忆 2024-07-19 06:34:38

根据这个文档,似乎tspan可以给人自动换行的错觉:

tspan 标签与文本标签相同,但可以嵌套在文本标签及其自身内部。 与“dy”属性相结合,可以在 SVG 1.1 中产生自动换行的效果。 请注意,“dy”是相对于最后绘制的字形(字符)而言的。

Per this document, it appears that tspan can give the illusion of word wrap:

The tspan tag is identical to the text tag but can be nested inside text tags and inside itself. Coupled with the 'dy' attribute this allows the illusion of word wrap in SVG 1.1. Note that 'dy' is relative to the last glyph (character) drawn.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文