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

three-svg-js

v0.5.6

Published

[![NPM Package][npm]][npm-url]

Downloads

8

Readme

three-svg-js

NPM Package

Three.js classes to display SVG as geometry, parse SVG and building geometry with SVG like methods. Its similar to SVGLoader but with more features and programmatic control

Features

  • SVG Parsing
  • SVG version of Shape Path
  • Save and load JSON format
  • Stroke and gradiant support
  • Text path support

Installation

npm install three three-svg-js

npm install @types/three --save-dev

Demo

Live Demo

Usage

SVGLoader is mostly a black box that takes an SVG file and outputs an array of ShapePaths. Unfortunately, none of the structure of the original SVG file is accessible to dynamically build or modify the shapes. Classes in this library give you that access and control

Initialization

The SVGShape class can load an SVG document for adding to the scene

const svgshape = new SVGShape()
svgshape.loadSVG(`<svg width="300" height="200" xmlns="http://www.w3.org/2000/svg">
  <rect width="100%" height="100%" fill="red" />
  <circle cx="150" cy="100" r="80" fill="green" />
  <text x="150" y="125" font-size="60" text-anchor="middle" fill="white">SVG</text>
</svg>`)
svgshape.update() // generate the geometry
scene.add(svgshape)

Codesandbox Example

Manipulating SVG Shapes

The SVGShape class has methods that mimic an SVG document to allow programmatic building shapes and geometry

const svgshape = new SVGShape({ width: 300, height: 200 })
.rect({
  width: "100%",
  height: "100%",
  fill: "red"
})
.circle({ cx: 150, cy: 100, r: 80, fill: "green" })
.text({
  content: 'SVG',
  x: "150",
  y: "125",
  fontSize: 60,
  textAnchor: "middle",
  fill: "white"
})

This is equivalent to the original SVG

<svg version="1.1" width="300" height="200" xmlns="http://www.w3.org/2000/svg">
  <rect width="100%" height="100%" fill="red" />
  <circle cx="150" cy="100" r="80" fill="green" />
  <text x="150" y="125" font-size="60" text-anchor="middle" fill="white">SVG</text>
</svg>

Adding to ThreeJS scene

SVGShape extends Mesh, so can be added to the scene and be positioned and scaled like any other Object3D

svgshape.update() // generate the geometry
scene.add(svgshape)

SVGParser

SVGParser converts an SVG file to equivalent JSON format for loading into SVGShape. This is equivalent to SVGLoader parse method adapted to just create an equivalent data representation. It has no dependencies on threejs so can be used as a standalone component.

const svg = `<svg version="1.1" width="300" height="200" xmlns="http://www.w3.org/2000/svg">
  <rect width="100%" height="100%" fill="red" />
  <circle cx="150" cy="100" r="80" fill="green" />
  <text x="150" y="125" font-size="60" text-anchor="middle" fill="white">SVG</text>
</svg>`

const parser = new SVGParser()
const schema = parser.parse(svg)

Here's the JSON data structure

export interface SVGSchema {
  options?: SVGShapeOptions
  gradients?: Array<LinearGradient | RadialGradient>
  elements: Array<ShapeTypes>
}

export interface ShapeTypes {
  circle?: CircleParams
  ellipse?: EllipseParams
  group?: GroupShapeType
  line?: LineParams
  path?: PathParams
  polygon?: PolygonParams
  polyline?: PolylineParams
  rect?: RectParams
  text?: TextParams
}

export interface GroupShapeType {
  options?: PresentationAttributes,
  elements: Array<ShapeTypes>
}

SVGShape provides the load method to convert this data structure into equivalent shapes

const svgshape = new SVGShape(schema.options)
svgshape.load(schema)
svgshape.update() // generate the geometry
scene.add(svgshape)

SVGShape provides the save method to convert the current shape definition into equivalent data structure

const svgshape = new SVGShape(schema.options)
.rect({
  width: "100%",
  height: "100%",
  fill: "red"
})
const schema = svgshape.save()

Reference

SVGShape

SVGShape mesh for threejs. It supports

  • parsing SVG into JSON format for storage or programmatic manipulation
  • loading JSON format to define mesh and geometry for adding to the scene
  • uses SVGLoader undocumented pointsToStroke method to support SVG stroke style
  • supports basic linear and radial gradient texture as fill style

SVGShapePath

Used by SVGShape to generate the shape paths. Improves on ShapePath in the following ways

  • adds missing arc, absarc, ellipse, absellipse and closePath methods
  • adds support for processing SVG path commands

Shape classes are provided for each type of SVG element

  • CircleShape - equivalent to SVG circle
  • EllipseShape - equivalent to SVG ellipse
  • GroupShape - equivalent to SVG group
  • LineShape - equivalent to SVG line
  • PathShape - equivalent to SVG path
  • PolygonShape - equivalent to SVG polygon
  • PolylineShape - equivalent to SVG polyline
  • RectShape - equivalent to SVG rect
  • SVGShape - equivalent to SVG element
  • TextShape - equivalent to SVG text
  • BaseShape - base class for all shapes

BaseShape includes methods for creating stroke and fill materials and converting each shape or stroke to geometry

Developer Notes

The library has been checked against a wide range of SVG content including

Known Limitations / Not Supported

The following SVG features are not supported by SVGLoader or this library

  • clipPath

  • mask

  • pattern

  • filter effect

  • animate

  • set

  • animateColor

  • animateTransform

  • title

  • image

  • SVG fonts

  • fill-rule="evenodd"

  • gradientUnits="useSpaceOnUse"

  • stroke-dasharray

  • rotateONAxis, skewX and skewY transforms

  • nested <use>