tldraw/state/code/docs.md

118 lines
2.5 KiB
Markdown
Raw Normal View History

2021-06-27 13:53:06 +00:00
Welcome to the documentation for tldraw's code editor. You can use the code editor to create shapes using JavaScript or TypeScript code.
2021-06-27 13:55:48 +00:00
```ts
2021-06-27 13:53:06 +00:00
const rect = new Rectangle({
point: [100, 100],
size: [200, 200],
style: {
2021-06-27 13:55:48 +00:00
color: ColorStyle.Blue,
},
2021-06-27 13:53:06 +00:00
})
rect.x = 300
```
2021-06-27 13:55:48 +00:00
To run your code, press **Command + S**.
2021-06-27 13:53:06 +00:00
Your new shapes will appear on the canvas. You can interact with code-created shapes just like any other shape: you can move the shape, change its style, delete it, etc.
Each time you run your code, any existing code-created shapes will be replaced by your new code-created shapes. If you want to keep your code-created shapes, select the shapes that you want to keep, press **Command + D** to duplicate them, and move them off to the side.
## Shapes
You can use the code editor to create any of the regular shapes:
- Draw
- Rectangle
- Ellipse
- Arrow
- Text
You can also create shapes that can _only_ be created with code:
- Dot
- Ray
- Line
- Polyline
Each of these shapes is a `class`. To create the shape, use the following syntax:
2021-06-27 13:55:48 +00:00
```ts
2021-06-27 13:53:06 +00:00
const myShape = new Rectangle()
```
You can also create a shape with custom properties like this:
2021-06-27 13:55:48 +00:00
```ts
2021-06-27 13:53:06 +00:00
const myShape = new Rectangle({
point: [100, 100],
size: [200, 200],
style: {
color: ColorStyle.Blue,
size: SizeStyle.Large,
2021-06-27 13:55:48 +00:00
dash: DashStyle.Dotted,
},
2021-06-27 13:53:06 +00:00
})
```
Once you've created a shape, you can set its properties like this:
2021-06-27 13:55:48 +00:00
```ts
2021-06-27 13:53:06 +00:00
const myShape = new Rectangle()
myShape.x = 100
myShape.color = ColorStyle.Red
```
You can find more information on each shape class by clicking its name in the list above.
## Controls
In addition to shapes, you can also use code to create controls.
```ts
new NumberControl({
2021-06-27 13:55:48 +00:00
label: 'x',
value: 0,
2021-06-27 13:53:06 +00:00
})
const myShape = new Rectangle({
2021-06-27 13:55:48 +00:00
point: [controls.x, 0],
2021-06-27 13:53:06 +00:00
})
```
Once you've created a control, the app's will display a panel where you can edit the control's value. As you edit the value, your code will run again with the control's new value.
There are two kinds of controls:
- NumberControl
- VectorControl
- TextControl
Each of these controls is a `class`. To create the control, use the following syntax:
2021-06-27 13:55:48 +00:00
```ts
2021-06-27 13:53:06 +00:00
const control = new TextControl({
2021-06-27 13:55:48 +00:00
label: 'myLabel',
value: 'my value',
2021-06-27 13:53:06 +00:00
})
```
Once you've created a control, you can use its value in your code like this:
```ts
const myShape = new Text({
2021-06-27 13:55:48 +00:00
text: controls.myLabel,
2021-06-27 13:53:06 +00:00
})
```
You can find more information on each control class by clicking its name in the list above.
## Shape Classes
...
## Control Classes
2021-06-27 13:55:48 +00:00
...