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 React, { useState, useEffect, useRef } from 'react';
import { X } from 'lucide-react';
import { HalftoneDotShape, applyScreentoneCPU } from '../utils/screentoneEngine';
import { isImeComposing, useI18n } from '../i18n';
export interface SimpleToneSettings {
density: number; // 1 to 100
densityType: 'specified' | 'image';
frequency: number; // 20 to 85
angle: number; // 0 to 90
dotShape: HalftoneDotShape;
}
interface SimpleToneModalProps {
isOpen: boolean;
onClose: () => void;
onConfirm: (settings: SimpleToneSettings) => void;
initialFrequency?: number;
initialDensity?: number;
initialAngle?: number;
initialDotShape?: HalftoneDotShape;
initialDensityType?: 'specified' | 'image';
dpi?: number;
}
const FREQUENCY_PRESETS = [28, 42.5, 50, 60, 70, 85];
const DENSITY_PRESETS = [5, 10, 20, 30, 40, 50, 60, 70, 80];
const ANGLE_PRESETS = [0, 30, 45, 60, 90];
export const SimpleToneModal: React.FC<SimpleToneModalProps> = ({
isOpen,
onClose,
onConfirm,
initialFrequency = 60,
initialDensity = 40,
initialAngle = 45,
initialDotShape = 'circle',
dpi = 600,
}) => {
const { t } = useI18n();
const [frequency, setFrequency] = useState(initialFrequency);
const [density, setDensity] = useState(initialDensity);
const [angle, setAngle] = useState(initialAngle);
const [dotShape, setDotShape] = useState<HalftoneDotShape>(initialDotShape);
const previewCanvasRef = useRef<HTMLCanvasElement>(null);
// Synchronize state when modal opens
useEffect(() => {
if (isOpen) {
setFrequency(initialFrequency);
setDensity(initialDensity);
setAngle(initialAngle);
setDotShape(initialDotShape);
}
}, [isOpen, initialFrequency, initialDensity, initialAngle, initialDotShape]);
// Live Swatch Preview renderer
useEffect(() => {
if (!isOpen) return;
const canvas = previewCanvasRef.current;
if (!canvas) return;
const w = 120;
const h = 120;
canvas.width = w;
canvas.height = h;
// Create an offscreen source canvas filled with solid black (mask)
const src = document.createElement('canvas');
src.width = w;
src.height = h;
const sCtx = src.getContext('2d');
if (!sCtx) return;
sCtx.fillStyle = '#000000';
sCtx.fillRect(0, 0, w, h);
applyScreentoneCPU(src, canvas, {
frequency,
angle,
dotShape,
dpi,
densityType: 'specified',
specifiedDensity: density / 100,
antiAliasing: false,
});
}, [isOpen, frequency, density, angle, dotShape, dpi]);
// Keyboard navigation (Enter = Confirm, Escape = Close)
useEffect(() => {
if (!isOpen) return;
const handleKeyDown = (e: KeyboardEvent) => {
if (isImeComposing(e)) return;
if (e.key === 'Escape') {
e.preventDefault();
onClose();
} else if (e.key === 'Enter') {
e.preventDefault();
onConfirm({
frequency,
density,
angle,
dotShape,
densityType: 'specified',
});
}
};
window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
}, [isOpen, onClose, onConfirm, frequency, density, angle, dotShape]);
if (!isOpen) return null;
return (
<div
role="dialog"
aria-modal="true"
aria-labelledby="simple-tone-modal-title"
className="fixed inset-0 z-50 flex items-center justify-center bg-ui-overlay p-4 select-none"
onPointerDown={(e) => e.stopPropagation()}
onClick={onClose}
>
<div
onClick={(e) => e.stopPropagation()}
className="bg-ui-panel border border-ui-default rounded-ui-lg shadow-ui-popover w-96 max-w-[95vw] flex flex-col overflow-hidden text-ui-primary"
>
{/* Header */}
<div className="h-10 px-4 bg-ui-header border-b border-ui-subtle flex items-center justify-between">
<h2 id="simple-tone-modal-title" className="text-xs font-medium text-ui-primary">
{t('Simple Tone Settings')}
</h2>
<button
type="button"
onClick={onClose}
aria-label={t('Close')}
className="w-6 h-6 flex items-center justify-center text-ui-muted hover:text-ui-primary rounded-ui-sm hover:bg-ui-control-hover transition-colors cursor-pointer"
title={t('Cancel (Esc)')}
>
<X className="w-3.5 h-3.5" />
</button>
</div>
{/* Body */}
<div className="p-4 space-y-4 text-xs">
{/* Top Row: Settings + Swatch Preview */}
<div className="flex gap-4">
{/* Swatch Preview */}
<div className="flex flex-col items-center">
<span className="text-[10px] text-ui-muted mb-1">{t('Preview')}</span>
<div className="w-[120px] h-[120px] rounded-ui-sm border border-ui-default bg-white overflow-hidden flex items-center justify-center">
<canvas ref={previewCanvasRef} className="w-full h-full" style={{ imageRendering: 'pixelated' }} />
</div>
<span className="text-[9px] text-ui-muted font-ui-mono tabular-nums mt-1">
{frequency}L · {density}%
</span>
</div>
{/* Quick Parameters */}
<div className="flex-1 space-y-3">
{/* Dot Shape */}
<div>
<label className="text-[10px] text-ui-muted block mb-1">{t('Dot Shape')}</label>
<select
value={dotShape}
onChange={(e) => setDotShape(e.target.value as HalftoneDotShape)}
className="w-full bg-ui-input border border-ui-default text-ui-primary text-xs rounded-ui-sm px-2 py-1 cursor-pointer"
>
<option value="circle">{t('Circle (Standard)')}</option>
<option value="euclidean">{t('Euclidean (Halftone)')}</option>
<option value="line">{t('Line (Hatching)')}</option>
<option value="diamond">{t('Diamond')}</option>
</select>
</div>
{/* Angle */}
<div>
<div className="flex items-center justify-between text-[10px] mb-1">
<span className="text-ui-muted">{t('Angle')}</span>
<span className="font-ui-mono tabular-nums text-ui-primary">{angle}°</span>
</div>
<input
type="range"
min="0"
max="90"
step="1"
value={angle}
onChange={(e) => setAngle(parseInt(e.target.value, 10))}
className="w-full"
/>
<div className="flex items-center justify-between text-[9px] text-ui-muted font-ui-mono tabular-nums mt-0.5">
{ANGLE_PRESETS.map((p) => (
<button
key={p}
type="button"
onClick={() => setAngle(p)}
className={`hover:text-ui-primary transition-colors cursor-pointer ${
angle === p ? 'text-ui-accent font-semibold' : ''
}`}
>
{p}°
</button>
))}
</div>
</div>
</div>
</div>
{/* Density Slider & Presets */}
<div className="p-2.5 bg-ui-header rounded-ui-md border border-ui-subtle">
<div className="flex items-center justify-between text-xs mb-1.5">
<span className="text-ui-secondary">{t('Density')}</span>
<div className="flex items-center space-x-1">
<input
type="number"
min="1"
max="100"
value={density}
onChange={(e) => {
const v = parseInt(e.target.value, 10);
if (!isNaN(v)) setDensity(Math.max(1, Math.min(100, v)));
}}
className="w-12 bg-ui-input border border-ui-default text-ui-primary font-ui-mono tabular-nums text-xs rounded-ui-sm px-1.5 py-0.5 text-right"
/>
<span className="text-xs text-ui-muted font-ui-mono">%</span>
</div>
</div>
<input
type="range"
min="1"
max="100"
step="1"
value={density}
onChange={(e) => setDensity(parseInt(e.target.value, 10))}
className="w-full mb-1.5"
/>
<div className="flex items-center justify-between text-[9px] text-ui-muted font-ui-mono tabular-nums">
{DENSITY_PRESETS.map((p) => (
<button
key={p}
type="button"
onClick={() => setDensity(p)}
className={`hover:text-ui-primary px-1 py-0.5 rounded-ui-xs transition-colors cursor-pointer ${
density === p ? 'bg-ui-accent-subtle text-ui-accent font-semibold' : ''
}`}
>
{p}%
</button>
))}
</div>
</div>
{/* Frequency (Lines / LPI) */}
<div className="p-2.5 bg-ui-header rounded-ui-md border border-ui-subtle">
<div className="flex items-center justify-between text-xs mb-1.5">
<span className="text-ui-secondary">{t('Screen Frequency (LPI)')}</span>
<div className="flex items-center space-x-1">
<input
type="number"
min="20"
max="85"
step="0.5"
value={frequency}
onChange={(e) => {
const v = parseFloat(e.target.value);
if (!isNaN(v)) setFrequency(Math.max(20, Math.min(85, v)));
}}
className="w-12 bg-ui-input border border-ui-default text-ui-primary font-ui-mono tabular-nums text-xs rounded-ui-sm px-1.5 py-0.5 text-right"
/>
<span className="text-xs text-ui-muted font-ui-mono">L</span>
</div>
</div>
<input
type="range"
min="20"
max="85"
step="1"
value={frequency}
onChange={(e) => setFrequency(parseFloat(e.target.value))}
className="w-full mb-1.5"
/>
<div className="flex items-center justify-between text-[9px] text-ui-muted font-ui-mono tabular-nums">
{FREQUENCY_PRESETS.map((p) => (
<button
key={p}
type="button"
onClick={() => setFrequency(p)}
className={`hover:text-ui-primary px-1 py-0.5 rounded-ui-xs transition-colors cursor-pointer ${
frequency === p ? 'bg-ui-accent-subtle text-ui-accent font-semibold' : ''
}`}
>
{p}
</button>
))}
</div>
</div>
</div>
{/* Footer */}
<div className="px-4 py-2.5 bg-ui-header border-t border-ui-subtle flex items-center justify-end gap-2">
<button
type="button"
onClick={onClose}
className="px-3.5 py-1.5 rounded-ui-sm bg-ui-control hover:bg-ui-control-hover border border-ui-default text-ui-primary font-medium transition-colors cursor-pointer text-xs"
>
{t('Cancel')}
</button>
<button
type="button"
onClick={() =>
onConfirm({
frequency,
density,
angle,
dotShape,
densityType: 'specified',
})
}
className="px-4 py-1.5 rounded-ui-sm bg-ui-accent text-ui-accent-contrast hover:opacity-90 font-medium transition-opacity cursor-pointer text-xs"
>
{t('OK')}
</button>
</div>
</div>
</div>
);
};