Skip to content

Visualize Your Ideas With Code

feat(2d): make Rect extend Curve (#759)

Jacob981021264882
diff --git a/packages/2d/src/components/Curve.ts b/packages/2d/src/components/Curve.ts
index c6170c68..c8d12a2f 100644
--- a/packages/2d/src/components/Curve.ts
+++ b/packages/2d/src/components/Curve.ts
@@ -352,12 +352,6 @@ export abstract class Curve extends Shape {
return getPointAtDistance(this.profile(), this.percentageToDistance(value));
}
- protected override applyStyle(context: CanvasRenderingContext2D) {
- super.applyStyle(context);
- const {arcLength} = this.profile();
- context.lineDashOffset -= arcLength / 2;
- }
-
protected override getComputedLayout(): BBox {
return this.offsetComputedLayout(super.getComputedLayout());
}
@@ -385,8 +379,33 @@ export abstract class Curve extends Shape {
return this.lineCap() === 'square' ? 0.5 * 1.4143 : 0.5;
}
+ /**
+ * Check if the path requires a profile.
+ *
+ * @remarks
+ * The profile is only required if certain features are used. Otherwise, the
+ * profile generation can be skipped, and the curve can be drawn directly
+ * using the 2D context.
+ */
+ protected requiresProfile(): boolean {
+ return (
+ !this.start.isInitial() ||
+ !this.startOffset.isInitial() ||
+ !this.startArrow.isInitial() ||
+ !this.end.isInitial() ||
+ !this.endOffset.isInitial() ||
+ !this.endArrow.isInitial()
+ );
+ }
+
protected override drawShape(context: CanvasRenderingContext2D) {
super.drawShape(context);
+ if (this.startArrow() || this.endArrow()) {
+ this.drawArrows(context);
+ }
+ }
+
+ private drawArrows(context: CanvasRenderingContext2D) {
const {startPoint, startTangent, endPoint, endTangent, arrowSize} =
this.curveDrawingInfo();
if (arrowSize < 0.001) {
diff --git a/packages/2d/src/components/Rect.ts b/packages/2d/src/components/Rect.ts
index c053ef27..2aaf5c2e 100644
--- a/packages/2d/src/components/Rect.ts
+++ b/packages/2d/src/components/Rect.ts
@@ -2,14 +2,17 @@ import {
PossibleSpacing,
BBox,
SpacingSignal,
+ SerializedVector2,
} from '@motion-canvas/core/lib/types';
-import {Shape, ShapeProps} from './Shape';
-import {drawRoundRect} from '../utils';
import {initial, signal} from '../decorators';
import {spacingSignal} from '../decorators/spacingSignal';
import {SignalValue, SimpleSignal} from '@motion-canvas/core/lib/signals';
+import {Curve, CurveProps} from './Curve';
+import {CurveProfile, getRectProfile} from '../curves';
+import {drawRoundRect} from '../utils';
+import {DesiredLength} from '../partials';
-export interface RectProps extends ShapeProps {
+export interface RectProps extends CurveProps {
/**
* {@inheritDoc Rect.radius}
*/
@@ -26,7 +29,7 @@ export interface RectProps extends ShapeProps {
cornerSharpness?: SignalValue<number>;
}
-export class Rect extends Shape {
+export class Rect extends Curve {
/**
* Rounds the corners of this rectangle.
*
@@ -123,7 +126,35 @@ export class Rect extends Shape {
super(props);
}
+ public profile(): CurveProfile {
+ return getRectProfile(
+ this.childrenBBox(),
+ this.radius(),
+ this.smoothCorners(),
+ this.cornerSharpness(),
+ );
+ }
+
+ protected override desiredSize(): SerializedVector2<DesiredLength> {
+ return {
+ x: this.width.context.getter(),
+ y: this.height.context.getter(),
+ };
+ }
+
+ protected override offsetComputedLayout(box: BBox): BBox {
+ return box;
+ }
+
+ protected override childrenBBox(): BBox {
+ return BBox.fromSizeCentered(this.computedSize());
+ }
+
protected override getPath(): Path2D {
+ if (this.requiresProfile()) {
+ return this.curveDrawingInfo().path;
+ }
+
const path = new Path2D();
const radius = this.radius();
const smoothCorners = this.smoothCorners();
diff --git a/packages/2d/src/components/Video.ts b/packages/2d/src/components/Video.ts
index 4417f33b..6a43e07b 100644
--- a/packages/2d/src/components/Video.ts
+++ b/packages/2d/src/components/Video.ts
@@ -131,7 +131,7 @@ export class Video extends Rect {
}
@computed()
- public completion(): number {
+ public override completion(): number {
return this.clampTime(this.time()) / this.video().duration;
}
diff --git a/packages/2d/src/curves/getRectProfile.ts b/packages/2d/src/curves/getRectProfile.ts
new file mode 100644
index 00000000..9ce31f48
--- /dev/null
+++ b/packages/2d/src/curves/getRectProfile.ts
@@ -0,0 +1,139 @@
+import {Vector2} from '@motion-canvas/core/lib/types';
+import {CurveProfile} from './CurveProfile';
+import {LineSegment} from './LineSegment';
+import {CircleSegment} from './CircleSegment';
+import {BBox, Spacing} from '@motion-canvas/core';
+import {adjustRectRadius} from '../utils';
+import {Segment} from './Segment';
+import {CubicBezierSegment} from './CubicBezierSegment';
+
+export function getRectProfile(
+ rect: BBox,
+ radius: Spacing,
+ smoothCorners: boolean,
+ cornerSharpness: number,
+): CurveProfile {
+ const profile: CurveProfile = {
+ arcLength: 0,
+ segments: [],
+ minSin: 1,
+ };
+
+ const topLeft = adjustRectRadius(radius.top, radius.right, radius.left, rect);
+ const topRight = adjustRectRadius(
+ radius.right,
+ radius.top,
+ radius.bottom,
+ rect,
+ );
+ const bottomRight = adjustRectRadius(
+ radius.bottom,
+ radius.left,
+ radius.right,
+ rect,
+ );
+ const bottomLeft = adjustRectRadius(
+ radius.left,
+ radius.bottom,
+ radius.top,
+ rect,
+ );
+
+ let from = new Vector2(rect.left + topLeft, rect.top);
+ let to = new Vector2(rect.right - topRight, rect.top);
+ addSegment(profile, new LineSegment(from, to));
+
+ from = new Vector2(rect.right, rect.top + topRight);
+ to = new Vector2(rect.right, rect.bottom - bottomRight);
+ if (topRight > 0) {
+ addCornerSegment(
+ profile,
+ from.addX(-topRight),
+ topRight,
+ Vector2.down,
+ Vector2.right,
+ smoothCorners,
+ cornerSharpness,
+ );
+ }
+ addSegment(profile, new LineSegment(from, to));
+
+ from = new Vector2(rect.right - bottomRight, rect.bottom);
+ to = new Vector2(rect.left + bottomLeft, rect.bottom);
+ if (bottomRight > 0) {
+ addCornerSegment(
+ profile,
+ from.addY(-bottomRight),
+ bottomRight,
+ Vector2.right,
+ Vector2.up,
+ smoothCorners,
+ cornerSharpness,
+ );
+ }
+ addSegment(profile, new LineSegment(from, to));
+
+ from = new Vector2(rect.left, rect.bottom - bottomLeft);
+ to = new Vector2(rect.left, rect.top + topLeft);
+ if (bottomLeft > 0) {
+ addCornerSegment(
+ profile,
+ from.addX(bottomLeft),
+ bottomLeft,
+ Vector2.up,
+ Vector2.left,
+ smoothCorners,
+ cornerSharpness,
+ );
+ }
+ addSegment(profile, new LineSegment(from, to));
+
+ from = new Vector2(rect.left + topLeft, rect.top);
+ if (topLeft > 0) {
+ addCornerSegment(
+ profile,
+ from.addY(topLeft),
+ topLeft,
+ Vector2.left,
+ Vector2.down,
+ smoothCorners,
+ cornerSharpness,
+ );
+ }
+
+ return profile;
+}
+
+function addSegment(profile: CurveProfile, segment: Segment) {
+ profile.segments.push(segment);
+ profile.arcLength += segment.arcLength;
+}
+
+function addCornerSegment(
+ profile: CurveProfile,
+ center: Vector2,
+ radius: number,
+ fromNormal: Vector2,
+ toNormal: Vector2,
+ smooth: boolean,
+ sharpness: number,
+) {
+ const from = center.add(fromNormal.scale(radius));
+ const to = center.add(toNormal.scale(radius));
+ if (smooth) {
+ addSegment(
+ profile,
+ new CubicBezierSegment(
+ from,
+ from.add(toNormal.scale(sharpness * radius)),
+ to.add(fromNormal.scale(sharpness * radius)),
+ to,
+ ),
+ );
+ } else {
+ addSegment(
+ profile,
+ new CircleSegment(center, radius, fromNormal, toNormal, false),
+ );
+ }
+}
diff --git a/packages/2d/src/curves/index.ts b/packages/2d/src/curves/index.ts
index 29cb1c19..33fc041b 100644
--- a/packages/2d/src/curves/index.ts
+++ b/packages/2d/src/curves/index.ts
@@ -6,6 +6,7 @@ export * from './CurveProfile';
export * from './getBezierSplineProfile';
export * from './getPointAtDistance';
export * from './getPolylineProfile';
+export * from './getRectProfile';
export * from './KnotInfo';
export * from './LineSegment';
export * from './Polynomial';
diff --git a/packages/2d/src/utils/CanvasUtils.ts b/packages/2d/src/utils/CanvasUtils.ts
index fa8ff541..dee63690 100644
--- a/packages/2d/src/utils/CanvasUtils.ts
+++ b/packages/2d/src/utils/CanvasUtils.ts
@@ -129,7 +129,7 @@ export function drawRoundRect(
context.arcTo(rect.left, rect.top, rect.right, rect.top, topLeft);
}
-function adjustRectRadius(
+export function adjustRectRadius(
radius: number,
horizontal: number,
vertical: number,