Socket
Socket
Sign inDemoInstall

@tldraw/core

Package Overview
Dependencies
Maintainers
2
Versions
197
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tldraw/core

A tiny little drawing app (core).


Version published
Weekly downloads
4.9K
increased by18.59%
Maintainers
2
Weekly downloads
 
Created
Source

@tldraw/core

This package contains the core of the tldraw library. It includes:

Installation

yarn add @tldraw/core --peer

Usage

Import the Renderer React component and pass it the required props.

Documentation

Renderer

PropTypeDescription
pageTLPageThe current page object.
pageStateTLPageStateThe current page's state.
shapeUtilsTLShapeUtils{}The shape utilities used to render the shapes.
themeobject(optional) an object with overrides for the Renderer's default colors.

The theme object accepts valid CSS colors for the following properties:

PropertyDescription
foreground(optional) The primary (usually "text") color
background(optional) The default page's background color
brushFill(optional) The fill color of the brush selection box
brushStroke(optional) The stroke color of the brush selection box
selectFill(optional) The fill color of the selection bounds
selectStroke(optional) The stroke color of the selection bounds and handles

Tip: If providing an object for the theme prop, either define the object outside of the parent component or memoize it with React.useMemo.

The renderer also accepts many (optional) event callbacks.

PropDescription
onPanThe user panned with the mouse wheel
onZoomThe user zoomed with the mouse wheel
onPinchThe user moved their pointers during a pinch
onPinchEndThe user stopped a two-pointer pinch
onPinchStartThe user began a two-pointer pinch
onPointerMoveThe user moved their pointer
onPointerUpThe user ended a point
onPointShapeThe user pointed a shape
onDoubleClickShapeThe user double-pointed a shape
onRightPointShapeThe user right-pointed a shape
onMoveOverShapeThe user moved their pointer a shape
onHoverShapeThe user moved their pointer onto a shape
onUnhoverShapeThe user moved their pointer off of a shape
onPointHandleThe user pointed a shape handle
onDoubleClickHandleThe user double-pointed a shape handle
onRightPointHandle-he user right-pointed a shape handle
onMoveOverHandleThe user moved their pointer over a shape handle
onHoverHandleThe user moved their pointer onto a shape handle
onUnhoverHandleThe user moved their pointer off of a shape handle
onPointCanvasThe user pointed the canvas
onDoubleClickCanvasThe user double-pointed the canvas
onRightPointCanvasThe user right-pointed the canvas
onPointBoundsThe user pointed the selection bounds
onDoubleClickBoundsThe user double-pointed the selection bounds
onRightPointBoundsThe user right-pointed the selection bounds
onPointBoundsHandleThe user pointed a selection bounds edge or corner
onDoubleClickBoundsHandleThe user double-pointed a selection bounds edge or corner
onBlurEditingShapeThe user blurred an editing (text) shape
onErrorThe renderer encountered an error

Tip: If providing callbacks, either define the functions outside of the parent component or memoize them first with React.useMemo.

TLPage

An object describing the current page. It contains:

PropertyTypeDescription
idstringA unique id for the page.
shapesTLShape{}A table of shapes.
bindingsTLBinding{}A table of bindings.
backgroundColorstring(optional) The page's background fill color. Will also overwrite the theme.

TLPageState

An object describing the current page. It contains:

PropertyTypeDescription
idstringThe corresponding page's id
selectedIdsstring[]An array of selected shape ids
cameraobjectAn object describing the camera state
camera.pointnumber[]The camera's [x, y] coordinates
camera.zoomnumberThe camera's zoom level
currentParentIdstring(optional) The current parent id for selection
brushTLBounds(optional) A Bounds for the current selection box
pointedIdstring(optional) The currently pointed shape id
hoveredIdstring(optional) The currently hovered shape id
editingIdstring(optional) The currently editing shape id
editingBindingIdstring(optional) The currently editing binding id

