Skip to content
  • Android
  • iOS
  • Web

Drawing

Create a drawing document, mount a canvas and manage brush strokes and layers.

Examples

Quick start

nayan-ui/drawing
import { useEffect, useState } from 'react';
import { DrawingCanvas, DrawingDocument } from 'nayan-ui/drawing';
import { warmUpEngine } from 'nayan-ui/games';

export function Sketch() {
  const [document, setDocument] = useState<DrawingDocument>();
  const [error, setError] = useState<string>();
  useEffect(() => {
    let active = true;
    let drawing: DrawingDocument | undefined;
    void warmUpEngine().then(() => {
      if (!active) return;
      drawing = new DrawingDocument(320, 240);
      setDocument(drawing);
    }).catch((cause: unknown) => {
      if (active) setError(String(cause));
    });
    return () => { active = false; drawing?.dispose(); };
  }, []);
  if (error) throw new Error(error);
  return document ? <DrawingCanvas document={document} style={{ height: 240 }} /> : null;
}

Add a layer and configure a tool

nayan-ui/drawing
import type { DrawingDocument } from 'nayan-ui/drawing';

export function configureSketch(document: DrawingDocument) {
  document.layer('add', { name: 'Outline' });
  document.setBrush({ brushType: 'PENCIL', color: '#475569', width: 3, opacity: 0.9, pressure: true });
}

export function configureShape(document: DrawingDocument) {
  document.setBrush({
    brushType: 'NORMAL', color: '#0ea5e9', width: 3, opacity: 1, tool: 'circle', filled: true
  });
}

Save, restore, and export a drawing

nayan-ui/drawing
import type { DrawingDocument } from 'nayan-ui/drawing';

export function saveSketch(document: DrawingDocument) {
  return { project: document.exportProject(), svg: document.exportSVG() };
}

export function restoreSketch(document: DrawingDocument, project: string) {
  return document.load(project);
}
Related guide

Tags