UNPKG

4.8 kBJavaScriptView Raw
1// https://d3js.org/d3-path/ Version 1.0.0. Copyright 2016 Mike Bostock.
2(function (global, factory) {
3 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
4 typeof define === 'function' && define.amd ? define(['exports'], factory) :
5 (factory((global.d3 = global.d3 || {})));
6}(this, function (exports) { 'use strict';
7
8 var pi = Math.PI;
9 var tau = 2 * pi;
10 var epsilon = 1e-6;
11 var tauEpsilon = tau - epsilon;
12 function Path() {
13 this._x0 = this._y0 = // start of current subpath
14 this._x1 = this._y1 = null; // end of current subpath
15 this._ = [];
16 }
17
18 function path() {
19 return new Path;
20 }
21
22 Path.prototype = path.prototype = {
23 constructor: Path,
24 moveTo: function(x, y) {
25 this._.push("M", this._x0 = this._x1 = +x, ",", this._y0 = this._y1 = +y);
26 },
27 closePath: function() {
28 if (this._x1 !== null) {
29 this._x1 = this._x0, this._y1 = this._y0;
30 this._.push("Z");
31 }
32 },
33 lineTo: function(x, y) {
34 this._.push("L", this._x1 = +x, ",", this._y1 = +y);
35 },
36 quadraticCurveTo: function(x1, y1, x, y) {
37 this._.push("Q", +x1, ",", +y1, ",", this._x1 = +x, ",", this._y1 = +y);
38 },
39 bezierCurveTo: function(x1, y1, x2, y2, x, y) {
40 this._.push("C", +x1, ",", +y1, ",", +x2, ",", +y2, ",", this._x1 = +x, ",", this._y1 = +y);
41 },
42 arcTo: function(x1, y1, x2, y2, r) {
43 x1 = +x1, y1 = +y1, x2 = +x2, y2 = +y2, r = +r;
44 var x0 = this._x1,
45 y0 = this._y1,
46 x21 = x2 - x1,
47 y21 = y2 - y1,
48 x01 = x0 - x1,
49 y01 = y0 - y1,
50 l01_2 = x01 * x01 + y01 * y01;
51
52 // Is the radius negative? Error.
53 if (r < 0) throw new Error("negative radius: " + r);
54
55 // Is this path empty? Move to (x1,y1).
56 if (this._x1 === null) {
57 this._.push(
58 "M", this._x1 = x1, ",", this._y1 = y1
59 );
60 }
61
62 // Or, is (x1,y1) coincident with (x0,y0)? Do nothing.
63 else if (!(l01_2 > epsilon));
64
65 // Or, are (x0,y0), (x1,y1) and (x2,y2) collinear?
66 // Equivalently, is (x1,y1) coincident with (x2,y2)?
67 // Or, is the radius zero? Line to (x1,y1).
68 else if (!(Math.abs(y01 * x21 - y21 * x01) > epsilon) || !r) {
69 this._.push(
70 "L", this._x1 = x1, ",", this._y1 = y1
71 );
72 }
73
74 // Otherwise, draw an arc!
75 else {
76 var x20 = x2 - x0,
77 y20 = y2 - y0,
78 l21_2 = x21 * x21 + y21 * y21,
79 l20_2 = x20 * x20 + y20 * y20,
80 l21 = Math.sqrt(l21_2),
81 l01 = Math.sqrt(l01_2),
82 l = r * Math.tan((pi - Math.acos((l21_2 + l01_2 - l20_2) / (2 * l21 * l01))) / 2),
83 t01 = l / l01,
84 t21 = l / l21;
85
86 // If the start tangent is not coincident with (x0,y0), line to.
87 if (Math.abs(t01 - 1) > epsilon) {
88 this._.push(
89 "L", x1 + t01 * x01, ",", y1 + t01 * y01
90 );
91 }
92
93 this._.push(
94 "A", r, ",", r, ",0,0,", +(y01 * x20 > x01 * y20), ",", this._x1 = x1 + t21 * x21, ",", this._y1 = y1 + t21 * y21
95 );
96 }
97 },
98 arc: function(x, y, r, a0, a1, ccw) {
99 x = +x, y = +y, r = +r;
100 var dx = r * Math.cos(a0),
101 dy = r * Math.sin(a0),
102 x0 = x + dx,
103 y0 = y + dy,
104 cw = 1 ^ ccw,
105 da = ccw ? a0 - a1 : a1 - a0;
106
107 // Is the radius negative? Error.
108 if (r < 0) throw new Error("negative radius: " + r);
109
110 // Is this path empty? Move to (x0,y0).
111 if (this._x1 === null) {
112 this._.push(
113 "M", x0, ",", y0
114 );
115 }
116
117 // Or, is (x0,y0) not coincident with the previous point? Line to (x0,y0).
118 else if (Math.abs(this._x1 - x0) > epsilon || Math.abs(this._y1 - y0) > epsilon) {
119 this._.push(
120 "L", x0, ",", y0
121 );
122 }
123
124 // Is this arc empty? We’re done.
125 if (!r) return;
126
127 // Is this a complete circle? Draw two arcs to complete the circle.
128 if (da > tauEpsilon) {
129 this._.push(
130 "A", r, ",", r, ",0,1,", cw, ",", x - dx, ",", y - dy,
131 "A", r, ",", r, ",0,1,", cw, ",", this._x1 = x0, ",", this._y1 = y0
132 );
133 }
134
135 // Otherwise, draw an arc!
136 else {
137 if (da < 0) da = da % tau + tau;
138 this._.push(
139 "A", r, ",", r, ",0,", +(da >= pi), ",", cw, ",", this._x1 = x + r * Math.cos(a1), ",", this._y1 = y + r * Math.sin(a1)
140 );
141 }
142 },
143 rect: function(x, y, w, h) {
144 this._.push("M", this._x0 = this._x1 = +x, ",", this._y0 = this._y1 = +y, "h", +w, "v", +h, "h", -w, "Z");
145 },
146 toString: function() {
147 return this._.join("");
148 }
149 };
150
151 exports.path = path;
152
153 Object.defineProperty(exports, '__esModule', { value: true });
154
155}));
\No newline at end of file