TLShape

An object that describes a shape on the page. The shapes in your document should extend this interface with other properties. See Guide: Create a Custom Shape.

PropertyTypeDescription
idstringThe shape's id.
typestringThe type of the shape, corresponding to the type of a TLShapeUtil.
parentIdstringThe id of the shape's parent (either the current page or another shape).
childIndexnumberthe order of the shape among its parent's children
namestringthe name of the shape
pointnumber[]the shape's current [x, y] coordinates on the page
rotationnumber(optiona) The shape's current rotation in radians
childrenstring[](optional) An array containing the ids of this shape's children
handlesTLHandle{}(optional) A table of TLHandle objects
isLockedboolean(optional) True if the shape is locked
isHiddenboolean(optional) True if the shape is hidden
isEditingboolean(optional) True if the shape is currently editing
isGeneratedbooleanoptional) True if the shape is generated programatically
isAspectRatioLockedboolean(optional) True if the shape's aspect ratio is locked

TLHandle

An object that describes a relationship between two shapes on the page.

PropertyTypeDescription
idstringAn id for the handle.
indexnumberThe handle's order within the shape's handles.
pointnumber[]The handle's [x, y] coordinates.

TLBinding

An object that describes a relationship between two shapes on the page.

PropertyTypeDescription
idstringA unique id for the binding.
typestringThe binding's type.
fromIdstringThe id of the shape where the binding begins.
toIdstringThe id of the shape where the binding begins.

TLShapeUtil

The TLShapeUtil is an abstract class that you can extend to create utilities for your custom shapes. See Guide: Create a Custom Shape.

brushUpdater

The brushUpdater is a special class instance that allows you to quickly update the selection brush rectangle.

MethodDescription
seta method that accepts either a TLBounds object or undefined
cleara method to hide the brush

Normally, the renderer's brush will update in response to changes to pageState.brush; however, calling brushUpdater.set will produce a faster change in the brush rectangle. Calling brushUpdater.set will prevent the brush from any future updates from pageState.brush.

inputs

A class instance that stores the current pointer position and pressed keys.

Utils

A general purpose utility class.

Vec

A utility class for vector math and related methods.

Svg

A utility class for creating SVG path data through imperative commands.

Intersect

A utility class for finding intersections between various geometric shapes.

Guides

Create a Custom Shape

...

Example

import * as React from "react"
import { Renderer, TLShape, TLShapeUtil, Vec } from '@tldraw/core'

interface RectangleShape extends TLShape {
  type: "rectangle",
  size: number[]
}

class Rectangle extends TLShapeUtil<RectangleShape> {
  // See the "Create a Custom Shape" guide
}

const myShapes = { rectangle: new Rectangle() }

function App() {
  const [page, setPage] = React.useState({
    id: "page1"
    shapes: {
      "rect1": {
        id: 'rect1',
        parentId: 'page1',
        name: 'Rectangle',
        childIndex: 0,
        type: 'rectangle',
        point: [0, 0],
        rotation: 0,
        size: [100, 100],
      }
    },
    bindings: {}
  })

  const [pageState, setPageState] = React.useState({
    id: "page1",
    selectedIds: [],
    camera: {
      point: [0,0],
      zoom: 1
    }
  })

  const handlePan = React.useCallback((delta: number[]) => {
    setPageState(pageState => {
      ...pageState,
      camera: {
        zoom,
        point: Vec.sub(pageState.point, Vec.div(delta, pageState.zoom)),
      },
    })
  }, [])

  return (<Renderer
    shapes={myShapes}
    page={page}
    pageState={pageState}
    onPan={handlePan}
  />)
}

Development

Run yarn to install dependencies.

Run yarn start to begin the monorepo's development server (@tldraw/site).

Run nx test core to execute unit tests via Jest.

Contribution

To contribute, visit the Discord channel.

FAQs

Package last updated on 31 Aug 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc