百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

svg-points

> 编程语言
开源

一种用于在 JavaScript 中存储 SVG 形状数据的规范,以及一些实用的转换函数

100 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

一种用于在 JavaScript 中存储 SVG 形状数据的规范,以及一些实用的转换函数

SVG points

A specification for storing SVG shape data in Javascript.

Best paired with the classic points library for powerful shape manipulation.

If you are looking to convert a SVG DOM node directly to SVG points, then check out the plainShapeObject function of Wilderness DOM node.

2.6kb gzipped.

Example shape

javascript
{
  type: 'circle',
  cx: 50,
  cy: 50,
  r: 20
}

Functions

  • toPoints – converts an SVG shape object to a points array
  • toPath – converts an SVG shape object or a points array to an SVG path d attribute string.
  • valid – checks an SVG shape object is valid

Specification

A SVG shape is an object that includes a type property that can take one of the following strings.

  • circle
  • ellipse
  • line
  • path
  • polygon
  • polyline
  • rect
  • g

It also maps all the other required SVG attributes for that particular shape to object properties.

Shape types

circle

javascript
{
  type: 'circle',
  cx: 50,
  cy: 50,
  r: 20
}

ellipse

javascript
{
  type: 'ellipse',
  cx: 100,
  cy: 300,
  rx: 65,
  ry: 120
}

line

javascript
{
  type: 'line',
  x1: 10,
  x2: 50,
  y1: 70,
  y2: 200
}

path

javascript
{
  type: 'path',
  d: 'M20,20h50v20A2,2,0,0,1,80,35L90,30H50V50a5,5,45,1,0-5-10l-5-10Z'
}

polygon

javascript
{
  type: 'polygon',
  points: '20,30 50,90 20,90 50,30'
}

polyline

javascript
{
  type: 'polyline',
  points: '20,30 50,90 20,90 50,30'
}

rect

javascript
{
  type: 'rect',
  height: 20,
  width: 50,
  x: 10,
  y: 10,
  rx: 2,
  ry: 2
}

The properties rx and ry are optional and if missing are assumed to be 0.

g

javascript
{
  type: 'g',
  shapes: [
    {
      type: 'circle',
      cx: 50,
      cy: 50,
      r: 20
    },
    {
      type: 'line',
      x1: 10,
      x2: 50,
      y1: 70,
      y2: 200
    }
  ]
}

Installation

bash
npm install svg-points

Usage

toPoints

javascript
import { toPoints } from 'svg-points'

const circle = {
  type: 'circle',
  cx: 50,
  cy: 50,
  r: 20
}

const points = toPoints(circle)

console.log(points)

// [
//   { x: 50, y: 30, moveTo: true },
//   { x: 50, y: 70, curve: { type: 'arc', rx: 20, ry: 20, sweepFlag: 1 } },
//   { x: 50, y: 30, curve: { type: 'arc', rx: 20, ry: 20, sweepFlag: 1 } }
// ]

Takes an SVG shape object as the only argument, and returns a new points array.

If passing in a group shape object then returns an array of points arrays.

toPath

javascript
import { toPath } from 'svg-points'

const circle = {
  type: 'circle',
  cx: 50,
  cy: 50,
  r: 20
}

const d = toPath(circle)

console.log(d)

// 'M50,30A20,20,0,0,1,50,70A20,20,0,0,1,50,30Z'

Takes either an SVG shape object, or a points array, and returns a SVG path d attribute string.

If passing in a group shape object, or an array of points arrays then returns an array of SVG path d attribute strings.

valid

javascript
import { valid } from 'svg-points'

const ellipse = {
  type: 'ellipse',
  cy: 50,
  rx: 5,
  ry: 10
}

const { errors } = valid(ellipse)

console.log(errors)

// [ 'cx prop is required on a ellipse' ]

CommonJS

This is how you get to the good stuff if you're using require.

javascript
const SVGPoints = require('svg-points')
const toPoints = SVGPoints.toPoints
const toPath = SVGPoints.toPath

UMD

And if you just want to smash in a Javascript file you're also covered. Drop this in place ...

https://unpkg.com/svg-points/dist/svg-points.min.js

Then access it on the SVGPoints global variable.

javascript
const toPoints = SVGPoints.toPoints
const toPath = SVGPoints.toPath

Help make this better

Issues and pull requests gratefully received!

I'm also on twitter @colinmeinke.

Thanks :star2:

License

ISC.

Issues· 2 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScriptjavascriptshapessvg

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言