<div style="text-align: center; transform: scale(.5);">
<img src="https://github.com/tldraw/tldraw/raw/main/assets/card-repo.png"/>
</div>
# @tldraw/core
This package contains the `Renderer` and core utilities used by [tldraw](https://tldraw.com).
You can use this package to build projects like [tldraw](https://tldraw.com), where React components are rendered on a canvas user interface. Check out the [advanced example](https://core-steveruiz.vercel.app/).
���� Love this library? Consider [becoming a sponsor](https://github.com/sponsors/steveruizok?frequency=recurring&sponsor=steveruizok).
## Installation
Use your package manager of choice to install `@tldraw/core` and its peer dependencies.
```bash
yarn add @tldraw/core && yarn build
# or
npm i @tldraw/core && npm run build
```
> Note: You'll need to run the `build` script before running `dev`.
## Examples
There are two examples in this repository.
The **simple** example in [`examples/core-example`](https://github.com/tldraw/tldraw/tree/main/examples/core-example) shows a minimal use of the library. It does not do much but this should be a good reference for the API without too much else built on top.
The **advanced** example in [`examples/core-example-advanced`](https://github.com/tldraw/tldraw/tree/main/examples/core-example-advanced) shows a more realistic use of the library. (Try it [here](https://core-steveruiz.vercel.app/)). While the fundamental patterns are the same, this example contains features such as: panning, pinching, and zooming the camera; creating, cloning, resizing, and deleting shapes; keyboard shortcuts, brush-selection; shape-snapping; undo, redo; and more. Much of the code in the advanced example comes from the [@tldraw/tldraw](https://tldraw.com) codebase.
If you're working on an app that uses this library, I recommend referring back to the advanced example for tips on how you might implement these features for your own project.
## Usage
Import the `Renderer` React component and pass it the required props.
```tsx
import * as React from "react"
import { Renderer, TLShape, TLShapeUtil, Vec } from '@tldraw/core'
import { BoxShape, BoxUtil } from "./shapes/box"
const shapeUtils = { box: new BoxUtil() }
function App() {
const [page, setPage] = React.useState({
id: "page"
shapes: {
"box1": {
id: 'box1',
type: 'box',
parentId: 'page',
childIndex: 0,
point: [0, 0],
size: [100, 100],
rotation: 0,
}
},
bindings: {}
})
const [pageState, setPageState] = React.useState({
id: "page",
selectedIds: [],
camera: {
point: [0,0],
zoom: 1
}
})
return (<Renderer
page={page}
pageState={pageState}
shapeUtils={shapeUtils}
/>)
}
```
## Documentation
### `Renderer`
To avoid unnecessary renders, be sure to pass "stable" values as props to the `Renderer`. Either define these values outside of the parent component, or place them in React state, or memoize them with `React.useMemo`.
| Prop | Type | Description |
| ------------ | ------------------------------- | ----------------------------------------------------------- |
| `page` | [`TLPage`](#tlpage) | The current page object. |
| `pageState` | [`TLPageState`](#tlpagestate) | The current page's state. |
| `shapeUtils` | [`TLShapeUtils`](#tlshapeutils) | The shape utilities used to render the shapes. |
| `assets` | [`TLAssets`](#tlassets) | (optional) A table of assets used by shapes in the project. |
In addition to these required props, the Renderer accepts many other **optional** props.
| Property | Type | Description |
| -------------------- | ----------------------------- | --------------------------------------------------------------------- |
| `containerRef` | `React.MutableRefObject` | A React ref for the container, where CSS variables will be added. |
| `theme` | `object` | An object with overrides for the Renderer's default colors. |
| `components` | `object` | An object with overrides for the Renderer's default React components. |
| `hideBounds` | `boolean` | Do not show the bounding box for selected shapes. |
| `hideHandles` | `boolean` | Do not show handles for shapes with handles. |
| `hideBindingHandles` | `boolean` | Do not show binding controls for selected shapes with bindings. |
| `hideResizeHandles` | `boolean` | Do not show resize handles for selected shapes. |
| `hideRotateHandles` | `boolean` | Do not show rotate handles for selected shapes. |
| `hideCursors` | `boolean` | Do not show multiplayer cursors. |
| `snapLines` | [`TLSnapLine`](#tlsnapline)[] | An array of "snap" lines. |
| `users` | `object` | A table of [`TLUser`](#tluser)s. |
| `userId` | `object` | The current user's [`TLUser`](#tluser) id. |
The theme object accepts valid CSS colors for the following properties:
| Property | Description |
| -------------- | ---------------------------------------------------- |
| `foreground` | The primary (usually "text") color |
| `background` | The default page's background color |
| `brushFill` | The fill color of the brush selection box |
| `brushStroke` | The stroke color of the brush selection box |
| `selectFill` | The fill color of the selection bounds |
| `selectStroke` | The stroke color of the selection bounds and handles |
The components object accepts React components for the following properties:
| Property | Description |
| -------- | --------------------------------- |
| `Cursor` | Multiplayer cursors on the canvas |
The Renderer also accepts many (optional) event callbacks.
| Prop | Description |
| --------------------------- | ----------------------------------------------------------- |
| `onPan` | Panned with the mouse wheel |
| `onZoom` | Zoomed with the mouse wheel |
| `onPinchStart` | Began a two-pointer pinch |
| `onPinch` | Moved their pointers during a pinch |
| `onPinchEnd` | Stopped a two-pointer pinch |
| `onPointerDown` | Started pointing |
| `onPointerMove` | Moved their pointer |
| `onPointerUp` | Ended a point |
| `onPointCanvas` | Pointed the canvas |
| `onDoubleClickCanvas` | Double-pointed the canvas |
| `onRightPointCanvas` | Right-pointed the canvas |
| `onDragCanvas` | Dragged the canvas |
| `onReleaseCanvas` | Stopped pointing the canvas |
| `onHoverShape`
没有合适的资源?快使用搜索试试~ 我知道了~
一个小巧的绘图应用程序。___下载.zip
共859个文件
ts:410个
tsx:224个
json:80个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 72 浏览量
2023-04-18
00:14:36
上传
评论
收藏 7.53MB ZIP 举报
温馨提示
一个小巧的绘图应用程序。___下载.zip
资源推荐
资源详情
资源评论
收起资源包目录
一个小巧的绘图应用程序。___下载.zip (859个子文件)
snippets.code-snippets 289B
styles.css 641B
globals.css 392B
styles.css 342B
styles.css 225B
index.css 225B
.eslintignore 38B
.eslintrc 1KB
extension 0B
recording.gif 2.38MB
recording.gif 2.38MB
.gitignore 386B
.gitignore 253B
.gitignore 253B
.gitignore 253B
.gitignore 222B
.gitignore 64B
index.html 407B
index.html 370B
index.html 361B
index.html 356B
favicon.ico 15KB
favicon.ico 15KB
favicon.ico 15KB
favicon.ico 15KB
.ignore 83B
build.js 1KB
package.js 986B
dev.js 927B
next.config.js 596B
index.js 34B
tldraw-assets.json 1004KB
tldraw-assets.json 1004KB
tldraw-assets.json 1004KB
tldraw-assets.json 1004KB
my.json 8KB
th.json 7KB
te.json 7KB
ru.json 6KB
fa.json 6KB
ne.json 5KB
es.json 5KB
gl.json 5KB
de.json 5KB
ar.json 5KB
ku.json 4KB
ko-kr.json 4KB
main.json 4KB
nn-no.json 4KB
pt-br.json 4KB
uk.json 4KB
pl.json 4KB
da.json 4KB
fr.json 4KB
sv.json 3KB
zh-cn.json 3KB
package.json 3KB
ja.json 3KB
he.json 3KB
it.json 3KB
package.json 3KB
zh-tw.json 3KB
pt-pt.json 3KB
tr.json 3KB
nb-no.json 3KB
package.json 3KB
package.json 2KB
package.json 1KB
package.json 1KB
turbo.json 1KB
package.json 1KB
tsconfig.base.json 1014B
package.json 980B
tsconfig.json 957B
package.json 944B
package.json 913B
launch.json 867B
package.json 830B
package.json 804B
manifest.json 771B
tsconfig.json 711B
tsconfig.json 710B
tsconfig.json 683B
settings.json 425B
tsconfig.json 412B
tsconfig.json 399B
tsconfig.json 390B
tsconfig.build.json 377B
tsconfig.dev.json 377B
tsconfig.build.json 363B
tsconfig.dev.json 363B
tsconfig.json 363B
tsconfig.build.json 363B
tsconfig.dev.json 363B
tsconfig.build.json 337B
tsconfig.dev.json 337B
tsconfig.build.json 337B
tsconfig.dev.json 337B
config.json 256B
tsconfig.json 216B
共 859 条
- 1
- 2
- 3
- 4
- 5
- 6
- 9
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9154
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功