d3-path
Serialize Canvas path commands to SVG.
Last updated 5 months ago by recifs .
BSD-3-Clause · Repository · Original npm · Tarball · package.json
$ cnpm install d3-path 
SYNC missed versions from official npm registry.

d3-path

Say you have some code that draws to a 2D canvas:

function drawCircle(context, radius) {
  context.moveTo(radius, 0);
  context.arc(0, 0, radius, 0, 2 * Math.PI);
}

The d3-path module lets you take this exact code and additionally render to SVG. It works by serializing CanvasPathMethods calls to SVG path data. For example:

var context = d3.path();
drawCircle(context, 40);
pathElement.setAttribute("d", context.toString());

Now code you write once can be used with both Canvas (for performance) and SVG (for convenience). For a practical example, see d3-shape.

Installing

If you use NPM, npm install d3-path. Otherwise, download the latest release. You can also load directly from d3js.org, either as a standalone library or as part of D3. AMD, CommonJS, and vanilla environments are supported. In vanilla, a d3 global is exported:

<script src="https://d3js.org/d3-path.v2.min.js"></script>
<script>

var path = d3.path();
path.moveTo(1, 2);
path.lineTo(3, 4);
path.closePath();

</script>

API Reference

# d3.path() · Source, Examples

Constructs a new path serializer that implements CanvasPathMethods.

# path.moveTo(x, y)

Move to the specified point ⟨x, y⟩. Equivalent to context.moveTo and SVG’s “moveto” command.

# path.closePath()

Ends the current subpath and causes an automatic straight line to be drawn from the current point to the initial point of the current subpath. Equivalent to context.closePath and SVG’s “closepath” command.

# path.lineTo(x, y)

Draws a straight line from the current point to the specified point ⟨x, y⟩. Equivalent to context.lineTo and SVG’s “lineto” command.

# path.quadraticCurveTo(cpx, cpy, x, y)

Draws a quadratic Bézier segment from the current point to the specified point ⟨x, y⟩, with the specified control point ⟨cpx, cpy⟩. Equivalent to context.quadraticCurveTo and SVG’s quadratic Bézier curve commands.

# path.bezierCurveTo(cpx1, cpy1, cpx2, cpy2, x, y)

Draws a cubic Bézier segment from the current point to the specified point ⟨x, y⟩, with the specified control points ⟨cpx1, cpy1⟩ and ⟨cpx2, cpy2⟩. Equivalent to context.bezierCurveTo and SVG’s cubic Bézier curve commands.

# path.arcTo(x1, y1, x2, y2, radius)

Draws a circular arc segment with the specified radius that starts tangent to the line between the current point and the specified point ⟨x1, y1⟩ and ends tangent to the line between the specified points ⟨x1, y1⟩ and ⟨x2, y2⟩. If the first tangent point is not equal to the current point, a straight line is drawn between the current point and the first tangent point. Equivalent to context.arcTo and uses SVG’s elliptical arc curve commands.

# path.arc(x, y, radius, startAngle, endAngle[, anticlockwise])

Draws a circular arc segment with the specified center ⟨x, y⟩, radius, startAngle and endAngle. If anticlockwise is true, the arc is drawn in the anticlockwise direction; otherwise, it is drawn in the clockwise direction. If the current point is not equal to the starting point of the arc, a straight line is drawn from the current point to the start of the arc. Equivalent to context.arc and uses SVG’s elliptical arc curve commands.

# path.rect(x, y, w, h)

Creates a new subpath containing just the four points ⟨x, y⟩, ⟨x + w, y⟩, ⟨x + w, y + h⟩, ⟨x, y + h⟩, with those four points connected by straight lines, and then marks the subpath as closed. Equivalent to context.rect and uses SVG’s “lineto” commands.

# path.toString()

Returns the string representation of this path according to SVG’s path data specification.

Current Tags

  • 2.0.0                                ...           latest (5 months ago)
  • 2.0.0-rc.1                                ...           next (6 months ago)

21 Versions

  • 2.0.0                                ...           5 months ago
  • 2.0.0-rc.1                                ...           6 months ago
  • 1.0.9                                ...           a year ago
  • 1.0.8                                ...           a year ago
  • 1.0.7                                ...           2 years ago
  • 1.0.6                                ...           2 years ago
  • 1.0.5                                ...           4 years ago
  • 1.0.4                                ...           4 years ago
  • 1.0.3                                ...           4 years ago
  • 1.0.2                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           5 years ago
  • 0.2.0                                ...           5 years ago
  • 0.1.5                                ...           5 years ago
  • 0.1.4                                ...           5 years ago
  • 0.1.3                                ...           5 years ago
  • 0.1.2                                ...           5 years ago
  • 0.1.1                                ...           5 years ago
  • 0.1.0                                ...           5 years ago
  • 0.0.2                                ...           5 years ago
  • 0.0.1                                ...           5 years ago
Maintainers (2)
Downloads
Today 1,373
This Week 31,783
This Month 99,368
Last Day 5,764
Last Week 32,667
Last Month 145,296
Dependencies (0)
None
Dev Dependencies (4)
Dependents (138)

Copyright 2014 - 2016 © taobao.org |