Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

modern-path2d

qq157251.4kMIT1.4.16TypeScript support: included

A Path2D library, fully compatible with Web Path2D, with additional support for triangulate、animation、deformation etc.

typescript, path2d, path2d-animation, path2d-deformation, path2d-playback

readme

modern-path2d

Minzip Version Downloads Issues License

Features

  • Compatible Web Path2D

  • Path transform

  • Path triangulate (fill、stroke)

  • Parse SVG to Path2DSet

  • TypeScript

📦 Install

npm i modern-path2d

🦄 Usage

import { Path2D, Path2DSet, svgToPath2DSet } from 'modern-path2d'

const path = new Path2D()

// Window.Path2D methods
path.arc(75, 75, 50, 0, Math.PI * 2, true)
path.moveTo(110, 75)
path.arc(75, 75, 35, 0, Math.PI, false)
path.moveTo(65, 65)
path.arc(60, 65, 5, 0, Math.PI * 2, true)
path.moveTo(95, 65)
path.arc(90, 65, 5, 0, Math.PI * 2, true)

// add SVG path data
path.addData('M10,30 A20,20 0,0,1 50,30 A20,20 0,0,1 90,30 Q90,60 50,90 Q10,60 10,30 z M5,5 L90,90')

// add SVG path commands
path.addCommands([
  { type: 'M', x: 118, y: 39 },
  { type: 'L', x: 218, y: 39 }
])

// add SVG XML
const pathSet = svgToPath2DSet(`<svg xmlns="http://www.w3.org/2000/svg" width="72" height="72" viewBox="0 0 72 72" fill="none">
<path d="M51.3646 45.8642C49.7808 46.2782 47.906 46.705 45.8588 47.0857M45.8588 47.0857C34.1649 49.2607 16.8486 49.9343 16.0277 38.1484C15.22 26.5533 32.264 22.3636 45.6135 24.5626C53.601 25.8783 57.4507 29.6208 57.9285 34.237C58.2811 37.6435 55.778 43.3702 45.8588 47.0857ZM45.8588 47.0857C42.3367 48.4051 37.8795 49.4708 32.283 50.0891" stroke="#FFC300" stroke-width="2.5" stroke-linecap="round"/>
</svg>`)
pathSet.paths.forEach((parsedPath) => {
  path.addPath(parsedPath)
})

/**
 * Export
 */

// export SVG path data
console.log(path.toData())

// export SVG path commands
console.log(path.toCommands())

// export to canvas ctx
path.drawTo(document.getElementById('canvas').getContext('2d'))

// export to canvas
document.body.append(new Path2DSet([path]).toCanvas())

// export to SVG DOM
document.body.append(new Path2DSet([path]).toSvg())

/**
 * Triangulate
 */

// triangulate for fill
console.log(path.fillTriangulate())

// triangulate for stroke
console.log(path.strokeTriangulate())

changelog

1.4.16 (2025-12-16)

Bug Fixes

1.4.15 (2025-12-10)

Bug Fixes

1.4.14 (2025-12-08)

Bug Fixes

1.4.13 (2025-12-08)

Bug Fixes

1.4.12 (2025-12-08)

Bug Fixes

1.4.11 (2025-11-03)

Bug Fixes

1.4.10 (2025-09-25)

Bug Fixes

1.4.9 (2025-08-25)

Features

1.4.8 (2025-07-23)

Bug Fixes

1.4.7 (2025-07-22)

Bug Fixes

1.4.6 (2025-07-22)

Bug Fixes

Features

  • up deps / change dir layout (c35cadf)

1.4.5 (2025-07-10)

Bug Fixes

1.4.4 (2025-07-10)

1.4.3 (2025-07-09)

Bug Fixes

1.4.2 (2025-07-09)

Bug Fixes

1.4.1 (2025-06-26)

Features

  • rename svgToDOM to svgToDom etc (1dde3ab)

1.4.0 (2025-06-26)

Features

  • rename toSVG to toSvg etc (a4854c5)

1.3.5 (2025-06-24)

Bug Fixes

1.3.4 (2025-06-11)

Bug Fixes

1.3.3 (2025-05-30)

Bug Fixes

1.3.2 (2025-05-28)

Bug Fixes

1.3.1 (2025-05-28)

Bug Fixes

  • LineCurve and RoundCurve the getAdaptivePointArray (22fc18e)

1.3.0 (2025-05-12)

Bug Fixes

  • getMinMax value sometime is infinity (278231d)

Features

1.2.19 (2025-04-24)

Features

  • applyTransform supports function (2b72eb6)

1.2.18 (2025-03-27)

Bug Fixes

1.2.17 (2025-03-27)

Bug Fixes

  • RoundCurve.getAdaptivePointArray (517fe59)

