SVG Word Wrap - Show stopper?

前端 未结 8 991
既然无缘
既然无缘 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: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.

    
        Here a realy long 
        title which needs 
        wrapping 
    
    

    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  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  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  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.

提交回复
热议问题