BaseBoxShapeUtil
See source codeTable of contents
- migrations
- props
- type
- editor
- options
- Properties
- Methods
- configure
- canBeLaidOut
- canBind
- canCrop
- canCull
- canEdit
- canEditInReadonly
- canReceiveNewChildrenOfType
- canResize
- canResizeChildren
- canScroll
- canSnap
- canTabTo
- component
- getAriaDescriptor
- getBoundsSnapGeometry
- getCanvasSvgDefs
- getClipPath
- getDefaultProps
- getFontFaces
- getGeometry
- getHandles
- getHandleSnapGeometry
- getInterpolatedProps
- getText
- hideInMinimap
- hideResizeHandles
- hideRotateHandle
- hideSelectionBoundsBg
- hideSelectionBoundsFg
- indicator
- isAspectRatioLocked
- isExportBoundsContainer
- onBeforeCreate
- onBeforeUpdate
- onChildrenChange
- onClick
- onCrop
- onDoubleClick
- onDoubleClickCorner
- onDoubleClickEdge
- onDoubleClickHandle
- onDragShapesIn
- onDragShapesOut
- onDragShapesOver
- onDropShapesOver
- onEditEnd
- onEditStart
- onHandleDrag
- onHandleDragCancel
- onHandleDragEnd
- onHandleDragStart
- onResize
- onResizeCancel
- onResizeEnd
- onResizeStart
- onRotate
- onRotateCancel
- onRotateEnd
- onRotateStart
- onTranslate
- onTranslateCancel
- onTranslateEnd
- onTranslateStart
- shouldClipChild
- toBackgroundSvg
- toSvg
Extends ShapeUtil<Shape>.
abstract class BaseBoxShapeUtil<
Shape extends TLBaseBoxShape,
> extends ShapeUtil<Shape> {}Constructor
Constructs a new instance of the ShapeUtil class
Parameters
| Name | Description |
|---|---|
|
Properties
migrations
Migrations allow you to make changes to a shape's props over time. Read the shape prop migrations guide for more information.
static migrations?: LegacyMigrations | MigrationSequence | TLPropsMigrationsprops
Props allow you to define the shape's properties in a way that the editor can understand. This has two main uses:
- Validation. Shapes will be validated using these props to stop bad data from being saved.
- Styles. Each StyleProp in the props can be set on many shapes at once, and will be remembered from one shape to the next.
static props?: RecordProps<TLUnknownShape>Example
import {
DefaultColorStyle,
ShapeUtil,
T,
TLBaseShape,
TLDefaultColorStyle,
} from 'tldraw'
type MyShape = TLBaseShape<
'mine',
{
color: TLDefaultColorStyle
text: string
}
>
class MyShapeUtil extends ShapeUtil<MyShape> {
static props = {
// we use tldraw's built-in color style:
color: DefaultColorStyle,
// validate that the text prop is a string:
text: T.string,
}
}type
The type of the shape util, which should match the shape's type.
static type: stringeditor
editor: Editoroptions
Options for this shape util. If you're implementing a custom shape util, you can override this to provide customization options for your shape. If using an existing shape util, you can customizing this by calling ShapeUtil.configure.
options: {}Methods
configure( )
Configure this shape utils options.
static configure<T extends TLShapeUtilConstructor<any, any>>(
this: T,
options: T extends new (...args: any[]) => {
options: infer Options
}
? Partial<Options>
: never
): TParameters
| Name | Description |
|---|---|
| |
| |
Returns
TcanBeLaidOut( )
Whether the shape can participate in layout functions such as alignment or distribution.
canBeLaidOut(_shape: Shape, _info: TLShapeUtilCanBeLaidOutOpts): booleanParameters
| Name | Description |
|---|---|
| |
|
Returns
booleancanBind( )
Whether the shape can be bound to. See TLShapeUtilCanBindOpts for details.
canBind(_opts: TLShapeUtilCanBindOpts): booleanParameters
| Name | Description |
|---|---|
|
Returns
booleancanCrop( )
Whether the shape can be cropped.
canCrop(_shape: Shape): booleanParameters
| Name | Description |
|---|---|
| |
Returns
booleancanCull( )
Whether this shape can be culled. By default, shapes are culled for
performance reasons when they are outside of the viewport. Culled shapes are still rendered
to the DOM, but have their display property set to none.
canCull(_shape: Shape): booleanParameters
| Name | Description |
|---|---|
| |
Returns
booleancanEdit( )
Whether the shape can be double clicked to edit.
canEdit(_shape: Shape): booleanParameters
| Name | Description |
|---|---|
| |
Returns
booleancanEditInReadonly( )
Whether the shape can be edited in read-only mode.
canEditInReadonly(_shape: Shape): booleanParameters
| Name | Description |
|---|---|
| |
Returns
booleancanReceiveNewChildrenOfType( )
Get whether the shape can receive children of a given type.
canReceiveNewChildrenOfType(_shape: Shape, _type: TLShape['type']): booleanParameters
| Name | Description |
|---|---|
| |
| |
Returns
booleancanResize( )
Whether the shape can be resized.
canResize(_shape: Shape): booleanParameters
| Name | Description |
|---|---|
| |
Returns
booleancanResizeChildren( )
When the shape is resized, whether the shape's children should also be resized.
canResizeChildren(_shape: Shape): booleanParameters
| Name | Description |
|---|---|
| |
Returns
booleancanScroll( )
Whether the shape can be scrolled while editing.
canScroll(_shape: Shape): booleanParameters
| Name | Description |
|---|---|
| |
Returns
booleancanSnap( )
Whether the shape can be snapped to by another shape.
canSnap(_shape: Shape): booleanParameters
| Name | Description |
|---|---|
| |
Returns
booleancanTabTo( )
Whether the shape can be tabbed to.
canTabTo(_shape: Shape): booleanParameters
| Name | Description |
|---|---|
| |
Returns
booleancomponent( )
Get a JSX element for the shape (as an HTML element).
abstract component(shape: Shape): anyParameters
| Name | Description |
|---|---|
| The shape. |
Returns
anygetAriaDescriptor( )
getAriaDescriptor(_shape: Shape): string | undefinedParameters
| Name | Description |
|---|---|
| |
Returns
string | undefinedgetBoundsSnapGeometry( )
Get the geometry to use when snapping to this this shape in translate/resize operations. See BoundsSnapGeometry for details.
getBoundsSnapGeometry(_shape: Shape): BoundsSnapGeometryParameters
| Name | Description |
|---|---|
| |
Returns
getCanvasSvgDefs( )
Return elements to be added to the <defs> section of the canvases SVG context. This can be
used to define SVG content (e.g. patterns & masks) that can be referred to by ID from svg
elements returned by component.
Each def should have a unique key. If multiple defs from different shapes all have the same
key, only one will be used.
getCanvasSvgDefs(): TLShapeUtilCanvasSvgDef[]getClipPath( )
Get the clip path to apply to this shape's children.
getClipPath?(shape: Shape): undefined | Vec[]Parameters
| Name | Description |
|---|---|
| The shape to get the clip path for |
Returns
undefined | Vec[]Array of points defining the clipping polygon in local coordinates, or undefined if no clipping
getDefaultProps( )
Get the default props for a shape.
abstract getDefaultProps(): Shape['props']getFontFaces( )
Get the font faces that should be rendered in the document in order for this shape to render correctly.
getFontFaces(shape: Shape): TLFontFace[]Parameters
| Name | Description |
|---|---|
| The shape. |
Returns
getGeometry( )
getGeometry(shape: Shape): Geometry2dParameters
| Name | Description |
|---|---|
| |
Returns
getHandles( )
Get an array of handle models for the shape. This is an optional method.
getHandles?(shape: Shape): TLHandle[]Example
util.getHandles?.(myShape)Parameters
| Name | Description |
|---|---|
| The shape. |
Returns
TLHandle[]getHandleSnapGeometry( )
getHandleSnapGeometry(shape: Shape): HandleSnapGeometryParameters
| Name | Description |
|---|---|
| |
Returns
getInterpolatedProps( )
getInterpolatedProps(
startShape: Shape,
endShape: Shape,
t: number
): Shape['props']Parameters
| Name | Description |
|---|---|
| |
| |
| |
Returns
Shape['props']getText( )
getText(_shape: Shape): string | undefinedParameters
| Name | Description |
|---|---|
| |
Returns
string | undefinedhideInMinimap( )
Whether a specific shape should hide in the minimap.
If not defined, the default behavior is to show all shapes in the minimap.
hideInMinimap?(_shape: Shape): booleanParameters
| Name | Description |
|---|---|
| |
Returns
booleanboolean indicating if this shape should hide in the minimap
hideResizeHandles( )
Whether the shape should hide its resize handles when selected.
hideResizeHandles(_shape: Shape): booleanParameters
| Name | Description |
|---|---|
| |
Returns
booleanhideRotateHandle( )
Whether the shape should hide its rotation handles when selected.
hideRotateHandle(_shape: Shape): booleanParameters
| Name | Description |
|---|---|
| |
Returns
booleanhideSelectionBoundsBg( )
Whether the shape should hide its selection bounds background when selected.
hideSelectionBoundsBg(_shape: Shape): booleanParameters
| Name | Description |
|---|---|
| |
Returns
booleanhideSelectionBoundsFg( )
Whether the shape should hide its selection bounds foreground when selected.
hideSelectionBoundsFg(_shape: Shape): booleanParameters
| Name | Description |
|---|---|
| |
Returns
booleanindicator( )
Get JSX describing the shape's indicator (as an SVG element).
abstract indicator(shape: Shape): anyParameters
| Name | Description |
|---|---|
| The shape. |
Returns
anyisAspectRatioLocked( )
Whether the shape's aspect ratio is locked.
isAspectRatioLocked(_shape: Shape): booleanParameters
| Name | Description |
|---|---|
| |
Returns
booleanisExportBoundsContainer( )
By default, the bounds of an image export are the bounds of all the shapes it contains, plus
some padding. If an export includes a shape where isExportBoundsContainer is true, then the
padding is skipped if the bounds of that shape contains all the other shapes. This is
useful in cases like annotating on top of an image, where you usually want to avoid extra
padding around the image if you don't need it.
isExportBoundsContainer(_shape: Shape): booleanParameters
| Name | Description |
|---|---|
| The shape to check |
Returns
booleanTrue if this shape should be treated as an export bounds container
onBeforeCreate( )
A callback called just before a shape is created. This method provides a last chance to modify the created shape.
onBeforeCreate?(next: Shape): Shape | voidExample
onBeforeCreate = (next) => {
return { ...next, x: next.x + 1 }
}Parameters
| Name | Description |
|---|---|
| The next shape. |
Returns
Shape | voidThe next shape or void.
onBeforeUpdate( )
A callback called just before a shape is updated. This method provides a last chance to modify the updated shape.
onBeforeUpdate?(prev: Shape, next: Shape): Shape | voidExample
onBeforeUpdate = (prev, next) => {
if (prev.x === next.x) {
return { ...next, x: next.x + 1 }
}
}Parameters
| Name | Description |
|---|---|
| The previous shape. |
| The next shape. |
Returns
Shape | voidThe next shape or void.
onChildrenChange( )
A callback called when a shape's children change.
onChildrenChange?(shape: Shape): TLShapePartial[] | voidParameters
| Name | Description |
|---|---|
| The shape. |
Returns
TLShapePartial[] | voidAn array of shape updates, or void.
onClick( )
A callback called when a shape is clicked.
onClick?(shape: Shape): TLShapePartial<Shape> | voidParameters
| Name | Description |
|---|---|
| The shape. |
Returns
TLShapePartial<Shape> | voidA change to apply to the shape, or void.
onCrop( )
A callback called when a shape changes from a crop.
onCrop?(
shape: Shape,
info: TLCropInfo<Shape>
): Omit<TLShapePartial<Shape>, 'id' | 'type'> | undefined | voidParameters
| Name | Description |
|---|---|
| The shape at the start of the crop. |
| Info about the crop. |
Returns
Omit<TLShapePartial<Shape>, 'id' | 'type'> | undefined | voidA change to apply to the shape, or void.
onDoubleClick( )
A callback called when a shape is double clicked.
onDoubleClick?(shape: Shape): TLShapePartial<Shape> | voidParameters
| Name | Description |
|---|---|
| The shape. |
Returns
TLShapePartial<Shape> | voidA change to apply to the shape, or void.
onDoubleClickCorner( )
A callback called when a shape's corner is double clicked.
onDoubleClickCorner?(
shape: Shape,
info: TLClickEventInfo
): TLShapePartial<Shape> | voidParameters
| Name | Description |
|---|---|
| The shape. |
| Info about the corner. |
Returns
TLShapePartial<Shape> | voidA change to apply to the shape, or void.
onDoubleClickEdge( )
A callback called when a shape's edge is double clicked.
onDoubleClickEdge?(
shape: Shape,
info: TLClickEventInfo
): TLShapePartial<Shape> | voidParameters
| Name | Description |
|---|---|
| The shape. |
| Info about the edge. |
Returns
TLShapePartial<Shape> | voidA change to apply to the shape, or void.
onDoubleClickHandle( )
A callback called when a shape's handle is double clicked.
onDoubleClickHandle?(
shape: Shape,
handle: TLHandle
): TLShapePartial<Shape> | voidParameters
| Name | Description |
|---|---|
| The shape. |
| The handle that is double-clicked. |
Returns
TLShapePartial<Shape> | voidA change to apply to the shape, or void.
onDragShapesIn( )
A callback called when some other shapes are dragged into this one. This fires when the shapes are dragged over the shape for the first time.
onDragShapesIn?(
shape: Shape,
shapes: TLShape[],
info: TLDragShapesInInfo
): voidParameters
| Name | Description |
|---|---|
| The shape. |
| The shapes that are being dragged in. |
|
Returns
voidonDragShapesOut( )
A callback called when some other shapes are dragged out of this one.
onDragShapesOut?(
shape: Shape,
shapes: TLShape[],
info: TLDragShapesOutInfo
): voidParameters
| Name | Description |
|---|---|
| The shape. |
| The shapes that are being dragged out. |
|
Returns
voidonDragShapesOver( )
A callback called when some other shapes are dragged over this one. This fires when the shapes are dragged over the shape for the first time (after the onDragShapesIn callback), and again on every update while the shapes are being dragged.
onDragShapesOver?(
shape: Shape,
shapes: TLShape[],
info: TLDragShapesOverInfo
): voidExample
onDragShapesOver = (shape, shapes) => {
this.editor.reparentShapes(shapes, shape.id)
}Parameters
| Name | Description |
|---|---|
| The shape. |
| The shapes that are being dragged over this one. |
|
Returns
voidonDropShapesOver( )
A callback called when some other shapes are dropped over this one.
onDropShapesOver?(
shape: Shape,
shapes: TLShape[],
info: TLDropShapesOverInfo
): voidParameters
| Name | Description |
|---|---|
| The shape. |
| The shapes that are being dropped over this one. |
|
Returns
voidonEditEnd( )
A callback called when a shape finishes being edited.
onEditEnd?(shape: Shape): voidParameters
| Name | Description |
|---|---|
| The shape. |
Returns
voidonEditStart( )
A callback called when a shape starts being edited.
onEditStart?(shape: Shape): voidParameters
| Name | Description |
|---|---|
| The shape. |
Returns
voidonHandleDrag( )
A callback called when a shape's handle changes.
onHandleDrag?(
shape: Shape,
info: TLHandleDragInfo<Shape>
): TLShapePartial<Shape> | voidParameters
| Name | Description |
|---|---|
| The current shape. |
| An object containing the handle and whether the handle is 'precise' or not. |
Returns
TLShapePartial<Shape> | voidA change to apply to the shape, or void.
onHandleDragCancel( )
A callback called when a shape's handle drag is cancelled.
onHandleDragCancel?(current: Shape, info: TLHandleDragInfo<Shape>): voidParameters
| Name | Description |
|---|---|
| The current shape. |
| An object containing the handle and whether the handle is 'precise' or not. |
Returns
voidonHandleDragEnd( )
A callback called when a shape's handle finishes being dragged.
onHandleDragEnd?(
current: Shape,
info: TLHandleDragInfo<Shape>
): TLShapePartial<Shape> | voidParameters
| Name | Description |
|---|---|
| The current shape. |
| An object containing the handle and whether the handle is 'precise' or not. |
Returns
TLShapePartial<Shape> | voidA change to apply to the shape, or void.
onHandleDragStart( )
A callback called when a shape's handle starts being dragged.
onHandleDragStart?(
shape: Shape,
info: TLHandleDragInfo<Shape>
): TLShapePartial<Shape> | voidParameters
| Name | Description |
|---|---|
| The shape. |
| An object containing the handle and whether the handle is 'precise' or not. |
Returns
TLShapePartial<Shape> | voidA change to apply to the shape, or void.
onResize( )
onResize(shape: any, info: TLResizeInfo<any>): anyParameters
| Name | Description |
|---|---|
| |
| |
Returns
anyonResizeCancel( )
A callback called when a shape resize is cancelled.
onResizeCancel?(initial: Shape, current: Shape): voidParameters
| Name | Description |
|---|---|
| The shape at the start of the resize. |
| The current shape. |
Returns
voidonResizeEnd( )
A callback called when a shape finishes resizing.
onResizeEnd?(initial: Shape, current: Shape): TLShapePartial<Shape> | voidParameters
| Name | Description |
|---|---|
| The shape at the start of the resize. |
| The current shape. |
Returns
TLShapePartial<Shape> | voidA change to apply to the shape, or void.
onResizeStart( )
A callback called when a shape starts being resized.
onResizeStart?(shape: Shape): TLShapePartial<Shape> | voidParameters
| Name | Description |
|---|---|
| The shape. |
Returns
TLShapePartial<Shape> | voidA change to apply to the shape, or void.
onRotate( )
A callback called when a shape changes from a rotation.
onRotate?(initial: Shape, current: Shape): TLShapePartial<Shape> | voidParameters
| Name | Description |
|---|---|
| The shape at the start of the rotation. |
| The current shape. |
Returns
TLShapePartial<Shape> | voidA change to apply to the shape, or void.
onRotateCancel( )
A callback called when a shape rotation is cancelled.
onRotateCancel?(initial: Shape, current: Shape): voidParameters
| Name | Description |
|---|---|
| The shape at the start of the rotation. |
| The current shape. |
Returns
voidonRotateEnd( )
A callback called when a shape finishes rotating.
onRotateEnd?(initial: Shape, current: Shape): TLShapePartial<Shape> | voidParameters
| Name | Description |
|---|---|
| The shape at the start of the rotation. |
| The current shape. |
Returns
TLShapePartial<Shape> | voidA change to apply to the shape, or void.
onRotateStart( )
A callback called when a shape starts being rotated.
onRotateStart?(shape: Shape): TLShapePartial<Shape> | voidParameters
| Name | Description |
|---|---|
| The shape. |
Returns
TLShapePartial<Shape> | voidA change to apply to the shape, or void.
onTranslate( )
A callback called when a shape changes from a translation.
onTranslate?(initial: Shape, current: Shape): TLShapePartial<Shape> | voidParameters
| Name | Description |
|---|---|
| The shape at the start of the translation. |
| The current shape. |
Returns
TLShapePartial<Shape> | voidA change to apply to the shape, or void.
onTranslateCancel( )
A callback called when a shape translation is cancelled.
onTranslateCancel?(initial: Shape, current: Shape): voidParameters
| Name | Description |
|---|---|
| The shape at the start of the translation. |
| The current shape. |
Returns
voidonTranslateEnd( )
A callback called when a shape finishes translating.
onTranslateEnd?(initial: Shape, current: Shape): TLShapePartial<Shape> | voidParameters
| Name | Description |
|---|---|
| The shape at the start of the translation. |
| The current shape. |
Returns
TLShapePartial<Shape> | voidA change to apply to the shape, or void.
onTranslateStart( )
A callback called when a shape starts being translated.
onTranslateStart?(shape: Shape): TLShapePartial<Shape> | voidParameters
| Name | Description |
|---|---|
| The shape. |
Returns
TLShapePartial<Shape> | voidA change to apply to the shape, or void.
shouldClipChild( )
Whether a specific child shape should be clipped by this shape. Only called if getClipPath returns a valid polygon.
If not defined, the default behavior is to clip all children.
shouldClipChild?(child: TLShape): booleanParameters
| Name | Description |
|---|---|
| The child shape to check |
Returns
booleanboolean indicating if this child should be clipped
toBackgroundSvg( )
Get the shape's background layer as an SVG object.
toBackgroundSvg?(
shape: Shape,
ctx: SvgExportContext
): null | Promise<null | ReactElement> | ReactElementParameters
| Name | Description |
|---|---|
| The shape. |
| ctx - The export context for the SVG - used for adding e.g. <def>s |
Returns
null | Promise<null | ReactElement> | ReactElementAn SVG element.
toSvg( )
Get the shape as an SVG object.
toSvg?(
shape: Shape,
ctx: SvgExportContext
): null | Promise<null | ReactElement> | ReactElementParameters
| Name | Description |
|---|---|
| The shape. |
| The export context for the SVG - used for adding e.g. <def>s |
Returns
null | Promise<null | ReactElement> | ReactElementAn SVG element.