| 1 | import {CardinalClosed} from "./cardinalClosed.js";
|
|---|
| 2 | import noop from "../noop.js";
|
|---|
| 3 | import {point} from "./catmullRom.js";
|
|---|
| 4 |
|
|---|
| 5 | function CatmullRomClosed(context, alpha) {
|
|---|
| 6 | this._context = context;
|
|---|
| 7 | this._alpha = alpha;
|
|---|
| 8 | }
|
|---|
| 9 |
|
|---|
| 10 | CatmullRomClosed.prototype = {
|
|---|
| 11 | areaStart: noop,
|
|---|
| 12 | areaEnd: noop,
|
|---|
| 13 | lineStart: function() {
|
|---|
| 14 | this._x0 = this._x1 = this._x2 = this._x3 = this._x4 = this._x5 =
|
|---|
| 15 | this._y0 = this._y1 = this._y2 = this._y3 = this._y4 = this._y5 = NaN;
|
|---|
| 16 | this._l01_a = this._l12_a = this._l23_a =
|
|---|
| 17 | this._l01_2a = this._l12_2a = this._l23_2a =
|
|---|
| 18 | this._point = 0;
|
|---|
| 19 | },
|
|---|
| 20 | lineEnd: function() {
|
|---|
| 21 | switch (this._point) {
|
|---|
| 22 | case 1: {
|
|---|
| 23 | this._context.moveTo(this._x3, this._y3);
|
|---|
| 24 | this._context.closePath();
|
|---|
| 25 | break;
|
|---|
| 26 | }
|
|---|
| 27 | case 2: {
|
|---|
| 28 | this._context.lineTo(this._x3, this._y3);
|
|---|
| 29 | this._context.closePath();
|
|---|
| 30 | break;
|
|---|
| 31 | }
|
|---|
| 32 | case 3: {
|
|---|
| 33 | this.point(this._x3, this._y3);
|
|---|
| 34 | this.point(this._x4, this._y4);
|
|---|
| 35 | this.point(this._x5, this._y5);
|
|---|
| 36 | break;
|
|---|
| 37 | }
|
|---|
| 38 | }
|
|---|
| 39 | },
|
|---|
| 40 | point: function(x, y) {
|
|---|
| 41 | x = +x, y = +y;
|
|---|
| 42 |
|
|---|
| 43 | if (this._point) {
|
|---|
| 44 | var x23 = this._x2 - x,
|
|---|
| 45 | y23 = this._y2 - y;
|
|---|
| 46 | this._l23_a = Math.sqrt(this._l23_2a = Math.pow(x23 * x23 + y23 * y23, this._alpha));
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | switch (this._point) {
|
|---|
| 50 | case 0: this._point = 1; this._x3 = x, this._y3 = y; break;
|
|---|
| 51 | case 1: this._point = 2; this._context.moveTo(this._x4 = x, this._y4 = y); break;
|
|---|
| 52 | case 2: this._point = 3; this._x5 = x, this._y5 = y; break;
|
|---|
| 53 | default: point(this, x, y); break;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | this._l01_a = this._l12_a, this._l12_a = this._l23_a;
|
|---|
| 57 | this._l01_2a = this._l12_2a, this._l12_2a = this._l23_2a;
|
|---|
| 58 | this._x0 = this._x1, this._x1 = this._x2, this._x2 = x;
|
|---|
| 59 | this._y0 = this._y1, this._y1 = this._y2, this._y2 = y;
|
|---|
| 60 | }
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 | export default (function custom(alpha) {
|
|---|
| 64 |
|
|---|
| 65 | function catmullRom(context) {
|
|---|
| 66 | return alpha ? new CatmullRomClosed(context, alpha) : new CardinalClosed(context, 0);
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | catmullRom.alpha = function(alpha) {
|
|---|
| 70 | return custom(+alpha);
|
|---|
| 71 | };
|
|---|
| 72 |
|
|---|
| 73 | return catmullRom;
|
|---|
| 74 | })(0.5);
|
|---|