npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wrap-svg-text

v0.0.7

Published

wrap svg text into group of multiline texts

Downloads

27

Readme

wrap-svg-text

Generate multiline svg texts directly through a text string and a set of css style. Example:

wrapSvgText({
    text: "Thank you, Chairman Rayon To Go(聽不懂他的發音人名), Chairman 還有我們President, 還有各位美僑商, 美國商會所有的好朋友, 還有各位新聞界的女士先生."
    style: {
        fontSize: "32px",
        width: "300px",
        whiteSpace: "pre-line"
    }
});

Usage

install:

npm install --save wrap-svg-text

include:

<script src="path-to-wrap-svg-text.js"></script>

call:

ret = wrapSvgText({text: '...', style: { ... }});
myGroup.textContent = ""; /* clear myGroup content */
myGroup.appendChild(ret)

with prepared div node so we don't have to mimic its style ( content inside ):

ret = wrapSvgText({node: node});
myGroup.textContent = ""; /* clear myGroup content */
myGroup.appendChild(ret)

Options

  • text: string. text to render.
  • style: object containing styles for rendering above text. optional.
  • node: when provided, use this node to layout content.
    • content may be altered due to algorithm in wrap-svg-text. check useRange for more detail.
    • node should contain plain text only.
    • text can be omitted if there are contents already in node
    • style will be ignored when node is provided.
  • useRange: if true, use document.range for position calculation. default false. see below.

useRange

wrap-svg-text by default get characters' position by wrapping them in span. however, this may touch node's content if user provide node as a paramter when calling wrap-svg-text. This may causing re-layout and change the position of elements, leading to incorrect result.

In this case, user can alternatively enable useRange and let wrap-svg-text calculate the position with range.getBoundingClientRect. However, this is 2 times slower so only suggested using for short text.

Mechanism

wrap-svg-text simply calculates layouts of texts with the help of browser layout engine. Input texts with specified style are added directly in a hidden div, with every single glyph wrapped in span. Then, span are joined based on their y coordinate, and added back as a <text> tag in a <g> tag, which is returned from wrapSvgText function call.

Note

Layout is calculated directly based on CSS and DOM, thus it's important to make sure that when running wrap-svg-text, CSS and DOM are ready and correctly rendered, otherwise wrap-svg-text may give incorrect result due to style updating.

Credit

Sample text from 'HolyMosquito's transcription of Han's speech in AmCham Taipei, a good example of English / Chinese mixed context.

License

MIT