How to Add Support for Custom Shapes or Extend Existing Element Types in Excalidraw

To add custom shapes in Excalidraw, extend the ExcalidrawElement union type in packages/element/src/types.ts, register the shape in the ElementShapes map, implement a constructor in newElement.ts, and add a rendering case in shape.ts to generate the RoughJS drawable.

Excalidraw treats every drawable object as an element described by the TypeScript type ExcalidrawElement. Adding a new shape—or extending an existing one—requires modifying the type system and the rendering pipeline that converts an element into a RoughJS or Canvas shape. This guide walks through the exact files and functions you need to touch in the excalidraw/excalidraw repository.

Understanding Excalidraw's Element Architecture

Every element inherits from _ExcalidrawElementBase, which defines common properties like id, x, y, width, height, and strokeStyle. The union type ExcalidrawElement (defined in packages/element/src/types.ts lines 106-116) acts as the single source of truth for all legal element types. When you add a custom shape, you create a new interface that extends the base and append it to this union.

The rendering pipeline relies on ShapeCache.generateElementShape, which internally calls _generateElementShape in packages/element/src/shape.ts. This function switches on element.type and returns a RoughJS Drawable (or Drawable[]). The ElementShapes map in packages/excalidraw/scene/types.ts (lines 62-75) provides the type contract that links each element type to its corresponding shape output.

Step-by-Step Guide to Adding a Custom Shape

Step 1: Define the New Element Type

Create a new interface in packages/element/src/types.ts that extends _ExcalidrawElementBase and includes any custom properties your shape requires.

export type ExcalidrawStarElement = _ExcalidrawElementBase & {
  type: "star";
  /** Number of points – default 5 */
  pointsCount?: number;
};

Step 2: Extend the ExcalidrawElement Union

Append your new type to the ExcalidrawElement union in the same file.

export type ExcalidrawElement =
  | ExcalidrawGenericElement
  | ExcalidrawTextElement
  | ExcalidrawLinearElement
  | ExcalidrawArrowElement
  | ExcalidrawFreeDrawElement
  | ExcalidrawImageElement
  | ExcalidrawFrameElement
  | ExcalidrawMagicFrameElement
  | ExcalidrawIframeElement
  | ExcalidrawEmbeddableElement
  | ExcalidrawStarElement;   // ← NEW

Step 3: Register the Shape in ElementShapes

Update the ElementShapes map in packages/excalidraw/scene/types.ts to declare what your element returns.

export type ElementShapes = {
  rectangle: Drawable;
  ellipse: Drawable;
  diamond: Drawable;
  iframe: Drawable;
  embeddable: Drawable;
  freedraw: (Drawable | SVGPathString)[];
  arrow: Drawable[];
  line: Drawable[];
  text: null;
  image: null;
  frame: null;
  magicframe: null;
  star: Drawable;           // ← NEW
};

Step 4: Create a Constructor Function

Add a factory function in packages/element/src/newElement.ts to generate default instances.

export const newStarElement = (
  opts: ElementConstructorOpts & { pointsCount?: number },
): NonDeleted<ExcalidrawStarElement> => {
  return _newElementBase<ExcalidrawStarElement>("star", {
    width: 100,
    height: 100,
    pointsCount: opts.pointsCount ?? 5,
    ...opts,
  });
};

Step 5: Implement Shape Generation

Insert a new case in _generateElementShape within packages/element/src/shape.ts.

case "star": {
  const points = getStarPoints(element.width, element.height, element.pointsCount ?? 5);
  // RoughJS polygon works well for a star.
  shape = generator.polygon(points, generateRoughOptions(element, false, isDarkMode));
  break;
}

Add the geometry helper in the same file:

