SVG Word Wrap - Show stopper?

前端 未结 8 966
既然无缘
既然无缘 2020-12-23 21:42

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

相关标签:
8条回答
  • 2020-12-23 22:15

    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.

    0 讨论(0)
  • 2020-12-23 22:23

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

    0 讨论(0)
  • 2020-12-23 22:25

    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.

    0 讨论(0)
  • 2020-12-23 22:26

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

    0 讨论(0)
  • 2020-12-23 22:28

    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.

    0 讨论(0)
  • 2020-12-23 22:36

    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.

    0 讨论(0)
提交回复
热议问题