Given a
<div
lang="en"
style="
overflow-wrap: word-break;
hyphens: auto;
width: 8.6em;
background-color: highlight;
color: highlightText;
font-size: 20px;
">
This is a text that is long enough
to cause a line break if set so.
By using words like
"incomprehensibilities",
we can demonstrate word breaks.
</div>
I would like to access the formatted text with hyphenations with JavaScript to get a string similar to:
"This is a text that is long enough to cause a line break if set so. By using words like "incomprehensibilit-ies", we can demon-strate word breaks.".
N.B. hyphens in incomprehensibilit-ies and demon-strate.
Is this possible?
The use case is that I need a way to break and hyphenate text in a <text> element of an SVG by only using SVG elements.
From the docs
Also read: https://www.brucebrotherton.com/blog/hyphens-on-the-web/
It is the browser in charge of rendering text, you have to analyze where text is rendered. With Canvas or SPAN
Far from perfect. This one:
<span>offsetHeightif a SPAN spans multiple lines<span>to find when the word wrapped to a new line
The Far from perfect is the word "demonstrate" which "fits" when shortened to "demonstr" - "ate" But is not the English spelling
Needs some more JS voodoo