function getStarPoints(w: number, h: number, spikes: number) {
  const cx = w / 2;
  const cy = h / 2;
  const outerRadius = Math.min(w, h) / 2;
  const innerRadius = outerRadius / 2;
  const step = Math.PI / spikes;
  const points: [number, number][] = [];

  for (let i = 0; i < 2 * spikes; i++) {
    const radius = i % 2 === 0 ? outerRadius : innerRadius;
    const angle = i * step - Math.PI / 2;
    points.push([cx + Math.cos(angle) * radius, cy + Math.sin(angle) * radius]);
  }
  return points;
}

Step 6: Add UI Controls (Optional)

To expose the shape in the toolbar, add a button component in packages/excalidraw/src/components/toolbar/ToolButton.tsx or a similar location:

<ToolButton
  name="star"
  icon={<StarIcon />}                 // any SVG you like
  onClick={() => {
    const star = newStarElement({ x: 10, y: 10 });
    onInsertElement?.(star);
  }}
/>

Step 7: Write Unit Tests

Create a test file in packages/element/tests/star.test.tsx to verify creation and rendering:

import { newStarElement } from "@excalidraw/element";
import { ShapeCache } from "@excalidraw/element";

test("star shape renders", () => {
  const star = newStarElement({ x: 0, y: 0, width: 100, height: 100 });
  const shape = ShapeCache.generateElementShape(star, null);
  expect(shape).toBeDefined();
});

Key Files and Their Roles

File Purpose
packages/element/src/types.ts Defines _ExcalidrawElementBase and the ExcalidrawElement union.
packages/excalidraw/scene/types.ts Contains the ElementShapes map that links types to RoughJS drawables.
packages/element/src/newElement.ts Factory functions like newStarElement that initialize element defaults.
packages/element/src/shape.ts Houses _generateElementShape and ShapeCache for converting elements to RoughJS shapes.
packages/element/src/renderElement.ts Generic renderer that pulls from ShapeCache and draws to canvas.
packages/excalidraw/src/components/toolbar/ UI components for toolbar buttons that trigger element creation.
packages/excalidraw/scene/serialization.ts Handles serializeElements; works automatically for new union members.

Summary

  • Define the type by extending _ExcalidrawElementBase and adding your interface to the ExcalidrawElement union in packages/element/src/types.ts.
  • Register the shape in the ElementShapes map inside packages/excalidraw/scene/types.ts to declare the RoughJS drawable type.
  • Create a constructor in packages/element/src/newElement.ts to generate default instances with sensible initial values.
  • Generate the geometry by adding a case to _generateElementShape in packages/element/src/shape.ts, using RoughJS helpers or custom vertex math.
  • Expose UI controls (optional) by adding toolbar buttons in packages/excalidraw/src/components/toolbar/ that invoke your constructor.
  • Test your implementation by adding unit tests in packages/element/tests/ to verify creation, rendering, and hit-testing.

Frequently Asked Questions

Do I need to modify the rendering engine to add a custom shape?

No. The generic renderer in packages/element/src/renderElement.ts automatically pulls the generated shape from ShapeCache and draws it using RoughJS. As long as you register your shape in ElementShapes and implement the generation logic in shape.ts, the existing rendering pipeline handles the rest without modification.

Can I reuse existing geometry utilities for my custom shape?

Yes. The codebase provides helpers like getPolygonShape, getEllipseShape, and getDiamondShape in packages/element/src/shape.ts. You can import and reuse these utilities in your _generateElementShape case, or write custom vertex math (like the getStarPoints helper in the example above) for specialized geometry.

How do I handle serialization for custom elements?

Serialization happens automatically via serializeElements in packages/excalidraw/scene/serialization.ts. Because the function operates on the ExcalidrawElement union, any new type you add to that union (and any custom properties you define in your interface) will be serialized and deserialized without additional code, provided the properties are JSON-serializable.

Where should I add tests for my new element type?

Add unit tests in packages/element/tests/, following the naming convention *.test.tsx (for example, star.test.tsx). Your tests should cover the constructor function (verifying default values), the shape generator (ensuring ShapeCache.generateElementShape returns a valid RoughJS drawable), and hit-testing logic if you implemented custom collision detection.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →