Skip to content

Visualize Your Ideas With Code

feat(2d): add completion property for curves (#635)

Jacob6577d6ddfaf7
diff --git a/packages/2d/src/components/Curve.ts b/packages/2d/src/components/Curve.ts
index 3c2e7405..0e98ff7e 100644
--- a/packages/2d/src/components/Curve.ts
+++ b/packages/2d/src/components/Curve.ts
@@ -177,7 +177,11 @@ export abstract class Curve extends Shape {
* @param value - The percentage along the curve.
*/
public percentageToDistance(value: number): number {
- return this.startOffset() + this.offsetArcLength() * value;
+ return clamp(
+ 0,
+ this.baseArcLength(),
+ this.startOffset() + this.offsetArcLength() * value,
+ );
}
/**
@@ -214,7 +218,8 @@ export abstract class Curve extends Shape {
public offsetArcLength() {
const startOffset = this.startOffset();
const endOffset = this.endOffset();
- return this.baseArcLength() - startOffset - endOffset;
+ const baseLength = this.baseArcLength();
+ return clamp(0, baseLength, baseLength - startOffset - endOffset);
}
/**
@@ -229,6 +234,17 @@ export abstract class Curve extends Shape {
return this.offsetArcLength() * Math.abs(this.start() - this.end());
}
+ /**
+ * The percentage of the curve that's currently visible.
+ *
+ * @remarks
+ * The returned value is the ratio between the visible length (as defined by
+ * {@link start} and {@link end}) and the offset length of the curve.
+ */
+ public completion(): number {
+ return Math.abs(this.start() - this.end());
+ }
+
@computed()
protected curveDrawingInfo(): CurveDrawingInfo {
const path = new Path2D();