Visualize Your Ideas With Code
feat(2d): make Circle extend Curve (#771)
Jacob4c8cf1954093
diff --git a/packages/2d/src/components/Circle.ts b/packages/2d/src/components/Circle.tsindex b1b91f87..e506c3c6 100644--- a/packages/2d/src/components/Circle.ts+++ b/packages/2d/src/components/Circle.ts@@ -1,7 +1,12 @@-import {Shape, ShapeProps} from './Shape';+import {ShapeProps} from './Shape';import {SignalValue, SimpleSignal} from '@motion-canvas/core/lib/signals';-import {initial, signal} from '../decorators';+import {computed, initial, signal} from '../decorators';import {DEG2RAD} from '@motion-canvas/core/lib/utils';+import {Curve} from './Curve';+import {BBox} from '@motion-canvas/core';+import {CurveProfile, getCircleProfile} from '../curves';+import {SerializedVector2} from '@motion-canvas/core/lib/types';+import {DesiredLength} from '../partials';export interface CircleProps extends ShapeProps {/**@@ -12,6 +17,10 @@ export interface CircleProps extends ShapeProps {* {@inheritDoc Circle.endAngle}*/endAngle?: SignalValue<number>;+ /**+ * {@inheritDoc Circle.counterclockwise}+ */+ counterclockwise?: SignalValue<boolean>;/*** {@inheritDoc Circle.closed}*/@@ -91,9 +100,31 @@ export interface CircleProps extends ShapeProps {** yield* ref().startAngle(-270, 2).to(-90, 2);* });+ *+ * // snippet Curve properties:+ * import {makeScene2D, Circle} from '@motion-canvas/2d';+ * import {all, createRef, easeInCubic, easeOutCubic} from '@motion-canvas/core';+ *+ * export default makeScene2D(function* (view) {+ * const ref = createRef<Circle>();+ * view.add(+ * <Circle+ * ref={ref}+ * size={160}+ * stroke={'lightseagreen'}+ * lineWidth={8}+ * endAngle={270}+ * endArrow+ * />,+ * );+ *+ * yield* all(ref().start(1, 1), ref().rotation(180, 1, easeInCubic));+ * ref().start(0).end(0);+ * yield* all(ref().end(1, 1), ref().rotation(360, 1, easeOutCubic));+ * });* ```*/-export class Circle extends Shape {+export class Circle extends Curve {/*** The starting angle in degrees for the circle sector.*@@ -120,6 +151,18 @@ export class Circle extends Shape {@signal()public declare readonly endAngle: SimpleSignal<number, this>;+ /**+ * Whether the circle sector should be drawn counterclockwise.+ *+ * @remarks+ * By default, the circle begins at {@link startAngle} and is drawn clockwise+ * until reaching {@link endAngle}. Setting this property to true will reverse+ * this direction.+ */+ @initial(false)+ @signal()+ public declare readonly counterclockwise: SimpleSignal<boolean, this>;+/*** Whether the path of this circle should be closed.*@@ -150,15 +193,43 @@ export class Circle extends Shape {** @defaultValue false*/- @initial(false)- @signal()public declare readonly closed: SimpleSignal<boolean, this>;public constructor(props: CircleProps) {super(props);}+ @computed()+ public profile(): CurveProfile {+ return getCircleProfile(+ this.size().scale(0.5),+ this.startAngle() * DEG2RAD,+ this.endAngle() * DEG2RAD,+ this.closed(),+ this.counterclockwise(),+ );+ }++ 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;+ }+return this.createPath();}@@ -166,16 +237,26 @@ export class Circle extends Shape {return this.createPath(this.rippleSize());}+ protected override getCacheBBox(): BBox {+ return super.getCacheBBox().expand(this.rippleSize());+ }+protected createPath(expand = 0) {const path = new Path2D();const start = this.startAngle() * DEG2RAD;- const end = this.endAngle() * DEG2RAD;- const size = this.size().scale(0.5);+ let end = this.endAngle() * DEG2RAD;+ const size = this.size().scale(0.5).add(expand);const closed = this.closed();++ if (end > start + Math.PI * 2) {+ const loops = Math.floor((end - start) / (Math.PI * 2));+ end -= Math.PI * 2 * loops;+ }+if (closed) {path.moveTo(0, 0);}- path.ellipse(0, 0, size.x + expand, size.y + expand, 0, start, end);+ path.ellipse(0, 0, size.x, size.y, 0, start, end, this.counterclockwise());if (closed) {path.closePath();}diff --git a/packages/2d/src/components/Curve.ts b/packages/2d/src/components/Curve.tsindex c8d12a2f..297e2013 100644--- a/packages/2d/src/components/Curve.ts+++ b/packages/2d/src/components/Curve.ts@@ -327,7 +327,13 @@ export abstract class Curve extends Shape {}}- if (this.end() === 1 && this.closed()) {+ if (+ this.closed() &&+ this.start.isInitial() &&+ this.end.isInitial() &&+ this.startOffset.isInitial() &&+ this.endOffset.isInitial()+ ) {subpath.closePath();}this.processSubpath(subpath, startPoint, endPoint);diff --git a/packages/2d/src/components/Rect.ts b/packages/2d/src/components/Rect.tsindex 2aaf5c2e..7f41f814 100644--- a/packages/2d/src/components/Rect.ts+++ b/packages/2d/src/components/Rect.ts@@ -4,7 +4,7 @@ import {SpacingSignal,SerializedVector2,} from '@motion-canvas/core/lib/types';-import {initial, signal} from '../decorators';+import {computed, initial, signal} from '../decorators';import {spacingSignal} from '../decorators/spacingSignal';import {SignalValue, SimpleSignal} from '@motion-canvas/core/lib/signals';import {Curve, CurveProps} from './Curve';@@ -126,6 +126,7 @@ export class Rect extends Curve {super(props);}+ @computed()public profile(): CurveProfile {return getRectProfile(this.childrenBBox(),diff --git a/packages/2d/src/curves/getCircleProfile.ts b/packages/2d/src/curves/getCircleProfile.tsnew file mode 100644index 00000000..634b8135--- /dev/null+++ b/packages/2d/src/curves/getCircleProfile.ts@@ -0,0 +1,86 @@+import {Vector2} from '@motion-canvas/core';+import {CurveProfile} from './CurveProfile';+import {ArcSegment} from './ArcSegment';+import {Segment} from './Segment';+import {LineSegment} from './LineSegment';++export function getCircleProfile(+ size: Vector2,+ startAngle: number,+ endAngle: number,+ closed: boolean,+ counterclockwise = false,+): CurveProfile {+ const profile: CurveProfile = {+ arcLength: 0,+ minSin: 1,+ segments: [],+ };++ if (endAngle < startAngle) {+ const loops = Math.floor((startAngle - endAngle) / (Math.PI * 2)) + 1;+ endAngle += Math.PI * 2 * loops;+ } else if (endAngle > startAngle + Math.PI * 2) {+ const loops = Math.floor((endAngle - startAngle) / (Math.PI * 2));+ endAngle -= Math.PI * 2 * loops;+ }++ const middleAngle = (startAngle + endAngle) / 2;+ const from = size.mul(Vector2.fromRadians(startAngle));+ const to = size.mul(Vector2.fromRadians(endAngle));+ const middle = size+ .mul(Vector2.fromRadians(middleAngle))+ .scale(counterclockwise ? -1 : 1);++ if (closed) {+ addSegment(profile, new LineSegment(Vector2.zero, from));+ }++ addArcSegment(+ profile,+ size,+ from,+ middle,+ startAngle,+ middleAngle,+ counterclockwise,+ );+ addArcSegment(+ profile,+ size,+ middle,+ to,+ middleAngle,+ endAngle,+ counterclockwise,+ );++ if (closed) {+ addSegment(profile, new LineSegment(to, Vector2.zero));+ }++ return profile;+}++function addSegment(profile: CurveProfile, segment: Segment) {+ profile.segments.push(segment);+ profile.arcLength += segment.arcLength;+}++function addArcSegment(+ profile: CurveProfile,+ size: Vector2,+ from: Vector2,+ to: Vector2,+ fromAngle: number,+ toAngle: number,+ counterclockwise: boolean,+) {+ const small = Math.abs(fromAngle - toAngle) <= 180 ? 1 : 0;+ const flip = fromAngle > toAngle ? 0 : 1;+ const counter = counterclockwise ? 0 : 1;+ addSegment(+ profile,+ new ArcSegment(from, size, 0, 0, small ^ counter ^ flip, to),+ );+}diff --git a/packages/2d/src/curves/index.ts b/packages/2d/src/curves/index.tsindex 33fc041b..a194f3f5 100644--- a/packages/2d/src/curves/index.ts+++ b/packages/2d/src/curves/index.ts@@ -4,6 +4,7 @@ export * from './CurveDrawingInfo';export * from './CurvePoint';export * from './CurveProfile';export * from './getBezierSplineProfile';+export * from './getCircleProfile';export * from './getPointAtDistance';export * from './getPolylineProfile';export * from './getRectProfile';