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

lengthy-svg

v0.7.0

Published

Microlibrary to give an SVG path's length in a CSS var.

Downloads

179

Readme

Lengthy

MicroLibrary for SVG Shape Length in a CSS Var

Lengthy is a JavaScript microlibrary (1.2kb min, 0.7kb gzipped) to get the length of SVG shapes. The length will automatically be added to the element as a CSS Var to make it easy to do CSS animations of SVG stroke-dashoffset for the wonderful line drawing SVG technique and other interesting animations.


Installation

Add Lengthy to a projects with npm:

npm install -s lengthy-svg

For easy embedding on platforms like Codepen, use unpkg

<script src="https://unpkg.com/lengthy-svg/lengthy-svg.js"></script>

--

Usage

Simply call Lengthy on a specific element, list of elements or a selector. The affected elements will receive a CSS var in their style and the lengthy class added.

Input

<svg viewBox="0 0 60 60">
  <circle data-lengthy cx="30" cy="30" r="20" />
</svg>

JavaScript

Lengthy("[data-lengthy]");

Output

<svg viewBox="0 0 60 60">
  <circle data-lengthy cx="30" cy="30" r="20" class="lengthy" style="--path-length:124.854;"></circle>
</svg>

CSS Animation

For a standard line-drawing animation, this is the CSS required.

It should be a relatively simple setup, but unfortunately, Chrome/Blink has a glitch with the CSS var animation keyframes. Chrome incorrectly treats a unitless calc()'d var() animation as a "discrete" animation, snapping to each value instead of transitioning between the values.

Firefox and Webkit/Blink all use unprefixed animation-name, so to target Webkit/Blink specifically, you'll use -webkit-animation-name for the override, then -moz-animation-name to override back for Firefox.

CSS Animation Demo

.lengthy {
  stroke-dasharray: var(--path-length) var(--path-length);

  animation-duration: 3s;
  animation-timing-function: cubic-bezier(0.5, 0, 0.5, 1);
  animation-iteration-count: infinite;
  animation-direction: alternate;

  animation-name: stroke-move;
  /* Override keyframes to fix a webkit glitch */
  -webkit-animation-name: webkit-stroke-move;
  /* Override yet again back to the original keyframes since Firefox obeys -webkit properties */
  -moz-animation-name: stroke-move;
}

@keyframes stroke-move {
  0%,
  10% {
    stroke-dashoffset: calc(1 * var(--path-length));
  }
  90%,
  100% {
    stroke-dashoffset: calc(-1 * var(--path-length));
  }
}

/**
 * Webkit does not support animating the stroke-dashoffset value _without_ a unit,
 * so we have to deliver a separate keyframe list via -webkit-animation-name
 */
@keyframes webkit-stroke-move {
  0%,
  10% {
    stroke-dashoffset: calc(1px * var(--path-length));
  }
  90%,
  100% {
    stroke-dashoffset: calc(-1px * var(--path-length));
  }
}

--

JavaScript Animation

Lengthy will return an array containing all the elements affected and their lengths if you want to use a JavaScript animation library like TweenRex or use the length data for other purposes.

JavaScript Animation Demo

const targets = Lengthy(".stripe");

const tweens = targets.map(function(target) {
  target.el.style.strokeDasharray = target.length;
  return {
    duration: 3000,
    subscribe: tweenrex.interpolate({
      targets: target.el,
      strokeWidth: [2, 4, 2],
      strokeDashoffset: [target.length, -target.length]
    })
  };
});

const t1 = TweenRex({
  onFinish: function() {
    t1.restart();
  }
});
t1.add(tweens, { stagger: 500 });
t1.restart();