1.2.16 (2025-03-22)

Features

  • add Path2D.toTriangulatedSVG (b39ac66)

1.2.15 (2025-03-21)

Bug Fixes

1.2.14 (2025-03-21)

Bug Fixes

1.2.13 (2025-03-21)

Bug Fixes

1.2.12 (2025-03-21)

Bug Fixes

  • RoundCurve.getAdaptivePointArray (57e85c8)

1.2.11 (2025-03-20)

Features

  • Path2DSet add viewBox prop (c0f6bad)

1.2.10 (2025-01-22)

Bug Fixes

1.2.9 (2025-01-21)

Bug Fixes

1.2.8 (2025-01-21)

Bug Fixes

1.2.7 (2025-01-20)

Bug Fixes

1.2.6 (2025-01-08)

Bug Fixes

1.2.5 (2025-01-08)

Bug Fixes

1.2.4 (2025-01-08)

Bug Fixes

1.2.3 (2025-01-07)

Features

1.2.2 (2025-01-07)

Bug Fixes

1.2.1 (2025-01-07)

Features

  • rename svgToDom to svgToDOM (c837b3d)

1.2.0 (2025-01-07)

Bug Fixes

  • ArcCurve.getAdaptivePointArray (b0a1af5)
  • RoundCurve.getAdaptivePointArray (120bf09)

Features

  • Path2D.roundRect (70bdacc)
  • util method parseSVG rename to svgToPath2DSet (cef7d40)

1.1.0 (2025-01-06)

Bug Fixes

Features

  • add fillTriangulatestrokeTriangulategetAdaptivePoints method to Curve (eb3d8b1)
  • EquilateralPloygonCurve (eadefcb)
  • update CompositeCurve (301a10a)
  • update triangulate (1cc87e6)

1.0.1 (2024-12-30)

Bug Fixes

1.0.0 (2024-12-23)

Features

  • rename method toSvg to toSVG (a1fcb6a)

0.2.9 (2024-12-23)

Bug Fixes

  • throw error in parseSvgDom error (fb9e36f)

0.2.8 (2024-12-21)

Bug Fixes

  • moveTo 0,0 point not set currentPoint in Path2D (5f1e943)
  • path cmds to path data (3798882)
  • svg: parse sweepFlag to counterclockwise error (a7a596d)

0.2.7 (2024-12-18)

  • fix: pathCommandsToPathData (a9aaeec)

0.2.6 (2024-11-18)

Features

0.2.5 (2024-11-11)

Features

  • add BoundingBox.copy util method (c6bfe4e)

0.2.4 (2024-11-07)

Bug Fixes

0.2.3 (2024-11-07)

Bug Fixes

0.2.2 (2024-11-05)

Bug Fixes

0.2.1 (2024-11-04)

Features

0.2.0 (2024-10-30)

Features

  • add scale、skew、rotate、bold method (a252426)

0.1.18 (2024-10-25)

Bug Fixes

0.1.17 (2024-10-24)

Features

  • support style option to drawTo method (9464363)

0.1.16 (2024-10-18)

0.1.15 (2024-10-18)

Bug Fixes

  • CubicBezierCurve min max (5f0f0e5)

0.1.14 (2024-10-18)

Features

0.1.13 (2024-10-18)

0.1.12 (2024-10-18)

Features

0.1.11 (2024-10-18)

Bug Fixes

  • path2D render from parseSvg (468bb80)

0.1.10 (2024-10-16)

Features

0.1.9 (2024-10-16)

0.1.8 (2024-10-16)

Features

0.1.7 (2024-10-14)

Bug Fixes

0.1.6 (2024-10-14)

Features

  • parseSvg supports base64 dataURI and utf8 dataURI (013354d)

0.1.5 (2024-10-14)

Bug Fixes

0.1.4 (2024-10-13)

Bug Fixes

0.1.3 (2024-10-12)

Bug Fixes

0.1.2 (2024-10-11)

Features

0.1.1 (2024-10-11)

Features

0.1.0 (2024-10-11)

Bug Fixes

  • QuadraticBezierCurve transform (d784f9d)

Features

0.0.5 (2024-10-09)

Features

  • support create Path2D by commands (1971e98)

0.0.4 (2024-10-08)

Features

  • rename getPathCommands to getCommands (4e0f03a)

0.0.3 (2024-09-30)

Bug Fixes

  • EllipseCurve path commands (d47008b)

Features

  • add arcTo、ellipse methods (1c572d4)
  • add getBoundingBox、getPathData、getPathCommands methods (407fc9e)
  • add getSvgDataUri method (f3c5cde)

0.0.2 (2024-09-26)

Bug Fixes

Features

0.0.1 (2024-09-24)