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 { describe, it, expect, beforeEach } from 'vitest';
import {
computeEffectiveCursorAngleDeg,
computeNextDynamicCursorAngleDeg,
formatCursorRingTransform,
normalizeAngleDeg,
resolveBrushCursorRotationSpec,
} from '../../components/canvasArea/types';
import { BUILTIN_BRUSHES, getBuiltinBrushConfig } from '../presetBrushes';
import {
getEffectiveBrushSettings,
resetBrushSettingsCache,
saveBrushSettingsImmediately,
} from '../brushPresetManager';
import {
buildExportableBrush,
parseInkbFile,
serializeInkbBrush,
} from '../inkbBrushFormat';
import type { BrushSettings, CspBrush } from '../../types/editor';
function createMemoryStorage(): Storage {
const store = new Map<string, string>();
return {
get length() {
return store.size;
},
clear() {
store.clear();
},
getItem(key: string) {
return store.has(key) ? store.get(key)! : null;
},
key(index: number) {
return Array.from(store.keys())[index] ?? null;
},
removeItem(key: string) {
store.delete(key);
},
setItem(key: string, value: string) {
store.set(key, String(value));
},
};
}
function toArrayBuffer(bytes: Uint8Array): ArrayBuffer {
return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength) as ArrayBuffer;
}
describe('Rotating Brush Cursor & "This brush rotates" Setting', () => {
beforeEach(() => {
(globalThis as any).localStorage = createMemoryStorage();
resetBrushSettingsCache();
});
it('auto-enables rotating cursor for bristle-ribbon and directional stamp brushes while keeping fixed pens non-rotating', () => {
// 1. Natural Paint bristle-ribbon / bristle-hybrid configs rotate with +90° perpendicular alignment
expect(
resolveBrushCursorRotationSpec({ renderMode: 'bristle-ribbon' }, 'brush')
).toEqual({
enabled: true,
source: 'direction',
orientationOffsetDeg: 90,
});
expect(
resolveBrushCursorRotationSpec({ renderMode: 'bristle-hybrid' }, 'brush')
).toEqual({
enabled: true,
source: 'direction',
orientationOffsetDeg: 90,
});
// 2. Directional stamp brushes rotate with 0° offset
expect(
resolveBrushCursorRotationSpec({ customTipAngleDynamics: 'direction' }, 'brush')
).toEqual({
enabled: true,
source: 'direction',
orientationOffsetDeg: 0,
});
// 3. Fixed / non-directional built-in pen presets do NOT auto-rotate
expect(BUILTIN_BRUSHES.map((b) => b.id)).toEqual([
'g-pen',
'real-g-pen',
'textured-pen',
'rough-pen',
'dot-pen',
'calligraphy',
]);
for (const id of ['g-pen', 'real-g-pen', 'textured-pen', 'dot-pen', 'calligraphy']) {
const cfg = getBuiltinBrushConfig(id)!;
expect(resolveBrushCursorRotationSpec(cfg, 'brush').enabled, `${id} should not auto-rotate`).toBe(
false
);
}
});
it('auto-detects initial-direction, pen tilt, and barrel twist angle dynamics', () => {
expect(
resolveBrushCursorRotationSpec({ customTipAngleDynamics: 'initial-direction' }, 'brush')
).toEqual({
enabled: true,
source: 'direction',
orientationOffsetDeg: 0,
});
expect(resolveBrushCursorRotationSpec({ customTipAngleDynamics: 'tilt' }, 'brush')).toEqual({
enabled: true,
source: 'tilt',
orientationOffsetDeg: 0,
});
expect(resolveBrushCursorRotationSpec({ customTipAngleDynamics: 'twist' }, 'brush')).toEqual({
enabled: true,
source: 'twist',
orientationOffsetDeg: 0,
});
});
it('respects explicit per-brush rotateCursor ("This brush rotates") overrides and tool gating', () => {
const natRake: Partial<BrushSettings> = { renderMode: 'bristle-ribbon' };
expect(resolveBrushCursorRotationSpec({ ...natRake, rotateCursor: false }, 'brush')).toEqual({
enabled: false,
source: 'none',
orientationOffsetDeg: 0,
});
const gPen = getBuiltinBrushConfig('g-pen')!;
expect(resolveBrushCursorRotationSpec({ ...gPen, rotateCursor: true }, 'brush')).toEqual({
enabled: true,
source: 'direction',
orientationOffsetDeg: 0,
});
expect(resolveBrushCursorRotationSpec({ ...gPen, rotateCursor: true }, 'eraser')).toEqual({
enabled: true,
source: 'direction',
orientationOffsetDeg: 0,
});
// Non-brush/eraser tools never activate brush cursor rotation
expect(resolveBrushCursorRotationSpec({ ...natRake, rotateCursor: true }, 'fill').enabled).toBe(
false
);
expect(resolveBrushCursorRotationSpec({ ...natRake, rotateCursor: true }, 'liquify').enabled).toBe(
false
);
});
it('aligns Natural Paint rake teeth perpendicular to stroke direction across rightward, downward, rotated, and flipped viewports', () => {
// Moving right (0°): horizontal rake tip must rotate to 90° (vertical row of teeth across rightward stroke)
const moveRight = computeNextDynamicCursorAngleDeg({
source: 'direction',
currentDeg: 0,
hasCurrent: false,
sdx: 15,
sdy: 0,
canvasRotationDeg: 0,
flipX: false,
flipY: false,
});
expect(moveRight.updated).toBe(true);
expect(moveRight.angleDeg).toBeCloseTo(0, 4);
expect(
computeEffectiveCursorAngleDeg({
tipAngleDeg: 0,
orientationOffsetDeg: 90,
followDirection: true,
dynamicDirDeg: moveRight.angleDeg,
})
).toBeCloseTo(90, 4);
// Moving down (+90°): horizontal rake tip rotates to 180° (horizontal row of teeth across downward stroke)
const moveDown = computeNextDynamicCursorAngleDeg({
source: 'direction',
currentDeg: 0,
hasCurrent: false,
sdx: 0,
sdy: 15,
canvasRotationDeg: 0,
flipX: false,
flipY: false,
});
expect(moveDown.updated).toBe(true);
expect(moveDown.angleDeg).toBeCloseTo(90, 4);
expect(
computeEffectiveCursorAngleDeg({
tipAngleDeg: 0,
orientationOffsetDeg: 90,
followDirection: true,
dynamicDirDeg: moveDown.angleDeg,
})
).toBeCloseTo(180, 4);
// Viewport rotated 45° and user moves right on screen (0° screen -> -45° canvas)
const rotatedMove = computeNextDynamicCursorAngleDeg({
source: 'direction',
currentDeg: 0,
hasCurrent: false,
sdx: 20,
sdy: 0,
canvasRotationDeg: 45,
flipX: false,
flipY: false,
});
expect(rotatedMove.angleDeg).toBeCloseTo(-45, 4);
// Horizontally flipped viewport (flipX = true) and user moves up-right (+10, -10) -> canvas (-10, -10) = -135°
const flippedMove = computeNextDynamicCursorAngleDeg({
source: 'direction',
currentDeg: 0,
hasCurrent: false,
sdx: 10,
sdy: -10,
canvasRotationDeg: 0,
flipX: true,
flipY: false,
});
expect(flippedMove.angleDeg).toBeCloseTo(-135, 4);
const transform = formatCursorRingTransform(
100,
100,
0,
true,
false,
computeEffectiveCursorAngleDeg({
tipAngleDeg: 0,
orientationOffsetDeg: 90,
followDirection: true,
dynamicDirDeg: flippedMove.angleDeg,
})
);
expect(transform).toContain('scale(-1, 1) rotate(-45deg)');
});
it('rejects tiny sub-3px jitter, wraps cleanly across +-180deg, and supports tilt & twist inputs', () => {
const jitter = computeNextDynamicCursorAngleDeg({
source: 'direction',
currentDeg: 42,
hasCurrent: true,
sdx: 1.5,
sdy: -1.5,
canvasRotationDeg: 0,
flipX: false,
flipY: false,
});
expect(jitter.updated).toBe(false);
expect(jitter.angleDeg).toBe(42);
// Wrap across +-180°: from +170° toward -170° should step positively past 180°, never jump backward through 0°
const wrap = computeNextDynamicCursorAngleDeg({
source: 'direction',
currentDeg: 170,
hasCurrent: true,
sdx: Math.cos((-170 * Math.PI) / 180) * 10,
sdy: Math.sin((-170 * Math.PI) / 180) * 10,
canvasRotationDeg: 0,
flipX: false,
flipY: false,
});
expect(wrap.updated).toBe(true);
expect(Math.abs(normalizeAngleDeg(wrap.angleDeg - 180))).toBeLessThan(15);
// Pen tilt azimuth matches BrushEngine: atan2(tiltX, -tiltY)
const tilt = computeNextDynamicCursorAngleDeg({
source: 'tilt',
currentDeg: 0,
hasCurrent: false,
sdx: 0,
sdy: 0,
canvasRotationDeg: 0,
flipX: false,
flipY: false,
tiltX: 25,
tiltY: 0,
});
expect(tilt.updated).toBe(true);
expect(tilt.angleDeg).toBeCloseTo(90, 4);
// Barrel twist
const twist = computeNextDynamicCursorAngleDeg({
source: 'twist',
currentDeg: 0,
hasCurrent: false,
sdx: 0,
sdy: 0,
canvasRotationDeg: 0,
flipX: false,
flipY: false,
twist: 215,
});
expect(twist.updated).toBe(true);
expect(twist.angleDeg).toBeCloseTo(-145, 4);
});
it('persists rotateCursor per brush, clears manual override back to auto, and round-trips through .inkb', () => {
const customRake: CspBrush = {
id: 'inkb-custom-rake',
name: 'Custom Rake',
size: 24,
minSizeFactor: 0.2,
flow: 1,
opacity: 1,
spacing: 0.1,
hardness: 1,
thickness: 1,
angle: 0,
smoothing: true,
stabilization: 0,
usePressure: true,
useVelocity: false,
renderMode: 'bristle-ribbon',
importedAt: Date.now(),
};
saveBrushSettingsImmediately(customRake.id, { rotateCursor: false });
let eff = getEffectiveBrushSettings(customRake.id, [customRake]);
expect(eff.rotateCursor).toBe(false);
expect(resolveBrushCursorRotationSpec(eff, 'brush').enabled).toBe(false);
// Clearing override with explicit undefined returns to auto-detected rotating state
saveBrushSettingsImmediately(customRake.id, { rotateCursor: undefined });
eff = getEffectiveBrushSettings(customRake.id, [customRake]);
expect(eff.rotateCursor).toBeUndefined();
expect(resolveBrushCursorRotationSpec(eff, 'brush').enabled).toBe(true);
// Enabling on a fixed brush persists and survives .inkb export/import round-trip
saveBrushSettingsImmediately('g-pen', { rotateCursor: true });
const exported: CspBrush = buildExportableBrush('g-pen', [], {
preset: 'g-pen',
rotateCursor: true,
} as Partial<BrushSettings>);
expect(exported.rotateCursor).toBe(true);
const bytes = serializeInkbBrush(exported);
const parsed = parseInkbFile(toArrayBuffer(bytes), 'G-Pen.inkb');
expect(parsed.rotateCursor).toBe(true);
});
});