Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8816415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:36:59+00:00 2026-06-14T04:36:59+00:00

My app is dependent on the conversion of some HTML text elements into SVG

  • 0

My app is dependent on the conversion of some HTML text elements into SVG elements, using Raphael.js.

The text in those HTML objects is given by the user, via a textarea input. Therefore, they can input new lines too. And those new lines need to be accounted for when creating the SVG.
For this, I use the following code:

function replaceNL(text) {      
    return text.replace(/[\n\r]/g, "\n");
}

And when adding the SVG to the page:

var obj = paper.text(x,y,replaceNL(this.text));

The problem I’ve come across is that double (or more) line breaks strings (e.g. “\n\n”) have the effect of just one in the .text() method.
How can I overcome this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:37:01+00:00Added an answer on June 14, 2026 at 4:37 am

    This question does come up periodically… (http://stackoverflow.com/questions/12155954/nbsp-in-raphael-js)

    If you modify your paper element to preserve spaces, all of the text elements within it will behave in the way you want. Do it like this:

    paper.canvas.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:space","preserve");
    

    Here’s the caveat: Raphael splits the strings passed to text on newlines, and inserts each segment into its own tspan. When the SVG rendering evaluates the tspans, it is sequencing each based on the bounding box of its predecessors. Because an empty tspan has a 0x0 bounding box, the newline — though emitted — is functionally invisible.

    The rough workaround would be to emit “\n \n” — the space character produces a bounding box, and thus causes the extra newline to be rendered. Sanity test here.

    To control the height of the line breaks between tspans, you need to modify their dy attributes. Raphael does not support this out of the box, but it’s easy to write a javascript function to set these manually. Here’s the jquery method, given a Raphael text element textElement and your desired spacing, in pixels, as spacing:

    $(textElement.node).find( 'tspan' ).attr( 'dy', spacing );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My app is creating two sessions for a user dependent on whether the user
I am building a Web App. At some point a user needs to input
OOTB, Robolectric does not support Locales that well. Therefore, if your app is dependent
In my app I show some info and depending on the value I change
I have built libshared.so with is dependent on libshared_dependent.so Now, I am compiling app.bin
I have Rails app with a Postgres backend. I need to add full text
I am using WPF to generate tile images for my WinRT app. I have
Hi there I am using corelocation in my app and everything works fine and
I have an app with users, relationships, posts, likes. My models are: class User
I recently upgraded a Web Application Project (as well as some dependent projects) from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.