Visualize Your Ideas With Code
feat(2d): add Polygon component (#463)
Andrie de Vries15adb3e312a4
diff --git a/packages/2d/src/components/Polygon.ts b/packages/2d/src/components/Polygon.tsnew file mode 100644index 00000000..d454a039--- /dev/null+++ b/packages/2d/src/components/Polygon.ts@@ -0,0 +1,95 @@+import {BBox} from '@motion-canvas/core/lib/types';+import {Shape, ShapeProps} from './Shape';+import {initial, signal} from '../decorators';+import {SignalValue, SimpleSignal} from '@motion-canvas/core/lib/signals';+import {drawPolygon} from '../utils';++export interface PolygonProps extends ShapeProps {+ /**+ * {@inheritDoc Polygon.sides}+ */+ sides?: SignalValue<number>;+}++/**+ * A node for drawing regular polygons.+ *+ * @remarks+ * This node can be used to render shapes such as: triangle, pentagon,+ * hexagon and more.+ *+ * Note that the polygon is inscribed in a circle defined by the height+ * and width. If height and width are unequal, the polygon is inscribed+ * in the resulting ellipse.+ *+ * Since the polygon is inscribed in the circle, the actual displayed+ * height and width may differ somewhat from the bounding rectangle. This+ * will be particularly noticable if the number of sides is low, e.g. for a+ * triangle.+ *+ * @example+ * A hexagon:+ * ```tsx+ * <Polygon+ * sides={6}+ * size={300}+ * fill={'lightseagreen'}+ * />+ * ```+ * A pentagon outline:+ * ```tsx+ * <Polygon+ * sides={5}+ * size={300}+ * stroke={'lightblue'}+ * lineWidth={8}+ * />+ * ```+ */+export class Polygon extends Shape {+ /**+ * Sets the number of sides of the polygon.+ *+ * @remarks+ * For example, a value of 6 creates a hexagon.+ *+ *+ * @example+ * ```tsx+ * <Polygon+ * size={320}+ * sides={7}+ * stroke={'#fff'}+ * lineWidth={8}+ * fill={'lightseagreen'}+ * />+ * ```+ */+ @initial(6)+ @signal()+ public declare readonly sides: SimpleSignal<number, this>;++ public constructor(props: PolygonProps) {+ super(props);+ }++ protected override getPath(): Path2D {+ const path = new Path2D();+ const sides = this.sides();++ const box = BBox.fromSizeCentered(this.size());+ drawPolygon(path, box, sides);++ return path;+ }+ protected override getRipplePath(): Path2D {+ const path = new Path2D();+ const sides = this.sides();+ const rippleSize = this.rippleSize();++ const box = BBox.fromSizeCentered(this.size()).expand(rippleSize);+ drawPolygon(path, box, sides);++ return path;+ }+}diff --git a/packages/2d/src/components/index.ts b/packages/2d/src/components/index.tsindex 7893e91d..115b45ea 100644--- a/packages/2d/src/components/index.ts+++ b/packages/2d/src/components/index.ts@@ -7,6 +7,7 @@ export * from './Layout';export * from './Line';export * from './Node';export * from './Rect';+export * from './Polygon';export * from './Shape';export * from './Txt';export * from './types';diff --git a/packages/2d/src/utils/CanvasUtils.ts b/packages/2d/src/utils/CanvasUtils.tsindex a8d3f2cb..9c66af9f 100644--- a/packages/2d/src/utils/CanvasUtils.ts+++ b/packages/2d/src/utils/CanvasUtils.ts@@ -162,6 +162,25 @@ export function strokeRect(context: CanvasRenderingContext2D, rect: BBox) {context.strokeRect(rect.x, rect.y, rect.width, rect.height);}+export function drawPolygon(+ path: CanvasRenderingContext2D | Path2D,+ rect: BBox,+ sides: number,+) {+ const size = rect.size.scale(0.5);+ for (let i = 0; i <= sides; i++) {+ const theta = (i * 2 * Math.PI) / sides;+ const direction = Vector2.fromRadians(theta).perpendicular;+ const vertex = direction.mul(size);+ if (i == 0) {+ moveTo(path, vertex);+ } else {+ lineTo(path, vertex);+ }+ }+ path.closePath();+}+export function drawImage(context: CanvasRenderingContext2D,image: CanvasImageSource,