-
Notifications
You must be signed in to change notification settings - Fork 465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Getting the bounding box of a text #124
Comments
The font contains data for this, stored in the |
The original request was to get the bounding box of a given text as a Path I guess. I think it would be great if the Path object would contain such information. |
+1. We need this for a project we're working on. Is this as simple as determining the min/max x and min/mix y and creating a box from the command data? I might try and do this right now. |
Did this work for you? |
svg-path-bounding-box works well. |
Sorry, i meant get the size when using in browser. |
You can use svg-path-bounding-box in browser. |
@panarch sorry, I am a little not familiar with how nodejs works. Which of the .js files would I reference in the browser? And how do I call it on a path? |
@villanus That's ok, yes it's true that svg-path-bounding-box lib does not provide minified version. So, if you want to use the lib, you may need to use browserify or webpack to make bundle js file. It looks that the entry file is caster.js Anyway, in case of using svg-path-bounding-box in node.js is simple.
|
Thanks, I have been avoiding NodeJS for the longest time, and now feel stupid when I actually need to use it. =) |
You're welcome, NodeJS will be ok, it's kind enough compared to web front-end... |
RaphaelJS also has
RaphaelJS is self-contained, so just need to drop in the JS file. |
thanks |
SVGPathElements provide a getBBox method as well. If you're creating SVG path elements by parsing the string returned by |
@hypothete does this mean you did not have to use a 3rd party library such as SVG.js or Raphael? If so, can you please put a code sample? I read through your comment a few times and am not smart enough to follow. =) |
@villanus for sure, here's what I mean. You don't need a 3rd party library for this measurement when you are working on the frontend. This does not apply to node.js.
Since I'm explicitly creating the element in the SVG namespace, its prototype is the SVGPathElement instead of a generic Element, and so it has additional methods like getBBox that you couldn't get otherwise. Here's how to create the path element incorrectly as a generic Element. Don't do this!
You would think that providing the MIME type to the parser would handle assigning the right element type to the parsed path, but it does not. Now we want to measure the path. To get the bounds reliably across browsers, you must place the path on the DOM. This code makes a temporary SVG, adds it to the body, and removes it when we're done taking our measurement:
|
Creating a SVG Path element correctly seems like something useful to add to the OpenType.js API (with a disclaimer that it only works in the browser environment). I didn't know you had to add it to the DOM to measure it correctly though! That's a bit silly. |
@fdb yes… :( |
@hypothete Thank you! |
Would jsdom support this? I have other node code the uses createElementNS in jsdom in svg namespace
|
@hypothete have you tried your fix in IE<11? I am unclear as to older IE SVG support. Seems like spotty support before 11. I guess OpenType works in IE10+ so that would be the only issue. |
Version 0.6.8 now includes a |
Hi !
I'm trying to get the bounding box of a whole font. Not from each glyph.
Do you plan to integrate it as feature ?
Thanks
The text was updated successfully, but these errors were encountered: