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
/**
* Lightweight Pure-TS Selection Metadata & Hit-Test Utilities (Phase 6 Native Rust Engine).
*
* All sparse `R8Unorm` selection masks, Magic Wand, Select Pen/Eraser, expand/contract/feather,
* clipboard cut/copy/paste, floating pixels, and animated GPU marching ants execute natively
* inside `crates/ink-engine/src/selection/` with ZERO WebView2 `<canvas>` backing stores.
*/
import type { CanvasSelection } from '../types/editor';
import type { NativeSelectionMetaSnapshot } from './nativeEngineBridge';
export interface SelectionTileSnapshot {
format: 'monochrome-1bit' | 'grayscale-8bit' | 'color-32bit';
tiles: Array<{ tileX: number; tileY: number; format: 'monochrome-1bit' | 'grayscale-8bit' | 'color-32bit'; data: Uint8Array }>;
bounds: { x: number; y: number; width: number; height: number };
}
export function isPointInPolygon(
px: number,
py: number,
polygon: Array<{ x: number; y: number }>
): boolean {
if (polygon.length < 3) return false;
let inside = false;
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
const xi = polygon[i].x;
const yi = polygon[i].y;
const xj = polygon[j].x;
const yj = polygon[j].y;
const intersect = yi > py !== yj > py && px < ((xj - xi) * (py - yi)) / (yj - yi) + xi;
if (intersect) inside = !inside;
}
return inside;
}
export function isPointInSelection(
x: number,
y: number,
selection: CanvasSelection | null
): boolean {
if (!selection) return false;
if (selection.floating) {
const fx = selection.floating.x;
const fy = selection.floating.y;
const fw = selection.floating.width ?? selection.bounds.width;
const fh = selection.floating.height ?? selection.bounds.height;
return x >= fx && x <= fx + fw && y >= fy && y <= fy + fh;
}
const b = selection.bounds;
if (x < b.x || x > b.x + b.width || y < b.y || y > b.y + b.height) {
return false;
}
if (selection.type === 'lasso' && selection.polygon) {
return isPointInPolygon(x, y, selection.polygon);
}
return true;
}
export function computePolygonBounds(
polygon: Array<{ x: number; y: number }>
): { x: number; y: number; width: number; height: number } {
if (polygon.length === 0) return { x: 0, y: 0, width: 0, height: 0 };
let minX = polygon[0].x;
let maxX = polygon[0].x;
let minY = polygon[0].y;
let maxY = polygon[0].y;
for (let i = 1; i < polygon.length; i++) {
const p = polygon[i];
if (p.x < minX) minX = p.x;
if (p.x > maxX) maxX = p.x;
if (p.y < minY) minY = p.y;
if (p.y > maxY) maxY = p.y;
}
return {
x: Math.floor(minX),
y: Math.floor(minY),
width: Math.max(1, Math.ceil(maxX - minX)),
height: Math.max(1, Math.ceil(maxY - minY)),
};
}
export function selectionMetaToCanvasSelection(
meta: NativeSelectionMetaSnapshot | null | undefined
): CanvasSelection | null {
if (!meta || !meta.active || meta.bounds.width <= 0 || meta.bounds.height <= 0) {
return null;
}
return {
type: 'mask',
bounds: {
x: meta.bounds.x,
y: meta.bounds.y,
width: meta.bounds.width,
height: meta.bounds.height,
},
nativeVersion: meta.version,
floating: meta.hasFloating
? {
x: meta.floatingX ?? meta.bounds.x,
y: meta.floatingY ?? meta.bounds.y,
width: meta.floatingWidth ?? meta.bounds.width,
height: meta.floatingHeight ?? meta.bounds.height,
origX: meta.floatingX ?? meta.bounds.x,
origY: meta.floatingY ?? meta.bounds.y,
layerId: meta.floatingLayerId ?? '',
}
: null,
};
}
export function cloneSelectionForHistory(
selection: CanvasSelection | null
): CanvasSelection | null {
if (!selection) return null;
return {
type: selection.type,
bounds: { ...selection.bounds },
polygon: selection.polygon ? selection.polygon.map((p) => ({ x: p.x, y: p.y })) : undefined,
nativeVersion: selection.nativeVersion,
floating: null,
};
}
export function areSelectionsEqualForHistory(
a: CanvasSelection | null,
b: CanvasSelection | null
): boolean {
if (a === b) return true;
if (!a || !b) return false;
return (
a.type === b.type &&
a.nativeVersion === b.nativeVersion &&
a.bounds.x === b.bounds.x &&
a.bounds.y === b.bounds.y &&
a.bounds.width === b.bounds.width &&
a.bounds.height === b.bounds.height
);
}