svg-path-d
Version:
SVG path data (path[d] attribute content) manipulation library.
130 lines • 4.73 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PathBuilder = void 0;
var path_node_1 = require("./path-node");
var in_place_transform_1 = require("./in-place-transform");
var PathBuilder = /** @class */ (function () {
function PathBuilder(path) {
if (path === void 0) { path = []; }
this.path = path;
}
Object.defineProperty(PathBuilder.prototype, "last", {
get: function () {
return this.path[this.path.length - 1];
},
enumerable: false,
configurable: true
});
Object.defineProperty(PathBuilder.prototype, "lastX", {
get: function () {
return path_node_1.getX(this.last);
},
enumerable: false,
configurable: true
});
Object.defineProperty(PathBuilder.prototype, "lastY", {
get: function () {
return path_node_1.getY(this.last);
},
enumerable: false,
configurable: true
});
PathBuilder.prototype.push = function (next) {
next.prev = this.last;
this.path.push(next);
return this;
};
/**
* Adds `MoveTo` command to the path.
* @param x absolute x coordinate to move to.
* @param y absolute y coordinate to move to.
*/
PathBuilder.prototype.M = function (x, y) {
return this.push({ name: 'M', x: x, y: y });
};
/**
* Adds `LineTo` command to the path.
* @param x absolute x coordinate to draw line to.
* @param y absolute y coordinate to draw line to.
*/
PathBuilder.prototype.L = function (x, y) {
return this.push({ name: 'L', x: x, y: y });
};
PathBuilder.prototype.H = function (x) {
return this.push({ name: 'H', x: x });
};
PathBuilder.prototype.V = function (y) {
return this.push({ name: 'V', y: y });
};
PathBuilder.prototype.C = function (x1, y1, x2, y2, x, y) {
return this.push({ name: 'C', x1: x1, y1: y1, x2: x2, y2: y2, x: x, y: y });
};
PathBuilder.prototype.S = function (x2, y2, x, y) {
return this.push({ name: 'S', x2: x2, y2: y2, x: x, y: y });
};
PathBuilder.prototype.Q = function (x1, y1, x, y) {
return this.push({ name: 'Q', x1: x1, y1: y1, x: x, y: y });
};
PathBuilder.prototype.T = function (x, y) {
return this.push({ name: 'T', x: x, y: y });
};
PathBuilder.prototype.A = function (rx, ry, angle, largeArc, sweep, x, y) {
var largeArcFlag = !!largeArc;
var sweepFlag = !!sweep;
return this.push({ name: 'A', rx: rx, ry: ry, angle: angle, largeArcFlag: largeArcFlag, sweepFlag: sweepFlag, x: x, y: y });
};
PathBuilder.prototype.Z = function () {
return this.push({ name: 'Z' });
};
/**
* Adds `MoveTo` command to the path.
* @param dx relative x coordinate to move to.
* @param dy relative y coordinate to move to.
*/
PathBuilder.prototype.m = function (dx, dy) {
return this.M(dx, dy)._relative();
};
/**
* Adds `LineTo` command to the path.
* @param dx relative x coordinate to draw line to.
* @param dy relative y coordinate to draw line to.
*/
PathBuilder.prototype.l = function (dx, dy) {
return this.L(dx, dy)._relative();
};
PathBuilder.prototype.h = function (dx) {
return this.H(dx)._relative();
};
PathBuilder.prototype.v = function (dy) {
return this.V(dy)._relative();
};
PathBuilder.prototype.c = function (dx1, dy1, dx2, dy2, dx, dy) {
return this.C(dx1, dy1, dx2, dy2, dx, dy)._relative();
};
PathBuilder.prototype.s = function (dx2, dy2, dx, dy) {
return this.S(dx2, dy2, dx, dy)._relative();
};
PathBuilder.prototype.q = function (dx1, dy1, dx, dy) {
return this.Q(dx1, dy1, dx, dy)._relative();
};
PathBuilder.prototype.t = function (dx, dy) {
return this.T(dx, dy)._relative();
};
PathBuilder.prototype.a = function (rx, ry, angle, largeArc, sweep, dx, dy) {
return this.A(rx, ry, angle, largeArc, sweep, dx, dy)._relative();
};
PathBuilder.prototype.z = function () {
return this.Z();
};
PathBuilder.prototype._relative = function () {
var last = this.last;
if (last) {
var prev = last.prev;
in_place_transform_1.applyTranslate(last, path_node_1.getX(prev), path_node_1.getY(prev));
}
return this;
};
return PathBuilder;
}());
exports.PathBuilder = PathBuilder;
//# sourceMappingURL=path-builder.js.map