source: node_modules/d3-shape/src/curve/catmullRom.js@ e4c61dd

Last change on this file since e4c61dd was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 2.6 KB
Line 
1import {epsilon} from "../math.js";
2import {Cardinal} from "./cardinal.js";
3
4export function point(that, x, y) {
5 var x1 = that._x1,
6 y1 = that._y1,
7 x2 = that._x2,
8 y2 = that._y2;
9
10 if (that._l01_a > epsilon) {
11 var a = 2 * that._l01_2a + 3 * that._l01_a * that._l12_a + that._l12_2a,
12 n = 3 * that._l01_a * (that._l01_a + that._l12_a);
13 x1 = (x1 * a - that._x0 * that._l12_2a + that._x2 * that._l01_2a) / n;
14 y1 = (y1 * a - that._y0 * that._l12_2a + that._y2 * that._l01_2a) / n;
15 }
16
17 if (that._l23_a > epsilon) {
18 var b = 2 * that._l23_2a + 3 * that._l23_a * that._l12_a + that._l12_2a,
19 m = 3 * that._l23_a * (that._l23_a + that._l12_a);
20 x2 = (x2 * b + that._x1 * that._l23_2a - x * that._l12_2a) / m;
21 y2 = (y2 * b + that._y1 * that._l23_2a - y * that._l12_2a) / m;
22 }
23
24 that._context.bezierCurveTo(x1, y1, x2, y2, that._x2, that._y2);
25}
26
27function CatmullRom(context, alpha) {
28 this._context = context;
29 this._alpha = alpha;
30}
31
32CatmullRom.prototype = {
33 areaStart: function() {
34 this._line = 0;
35 },
36 areaEnd: function() {
37 this._line = NaN;
38 },
39 lineStart: function() {
40 this._x0 = this._x1 = this._x2 =
41 this._y0 = this._y1 = this._y2 = NaN;
42 this._l01_a = this._l12_a = this._l23_a =
43 this._l01_2a = this._l12_2a = this._l23_2a =
44 this._point = 0;
45 },
46 lineEnd: function() {
47 switch (this._point) {
48 case 2: this._context.lineTo(this._x2, this._y2); break;
49 case 3: this.point(this._x2, this._y2); break;
50 }
51 if (this._line || (this._line !== 0 && this._point === 1)) this._context.closePath();
52 this._line = 1 - this._line;
53 },
54 point: function(x, y) {
55 x = +x, y = +y;
56
57 if (this._point) {
58 var x23 = this._x2 - x,
59 y23 = this._y2 - y;
60 this._l23_a = Math.sqrt(this._l23_2a = Math.pow(x23 * x23 + y23 * y23, this._alpha));
61 }
62
63 switch (this._point) {
64 case 0: this._point = 1; this._line ? this._context.lineTo(x, y) : this._context.moveTo(x, y); break;
65 case 1: this._point = 2; break;
66 case 2: this._point = 3; // falls through
67 default: point(this, x, y); break;
68 }
69
70 this._l01_a = this._l12_a, this._l12_a = this._l23_a;
71 this._l01_2a = this._l12_2a, this._l12_2a = this._l23_2a;
72 this._x0 = this._x1, this._x1 = this._x2, this._x2 = x;
73 this._y0 = this._y1, this._y1 = this._y2, this._y2 = y;
74 }
75};
76
77export default (function custom(alpha) {
78
79 function catmullRom(context) {
80 return alpha ? new CatmullRom(context, alpha) : new Cardinal(context, 0);
81 }
82
83 catmullRom.alpha = function(alpha) {
84 return custom(+alpha);
85 };
86
87 return catmullRom;
88})(0.5);
Note: See TracBrowser for help on using the repository browser.