Skip to content

useDraw

Controls the drawing mode and all drawing tool settings on the canvas.

Import

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

Usage

tsx
function DrawToolbar() {
  const {
    isDrawMode,
    changeDrawMode,
    drawType,
    setDrawType,
    drawColor,
    setDrawColor,
    drawStrokeWidth,
    setDrawStrokeWidth,
  } = useDraw()

  return (
    <div>
      <button onClick={() => changeDrawMode(isDrawMode ? 'select' : 'draw')}>
        {isDrawMode ? 'Exit Draw' : 'Draw'}
      </button>
      <select value={drawType} onChange={e => setDrawType(e.target.value as DrawType)}>
        <option value="free">Freehand</option>
        <option value="straight">Straight</option>
        <option value="shape">Shape</option>
      </select>
      <input
        type="color"
        value={drawColor}
        onChange={e => setDrawColor(e.target.value)}
      />
      <input
        type="range"
        min={1}
        max={50}
        value={drawStrokeWidth}
        onChange={e => setDrawStrokeWidth(Number(e.target.value))}
      />
    </div>
  )
}

Return Value

Property / FunctionTypeDescription
isDrawModebooleanWhether draw mode is currently active.
isSelectionModebooleanWhether selection mode is currently active.
changeDrawMode(value: 'draw' | 'select') => voidSwitch between draw and select mode.
drawTypeDrawTypeCurrent draw type — 'free', 'straight', or 'shape'.
setDrawType(value: DrawType) => voidSet the draw type.
drawColorstringCurrent stroke color (hex).
setDrawColor(color: string) => voidSet the stroke color.
drawStrokeWidthnumberCurrent stroke width in pixels.
setDrawStrokeWidth(width: number) => voidSet the stroke width.
drawStrokeStyle'solid' | 'doodle'Current stroke style.
setDrawStrokeStyle(style: 'solid' | 'doodle') => voidSet the stroke style.
drawDashnumber[] | undefinedDash pattern for the stroke.
setDrawDash(dash: number[] | undefined) => voidSet the dash pattern.
drawFillColorstringFill color for closed shapes.
setDrawFillColor(color: string) => voidSet the fill color.
drawShapenumber[] | nullPoint data for the current shape path.
setDrawShape(shape: number[] | null) => voidSet the shape point data.
saveDrawShape() => voidSave the current drawn shape as an item on the canvas.

Types

ts
type DrawType = 'free' | 'straight' | 'shape'

Released under a proprietary license.