Skip to content

Using the Editor API

Manipulate the contents of the canvas using the editor API.

<script setup lang="ts">
import '@woven-canvas/vue/style.css'
import {
type Editor,
Block,
addComponent,
createEntity,
RankBounds,
Synced,
Shape,
SelectAll,
} from '@woven-canvas/core'
import { WovenCanvas } from '@woven-canvas/vue'
import Palette from './Palette.vue'
function handleReady(editor: Editor) {
// Create 3 shapes in a row
editor.nextTick((ctx) => {
for (let i = 0; i < 3; i++) {
const entityId = createEntity(ctx)
addComponent(ctx, entityId, Synced, { id: crypto.randomUUID() })
addComponent(ctx, entityId, Shape, {
kind: 'rectangle',
strokeWidth: 4,
fillRed: 128,
fillGreen: 0,
fillBlue: 0,
fillAlpha: 128,
})
addComponent(ctx, entityId, Block, {
tag: 'shape',
position: [100 + i * 150, 150],
size: [100, 100],
rank: RankBounds.genNext(ctx),
})
}
// Select all after creation
editor.command(SelectAll)
})
}
</script>
<template>
<WovenCanvas @ready="handleReady">
<Palette />
</WovenCanvas>
</template>

This example creates shapes on mount using the @ready event. The Palette component demonstrates useEditorContext() to access the editor from a child component, using defineQuery to find all shapes and Shape.write() to update their fill colors.