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 type { LayerItem } from '../types/editor';
export interface ViewportTransform {
pan: { x: number; y: number };
zoom: number;
rotation: number;
flipX?: boolean;
flipY?: boolean;
}
export interface DocumentBounds {
minX: number;
minY: number;
maxX: number;
maxY: number;
}
export interface LayerTreeNode {
layer: LayerItem;
children: LayerTreeNode[];
}
export interface TreeNodeRenderGroup {
baseNode: LayerTreeNode;
clippedNodes: LayerTreeNode[];
}
export interface LayerRenderGroup {
baseLayer: LayerItem;
clippedLayers: LayerItem[];
}
/**
* Builds a top-to-bottom hierarchical tree from InkStudio's flat top-to-bottom `layers` array.
*/
export function buildLayerTree(layers: LayerItem[]): LayerTreeNode[] {
const nodeMap = new Map<string, LayerTreeNode>();
for (const layer of layers) {
nodeMap.set(layer.id, { layer, children: [] });
}
const rootNodes: LayerTreeNode[] = [];
for (const layer of layers) {
const node = nodeMap.get(layer.id)!;
if (layer.parentId && nodeMap.has(layer.parentId)) {
nodeMap.get(layer.parentId)!.children.push(node);
} else {
rootNodes.push(node);
}
}
return rootNodes;
}
/**
* Groups consecutive clipped sibling nodes (`clipped: true`) under their immediate
* non-clipped base node underneath. Expects bottom-to-top rendering order.
*/
export function groupTreeNodesForCompositing(
nodesBottomToTop: LayerTreeNode[]
): TreeNodeRenderGroup[] {
const groups: TreeNodeRenderGroup[] = [];
let currentGroup: TreeNodeRenderGroup | null = null;
for (const node of nodesBottomToTop) {
if (node.layer.clipped && currentGroup) {
currentGroup.clippedNodes.push(node);
} else {
currentGroup = {
baseNode: node,
clippedNodes: [],
};
groups.push(currentGroup);
}
}
return groups;
}
/**
* Groups consecutive clipped layers under their immediate non-clipped base layer underneath.
* Expects bottom-to-top rendering order.
*/
export function groupLayersForCompositing(layersBottomToTop: LayerItem[]): LayerRenderGroup[] {
const groups: LayerRenderGroup[] = [];
let currentGroup: LayerRenderGroup | null = null;
for (const layer of layersBottomToTop) {
if (layer.clipped && currentGroup) {
currentGroup.clippedLayers.push(layer);
} else {
currentGroup = {
baseLayer: layer,
clippedLayers: [],
};
groups.push(currentGroup);
}
}
return groups;
}
/**
* Calculates the document-space AABB visible in the current camera viewport (frustum culling).
*/
export function calculateVisibleDocumentBounds(
viewportW: number,
viewportH: number,
transform: ViewportTransform,
docW: number,
docH: number
): DocumentBounds {
const { pan, zoom, rotation, flipX = false, flipY = false } = transform;
const rad = (-rotation * Math.PI) / 180;
const cosR = Math.cos(rad);
const sinR = Math.sin(rad);
const fx = flipX ? -1 : 1;
const fy = flipY ? -1 : 1;
const corners = [
{ x: 0, y: 0 },
{ x: viewportW, y: 0 },
{ x: viewportW, y: viewportH },
{ x: 0, y: viewportH },
];
let minX = Infinity;
let minY = Infinity;
let maxX = -Infinity;
let maxY = -Infinity;
for (const c of corners) {
const dx = c.x - pan.x;
const dy = c.y - pan.y;
const sx = dx / Math.max(0.001, zoom);
const sy = dy / Math.max(0.001, zoom);
const rx = sx * cosR - sy * sinR;
const ry = sx * sinR + sy * cosR;
const docX = rx * fx + docW * 0.5;
const docY = ry * fy + docH * 0.5;
if (docX < minX) minX = docX;
if (docY < minY) minY = docY;
if (docX > maxX) maxX = docX;
if (docY > maxY) maxY = docY;
}
return {
minX: Math.max(0, Math.floor(minX)),
minY: Math.max(0, Math.floor(minY)),
maxX: Math.min(docW, Math.ceil(maxX)),
maxY: Math.min(docH, Math.ceil(maxY)),
};
}