Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .cursorignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# Add directories or file patterns to ignore during indexing (e.g. foo/ or *.csv)
demo
scripts
2 changes: 1 addition & 1 deletion demo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 12 additions & 5 deletions scripts/preview.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ const P = new Path();
P.M(250, 250);
let x = c
x = x.replace(/\s+/g, '');
const cmd = x.split('(');
const name = cmd[0];
const args = cmd[1].split(',');
const numericArgs = args.map(arg => parseFloat(arg));
P[name](...numericArgs);
const commands = x.split(').');
commands.forEach(command => {
if (command) {
const [name, argsStr] = command.split('(');
if (argsStr) {
const args = argsStr.replace(')', '').split(',');
const numericArgs = args.map(arg => parseFloat(arg));
P[name](...numericArgs);
}
}
});



Expand All @@ -58,6 +64,7 @@ const htmlContent = `
background-color: white;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
path { fill: none; }
</style>
</head>
<body>
Expand Down
16 changes: 16 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,25 @@ export default class Path {
* @default []
*/
this.attributes = {};
this.lastCX = 0;
this.lastCY = 0;
return this;
}

nest = (cx, cy) => {
if (cx === undefined) {
cx = this.lastCX;
} else {
this.lastCX = cx;
}
if (cy === undefined) {
cy = this.lastCY;
} else {
this.lastCY = cy;
}
return [cx, cy];
};

/**
* @function angleInRadians
* @memberof Path
Expand Down
1 change: 1 addition & 0 deletions src/macros/cross.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @return {Path} the path object for chaining
*/
const cross = function (width, height, cx, cy, centerEnd = true) {
[cx ,cy] = this.nest(cx, cy);
const l = cx - width / 2;
const r = l + width;
const t = cy - height / 2;
Expand Down
1 change: 1 addition & 0 deletions src/macros/ellipse.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @return {Path} the path object for chaining
*/
const ellipse = function (width, height, cx, cy, centerEnd = true) {
[cx ,cy] = this.nest(cx, cy);
const rx = width / 2;
const ry = height / 2;

Expand Down
1 change: 1 addition & 0 deletions src/macros/isocube.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @return {Path} the path object for chaining
*/
const isocube = function (size, cx, cy, centerEnd = true) {
[cx ,cy] = this.nest(cx, cy);
this.regPolygon(size, 6, cx, cy, centerEnd)

const inner = this.constructor.radialPoints(0 / 2, cx, cy, 6);
Expand Down
1 change: 1 addition & 0 deletions src/macros/kite.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @return {Path} the path object for chaining
*/
const kite = function (width, height, dh, cx, cy, centerEnd = true) {
[cx ,cy] = this.nest(cx, cy);
dh = dh || parseInt(height * 0.33, 10);
const [t, _, b] = this.constructor.radialPoints(height / 2, cx, cy, 4);
const h = parseInt(t[1], 10) + dh;
Expand Down
1 change: 1 addition & 0 deletions src/macros/lens.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @return {Path} the path object for chaining
*/
const lens = function (width, height, cx, cy, centerEnd = true) {
[cx ,cy] = this.nest(cx, cy);
this.M(cx - width / 2, cy)
.Q(cx, cy - height, cx + width / 2, cy)
.Q(cx, cy + height, cx - width / 2, cy);
Expand Down
1 change: 1 addition & 0 deletions src/macros/polygram.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const polygram = function (
vertexSkip = 2,
centerEnd = true,
) {
[cx ,cy] = this.nest(cx, cy);
this.polygon(
this.constructor.radialPoints(size / 2, cx, cy, points, null, vertexSkip),
);
Expand Down
1 change: 1 addition & 0 deletions src/macros/radialLines.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const radialLines = function (
cy,
centerEnd = true,
) {
[cx ,cy] = this.nest(cx, cy);
const inner = this.constructor.radialPoints(innerSize / 2, cx, cy, points);
const outer = this.constructor.radialPoints(outerSize / 2, cx, cy, points);

Expand Down
1 change: 1 addition & 0 deletions src/macros/rect.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @return {Path} the path object for chaining
*/
const rect = function (width, height, cx, cy, centerEnd = true) {
[cx ,cy] = this.nest(cx, cy);
this.M(cx - width / 2, cy - height / 2)
.right(width)
.down(height)
Expand Down
1 change: 1 addition & 0 deletions src/macros/regPolygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @return {Path} the path object for chaining
*/
const regPolygon = function (size, sides, cx, cy, centerEnd = true) {
[cx ,cy] = this.nest(cx, cy);
this.polygon(this.constructor.radialPoints(size / 2, cx, cy, sides));
if (centerEnd) {
this.M(cx, cy);
Expand Down
1 change: 1 addition & 0 deletions src/macros/roundedRect.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @return {Path} the path object for chaining
*/
const roundedRect = function (width, height, radius, cx, cy, centerEnd = true) {
[cx ,cy] = this.nest(cx, cy);
const top = cy - height / 2;
const left = cx - width / 2;
const right = left + width;
Expand Down
1 change: 1 addition & 0 deletions src/macros/roundedSquare.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @return {Path} the path object for chaining
*/
const roundedSquare = function (size, radius, cx, cy, centerEnd = true) {
[cx ,cy] = this.nest(cx, cy);
return this.roundedRect(size, size, radius, cx, cy, centerEnd);
};

Expand Down
1 change: 1 addition & 0 deletions src/macros/sector.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @return {Path} the path object for chaining
*/
const sector = function (cx, cy, size, startAngle, endAngle, centerEnd = true) {
[cx ,cy] = this.nest(cx, cy);
const radius = size / 2;
const start = this.constructor.clockwisePoint(
cx,
Expand Down
1 change: 1 addition & 0 deletions src/macros/segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const segment = function (
endAngle,
centerEnd = true,
) {
[cx ,cy] = this.nest(cx, cy);
const radius = size / 2;
const start = this.constructor.clockwisePoint(
cx,
Expand Down
1 change: 1 addition & 0 deletions src/macros/square.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @return {Path} the path object for chaining
*/
const square = function (size, cx, cy, centerEnd = true) {
[cx ,cy] = this.nest(cx, cy);
return this.rect(size, size, cx, cy, centerEnd);
};

Expand Down
1 change: 1 addition & 0 deletions src/macros/star.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @return {Path} the path object for chaining
*/
const star = function (outerSize, innerSize, points, cx, cy, centerEnd = true) {
[cx ,cy] = this.nest(cx, cy);
const innerRadius = innerSize / 2;
const outerRadius = outerSize / 2;
const increment = 360 / (points * 2);
Expand Down
1 change: 1 addition & 0 deletions src/macros/symH.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @return {Path} the path object for chaining
*/
const symH = function (width, height, cx, cy, centerEnd = true) {
[cx ,cy] = this.nest(cx, cy);
const l = cx - width / 2;
const r = l + width;
const t = cy - height / 2;
Expand Down
1 change: 1 addition & 0 deletions src/macros/symI.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @return {Path} the path object for chaining
*/
const symI = function (width, height, cx, cy, centerEnd = true) {
[cx ,cy] = this.nest(cx, cy);
const l = cx - width / 2;
const r = l + width;
const t = cy - height / 2;
Expand Down
1 change: 1 addition & 0 deletions src/macros/symV.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @return {Path} the path object for chaining
*/
const symV = function (width, height, cx, cy, centerEnd = true) {
[cx ,cy] = this.nest(cx, cy);
const l = cx - width / 2;
const r = l + width;
const t = cy - height / 2;
Expand Down
1 change: 1 addition & 0 deletions src/macros/symX.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @return {Path} the path object for chaining
*/
const symX = function (width, height, cx, cy, centerEnd = true) {
[cx ,cy] = this.nest(cx, cy);
const l = cx - width / 2;
const r = l + width;
const t = cy - height / 2;
Expand Down
1 change: 1 addition & 0 deletions src/macros/triangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @return {Path} the path object for chaining
*/
const triangle = function (size, cx, cy, centerEnd = true) {
[cx ,cy] = this.nest(cx, cy);
const sq3 = Math.sqrt(3);
const a = [cx, cy - (sq3 / 3) * size];
const b = [cx - size / 2, cy + (sq3 / 6) * size];
Expand Down