A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.
TypeScript animationcartoonclip-studio-paintcollaborativedesktop-appdigital-paintingdrawing-appillustrationmangamultiplayeropen-sourcerusttaurithreejswebglwebtoon
import { useRef, useState } from 'react';
import {
LastStrokeEndState,
UseCanvasDrawingProps,
useBrushStrokeSession,
useDrawingHistory,
} from './drawing';
import type { QuickShapePreviewState } from '../utils/quickShapeEngine';
export type { UseCanvasDrawingProps, PushHistoryStepFn } from './drawing';
/**
* Thin canvas drawing dispatcher coordinating native Rust CoW history, DX12 stroke commits,
* and vector stroke commit/erase via modular hooks in `src/hooks/drawing/`.
*/
export function useCanvasDrawing({
tool,
brushSettings,
dimensions,
selection = null,
colorMode = 'color',
disabled = false,
viewportRef,
pan,
zoom,
rotation,
flipX = false,
flipY = false,
navigation,
activeLayerId,
activeLayerLocked = false,
activeLayerVisible = true,
layers,
onUpdateLayerThumbnail,
onApplyLayerTreeDelta,
onApplyDocumentStateDelta,
isMaskMode = false,
onUpdateMaskThumbnail,
onCollabStrokeStart,
onCollabStrokeChunk,
onCollabStrokeFinish,
onCollabCursorMove,
perspectiveRuler = null,
straightRuler = null,
onActiveConstraintAxisChange,
activeSessionId = null,
}: UseCanvasDrawingProps) {
const interactionRef = useRef<HTMLDivElement | null>(null);
const isDrawingRef = useRef(false);
const lastStrokeEndRef = useRef<LastStrokeEndState | null>(null);
const abortActiveStrokeRef = useRef<() => void>(() => {});
const [quickShapePreview, setQuickShapePreview] = useState<QuickShapePreviewState | null>(null);
const onQuickShapePreviewChangeRef = useRef<
((preview: QuickShapePreviewState | null) => void) | undefined
>(setQuickShapePreview);
onQuickShapePreviewChangeRef.current = setQuickShapePreview;
const fallbackPanRef = useRef(pan);
const fallbackZoomRef = useRef(zoom);
const fallbackRotationRef = useRef(rotation);
const fallbackFlipXRef = useRef(flipX);
const fallbackFlipYRef = useRef(flipY);
const panRef = navigation?.panRef ?? fallbackPanRef;
const zoomRef = navigation?.zoomRef ?? fallbackZoomRef;
const rotationRef = navigation?.rotationRef ?? fallbackRotationRef;
const flipXRef = navigation?.flipXRef ?? fallbackFlipXRef;
const flipYRef = navigation?.flipYRef ?? fallbackFlipYRef;
const toolOverrideRef = useRef(tool);
const brushSettingsOverrideRef = useRef(brushSettings);
const colorModeRef = useRef(colorMode);
const activeLayerIdRef = useRef(activeLayerId);
const activeLayerLockedRef = useRef(activeLayerLocked);
const activeLayerVisibleRef = useRef(activeLayerVisible);
const dimensionsRef = useRef(dimensions);
const selectionRef = useRef(selection);
const isMaskModeRef = useRef(isMaskMode);
const layersRef = useRef(layers);
const onApplyLayerTreeDeltaRef = useRef(onApplyLayerTreeDelta);
const onApplyDocumentStateDeltaRef = useRef(onApplyDocumentStateDelta);
const onUpdateMaskThumbnailRef = useRef(onUpdateMaskThumbnail);
const onCollabStrokeStartRef = useRef(onCollabStrokeStart);
const onCollabStrokeChunkRef = useRef(onCollabStrokeChunk);
const onCollabStrokeFinishRef = useRef(onCollabStrokeFinish);
const onCollabCursorMoveRef = useRef(onCollabCursorMove);
const rulerRef = useRef(perspectiveRuler);
const straightRulerRef = useRef(straightRuler);
const onActiveConstraintAxisChangeRef = useRef(onActiveConstraintAxisChange);
// Sync imperatively-read refs synchronously during render so native tablet channels and
// immediate pointerdown events after layer create/delete/switch never observe stale values.
if (!navigation) {
fallbackPanRef.current = pan;
fallbackZoomRef.current = zoom;
fallbackRotationRef.current = rotation;
fallbackFlipXRef.current = flipX;
fallbackFlipYRef.current = flipY;
}
toolOverrideRef.current = tool;
brushSettingsOverrideRef.current = brushSettings;
colorModeRef.current = colorMode;
activeLayerIdRef.current = activeLayerId;
activeLayerLockedRef.current = activeLayerLocked;
activeLayerVisibleRef.current = activeLayerVisible;
dimensionsRef.current = dimensions;
selectionRef.current = selection;
isMaskModeRef.current = isMaskMode;
layersRef.current = layers;
onApplyLayerTreeDeltaRef.current = onApplyLayerTreeDelta;
onApplyDocumentStateDeltaRef.current = onApplyDocumentStateDelta;
onUpdateMaskThumbnailRef.current = onUpdateMaskThumbnail;
onCollabStrokeStartRef.current = onCollabStrokeStart;
onCollabStrokeChunkRef.current = onCollabStrokeChunk;
onCollabStrokeFinishRef.current = onCollabStrokeFinish;
onCollabCursorMoveRef.current = onCollabCursorMove;
rulerRef.current = perspectiveRuler;
straightRulerRef.current = straightRuler;
onActiveConstraintAxisChangeRef.current = onActiveConstraintAxisChange;
const history = useDrawingHistory({
activeSessionId,
activeLayerId,
activeLayerLocked,
dimensions,
dimensionsRef,
activeLayerIdRef,
colorModeRef,
layersRef,
onUpdateLayerThumbnail,
onApplyLayerTreeDeltaRef,
onApplyDocumentStateDeltaRef,
onUpdateMaskThumbnailRef,
isDrawingRef,
lastStrokeEndRef,
abortActiveStrokeRef,
});
const brushSession = useBrushStrokeSession({
disabled,
activeSessionId,
viewportRef,
interactionRef,
isDrawingRef,
lastStrokeEndRef,
panRef,
zoomRef,
rotationRef,
flipXRef,
flipYRef,
toolOverrideRef,
brushSettingsOverrideRef,
colorModeRef,
activeLayerIdRef,
activeLayerLockedRef,
activeLayerVisibleRef,
dimensionsRef,
selectionRef,
isMaskModeRef,
layersRef,
rulerRef,
straightRulerRef,
onActiveConstraintAxisChangeRef,
onQuickShapePreviewChangeRef,
onUpdateLayerThumbnail,
onUpdateMaskThumbnailRef,
onCollabStrokeStartRef,
onCollabStrokeChunkRef,
onCollabStrokeFinishRef,
onCollabCursorMoveRef,
pushHistoryStep: history.pushHistoryStep,
flushPendingHistoryTasks: history.flushPendingHistoryTasks,
enqueueHistoryTask: history.enqueueHistoryTask,
});
abortActiveStrokeRef.current = brushSession.abortActiveStroke;
return {
interactionRef,
isDrawingRef,
abortActiveStroke: brushSession.abortActiveStroke,
pushHistoryStep: history.pushHistoryStep,
flushPendingHistoryTasks: history.flushPendingHistoryTasks,
handlePointerDown: brushSession.handlePointerDown,
handlePointerMove: brushSession.handlePointerMove,
handlePointerUp: brushSession.handlePointerUp,
handlePointerEnter: brushSession.handlePointerEnter,
handlePointerLeave: brushSession.handlePointerLeave,
undo: history.undo,
redo: history.redo,
canUndo: history.canUndo,
canRedo: history.canRedo,
historyList: history.historyList,
historyIndex: history.historyIndex,
jumpToHistoryIndex: history.jumpToHistoryIndex,
clearCanvas: history.clearCanvas,
resetHistory: history.resetHistory,
selectiveUndo: history.selectiveUndo,
selectiveRedo: history.selectiveRedo,
quickShapePreview,
cursorPos: null as { x: number; y: number } | null,
currentPressure: 0,
isHovering: false,
};
}