Skip to content

useStudio

The primary hook for interacting with the canvas editor. Provides access to items, selection state, and all item manipulation functions.

Import

ts
import { useStudio } from '@asset-studio/core'

Usage

tsx
function MyComponent() {
  const {
    items,
    selectedItem,
    addItem,
    updateItem,
    deleteItem,
  } = useStudio()

  // ...
}

Return Value

State

PropertyTypeDescription
itemsItem[]All items currently on the canvas.
selectedItemIdstring | nullID of the currently selected item.
selectedItemItem | undefinedThe full object of the selected item.
documentWidthnumberCanvas document width in pixels.
documentHeightnumberCanvas document height in pixels.
durationnumberTotal timeline duration in milliseconds.
stageRefReact.RefObject<Konva.Stage>Direct ref to the Konva stage for low-level access.

Item Management

FunctionSignatureDescription
addItem(type: ItemType, data: Partial<Item>) => voidAdd a new item to the canvas.
updateItem(id: string, changes: Partial<Item>) => voidUpdate properties of an existing item.
updateItems(updates: Array<{ id: string } & Partial<Item>>) => voidUpdate multiple items at once.
deleteItem(id: string) => voidRemove an item from the canvas.
clearItems() => voidRemove all items from the canvas and clear selection.
duplicateItem(id: string) => voidDuplicate an item.
reorderItem(fromIndex: number, toIndex: number) => voidChange the Z-order of items.

Selection

FunctionSignatureDescription
setSelectedItemId(id: string | null) => voidSelect an item by ID, or clear selection with null.

Document

FunctionSignatureDescription
setDocumentWidth(width: number) => voidChange the canvas document width.
setDocumentHeight(height: number) => voidChange the canvas document height.

History

FunctionSignatureDescription
handleBack() => voidUndo the last action.
handleForward() => voidRedo the last undone action.

Examples

Add a text item

tsx
const { addItem } = useStudio()

addItem('Text', {
  x: 100, y: 100,
  width: 400, height: 80,
  itemProps: {
    text: 'Hello, World!',
    fontSize: 48,
    color: '#ffffff',
    fontFamily: 'Inter',
  },
})

Update an item

tsx
const { updateItem, selectedItemId } = useStudio()

// Move the selected item
updateItem(selectedItemId, { x: 200, y: 150 })

// Change text content
updateItem(selectedItemId, { itemProps: { text: 'Updated text' } })

Delete selected item

tsx
const { deleteItem, selectedItemId } = useStudio()

if (selectedItemId) {
  deleteItem(selectedItemId)
}

Undo / Redo

tsx
const { handleBack, handleForward } = useStudio()

// Wire to keyboard shortcuts
useEffect(() => {
  function onKeyDown(e) {
    if (e.metaKey && e.key === 'z') handleBack()
    if (e.metaKey && e.shiftKey && e.key === 'z') handleForward()
  }
  window.addEventListener('keydown', onKeyDown)
  return () => window.removeEventListener('keydown', onKeyDown)
}, [])

Released under a proprietary license.