UNPKG

3.23 kBJavaScriptView Raw
1import { __extends } from "tslib";
2import Path from '../Path.js';
3import * as vec2 from '../../core/vector.js';
4import { quadraticSubdivide, cubicSubdivide, quadraticAt, cubicAt, quadraticDerivativeAt, cubicDerivativeAt } from '../../core/curve.js';
5var out = [];
6var BezierCurveShape = (function () {
7 function BezierCurveShape() {
8 this.x1 = 0;
9 this.y1 = 0;
10 this.x2 = 0;
11 this.y2 = 0;
12 this.cpx1 = 0;
13 this.cpy1 = 0;
14 this.percent = 1;
15 }
16 return BezierCurveShape;
17}());
18export { BezierCurveShape };
19function someVectorAt(shape, t, isTangent) {
20 var cpx2 = shape.cpx2;
21 var cpy2 = shape.cpy2;
22 if (cpx2 != null || cpy2 != null) {
23 return [
24 (isTangent ? cubicDerivativeAt : cubicAt)(shape.x1, shape.cpx1, shape.cpx2, shape.x2, t),
25 (isTangent ? cubicDerivativeAt : cubicAt)(shape.y1, shape.cpy1, shape.cpy2, shape.y2, t)
26 ];
27 }
28 else {
29 return [
30 (isTangent ? quadraticDerivativeAt : quadraticAt)(shape.x1, shape.cpx1, shape.x2, t),
31 (isTangent ? quadraticDerivativeAt : quadraticAt)(shape.y1, shape.cpy1, shape.y2, t)
32 ];
33 }
34}
35var BezierCurve = (function (_super) {
36 __extends(BezierCurve, _super);
37 function BezierCurve(opts) {
38 return _super.call(this, opts) || this;
39 }
40 BezierCurve.prototype.getDefaultStyle = function () {
41 return {
42 stroke: '#000',
43 fill: null
44 };
45 };
46 BezierCurve.prototype.getDefaultShape = function () {
47 return new BezierCurveShape();
48 };
49 BezierCurve.prototype.buildPath = function (ctx, shape) {
50 var x1 = shape.x1;
51 var y1 = shape.y1;
52 var x2 = shape.x2;
53 var y2 = shape.y2;
54 var cpx1 = shape.cpx1;
55 var cpy1 = shape.cpy1;
56 var cpx2 = shape.cpx2;
57 var cpy2 = shape.cpy2;
58 var percent = shape.percent;
59 if (percent === 0) {
60 return;
61 }
62 ctx.moveTo(x1, y1);
63 if (cpx2 == null || cpy2 == null) {
64 if (percent < 1) {
65 quadraticSubdivide(x1, cpx1, x2, percent, out);
66 cpx1 = out[1];
67 x2 = out[2];
68 quadraticSubdivide(y1, cpy1, y2, percent, out);
69 cpy1 = out[1];
70 y2 = out[2];
71 }
72 ctx.quadraticCurveTo(cpx1, cpy1, x2, y2);
73 }
74 else {
75 if (percent < 1) {
76 cubicSubdivide(x1, cpx1, cpx2, x2, percent, out);
77 cpx1 = out[1];
78 cpx2 = out[2];
79 x2 = out[3];
80 cubicSubdivide(y1, cpy1, cpy2, y2, percent, out);
81 cpy1 = out[1];
82 cpy2 = out[2];
83 y2 = out[3];
84 }
85 ctx.bezierCurveTo(cpx1, cpy1, cpx2, cpy2, x2, y2);
86 }
87 };
88 BezierCurve.prototype.pointAt = function (t) {
89 return someVectorAt(this.shape, t, false);
90 };
91 BezierCurve.prototype.tangentAt = function (t) {
92 var p = someVectorAt(this.shape, t, true);
93 return vec2.normalize(p, p);
94 };
95 return BezierCurve;
96}(Path));
97;
98BezierCurve.prototype.type = 'bezier-curve';
99export default BezierCurve;