| 1 | export function point(that, x, y) {
|
|---|
| 2 | that._context.bezierCurveTo(
|
|---|
| 3 | that._x1 + that._k * (that._x2 - that._x0),
|
|---|
| 4 | that._y1 + that._k * (that._y2 - that._y0),
|
|---|
| 5 | that._x2 + that._k * (that._x1 - x),
|
|---|
| 6 | that._y2 + that._k * (that._y1 - y),
|
|---|
| 7 | that._x2,
|
|---|
| 8 | that._y2
|
|---|
| 9 | );
|
|---|
| 10 | }
|
|---|
| 11 |
|
|---|
| 12 | export function Cardinal(context, tension) {
|
|---|
| 13 | this._context = context;
|
|---|
| 14 | this._k = (1 - tension) / 6;
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | Cardinal.prototype = {
|
|---|
| 18 | areaStart: function() {
|
|---|
| 19 | this._line = 0;
|
|---|
| 20 | },
|
|---|
| 21 | areaEnd: function() {
|
|---|
| 22 | this._line = NaN;
|
|---|
| 23 | },
|
|---|
| 24 | lineStart: function() {
|
|---|
| 25 | this._x0 = this._x1 = this._x2 =
|
|---|
| 26 | this._y0 = this._y1 = this._y2 = NaN;
|
|---|
| 27 | this._point = 0;
|
|---|
| 28 | },
|
|---|
| 29 | lineEnd: function() {
|
|---|
| 30 | switch (this._point) {
|
|---|
| 31 | case 2: this._context.lineTo(this._x2, this._y2); break;
|
|---|
| 32 | case 3: point(this, this._x1, this._y1); break;
|
|---|
| 33 | }
|
|---|
| 34 | if (this._line || (this._line !== 0 && this._point === 1)) this._context.closePath();
|
|---|
| 35 | this._line = 1 - this._line;
|
|---|
| 36 | },
|
|---|
| 37 | point: function(x, y) {
|
|---|
| 38 | x = +x, y = +y;
|
|---|
| 39 | switch (this._point) {
|
|---|
| 40 | case 0: this._point = 1; this._line ? this._context.lineTo(x, y) : this._context.moveTo(x, y); break;
|
|---|
| 41 | case 1: this._point = 2; this._x1 = x, this._y1 = y; break;
|
|---|
| 42 | case 2: this._point = 3; // falls through
|
|---|
| 43 | default: point(this, x, y); break;
|
|---|
| 44 | }
|
|---|
| 45 | this._x0 = this._x1, this._x1 = this._x2, this._x2 = x;
|
|---|
| 46 | this._y0 = this._y1, this._y1 = this._y2, this._y2 = y;
|
|---|
| 47 | }
|
|---|
| 48 | };
|
|---|
| 49 |
|
|---|
| 50 | export default (function custom(tension) {
|
|---|
| 51 |
|
|---|
| 52 | function cardinal(context) {
|
|---|
| 53 | return new Cardinal(context, tension);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | cardinal.tension = function(tension) {
|
|---|
| 57 | return custom(+tension);
|
|---|
| 58 | };
|
|---|
| 59 |
|
|---|
| 60 | return cardinal;
|
|---|
| 61 | })(0);
|
|---|