feat(editor): frame geometry for detail views drawn on the drawing
This commit is contained in:
1 parent
04360feb75
commit
93c68e6027
3 files changed
+430
-2
No files matched your search
@@ -32,9 +32,9 @@ export type PointProposal =
|
||||
const EPSILON = 1e-9
|
||||
|
||||
export const round2 = (value: number): number => Math.round(value * 100) / 100
|
||||
const floor2 = (value: number): number =>
|
||||
export const floor2 = (value: number): number =>
|
||||
Math.floor(value * 100 + EPSILON) / 100
|
||||
const ceil2 = (value: number): number => Math.ceil(value * 100 - EPSILON) / 100
|
||||
export const ceil2 = (value: number): number => Math.ceil(value * 100 - EPSILON) / 100
|
||||
|
||||
/**
|
||||
* F from the composed root's `viewBox` attribute TEXT (the property `baseVal` is held in
|
||||
|
||||
@@ -0,0 +1,244 @@
|
||||
import type { ViewBox } from '@faunosteo/core-wasm/types'
|
||||
|
||||
import type { Frame, XY } from './frame'
|
||||
import { ceil2, floor2, round2 } from './frame'
|
||||
|
||||
/** The eight handles of a frame (D8), clockwise from the top-left corner: their tab order. */
|
||||
export type FrameHandle = 'nw' | 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w'
|
||||
export type FramePart = FrameHandle | 'body'
|
||||
export const FRAME_HANDLES: readonly FrameHandle[] = [
|
||||
'nw',
|
||||
'n',
|
||||
'ne',
|
||||
'e',
|
||||
'se',
|
||||
's',
|
||||
'sw',
|
||||
'w',
|
||||
]
|
||||
|
||||
/** D6: the smallest side on screen while drawing and adjusting (44 px: a finger target). */
|
||||
export const MIN_FRAME_PX = { mouse: 16, pen: 16, touch: 44 } as const
|
||||
/** D6: a side shorter than this on screen hides its middle handle; the corners suffice. */
|
||||
export const SIDE_HANDLES_MIN_PX = 48
|
||||
|
||||
interface Edges {
|
||||
l: number
|
||||
t: number
|
||||
r: number
|
||||
b: number
|
||||
}
|
||||
|
||||
const edgesOf = (f: Frame): Edges => ({
|
||||
l: f.x,
|
||||
t: f.y,
|
||||
r: f.x + f.w,
|
||||
b: f.y + f.h,
|
||||
})
|
||||
const frameOf = (e: Edges): Frame => ({
|
||||
x: e.l,
|
||||
y: e.t,
|
||||
w: e.r - e.l,
|
||||
h: e.b - e.t,
|
||||
})
|
||||
|
||||
export function boxFromCorners(a: XY, b: XY): Frame {
|
||||
return {
|
||||
x: Math.min(a.x, b.x),
|
||||
y: Math.min(a.y, b.y),
|
||||
w: Math.abs(b.x - a.x),
|
||||
h: Math.abs(b.y - a.y),
|
||||
}
|
||||
}
|
||||
|
||||
export function intersection(a: Frame, b: Frame): Frame | null {
|
||||
const l = Math.max(a.x, b.x)
|
||||
const t = Math.max(a.y, b.y)
|
||||
const r = Math.min(a.x + a.w, b.x + b.w)
|
||||
const bottom = Math.min(a.y + a.h, b.y + b.h)
|
||||
return r > l && bottom > t ? { x: l, y: t, w: r - l, h: bottom - t } : null
|
||||
}
|
||||
|
||||
/** `box` moved inside `bounds`; it shrinks only when larger than them. */
|
||||
export function clampInto(box: Frame, bounds: Frame): Frame {
|
||||
const w = Math.min(box.w, bounds.w)
|
||||
const h = Math.min(box.h, bounds.h)
|
||||
const x = Math.min(Math.max(box.x, bounds.x), bounds.x + bounds.w - w)
|
||||
const y = Math.min(Math.max(box.y, bounds.y), bounds.y + bounds.h - h)
|
||||
return { x, y, w, h }
|
||||
}
|
||||
|
||||
/** D4: a third of the visible width at the drawing area's ratio (w / h), centred on `at`. */
|
||||
export function defaultBox(
|
||||
at: XY,
|
||||
visible: Frame,
|
||||
areaRatio: number,
|
||||
bounds: Frame,
|
||||
): Frame {
|
||||
const w = visible.w / 3
|
||||
const h = w / areaRatio
|
||||
const room = intersection(visible, bounds) ?? bounds
|
||||
return clampInto({ x: at.x - w / 2, y: at.y - h / 2, w, h }, room)
|
||||
}
|
||||
|
||||
/** D4, D6: the frame a drag draws, cut to `bounds`; one narrower or shorter than `min`
|
||||
* (drawing units) counts as a click: `fallback()`, the default frame there. */
|
||||
export function drawnBox(
|
||||
from: XY,
|
||||
to: XY,
|
||||
min: number,
|
||||
bounds: Frame,
|
||||
fallback: () => Frame,
|
||||
): Frame {
|
||||
const inside = intersection(boxFromCorners(from, to), bounds)
|
||||
return inside === null || inside.w < min || inside.h < min
|
||||
? fallback()
|
||||
: inside
|
||||
}
|
||||
|
||||
export function moveBox(box: Frame, delta: XY, bounds: Frame): Frame {
|
||||
return clampInto({ ...box, x: box.x + delta.x, y: box.y + delta.y }, bounds)
|
||||
}
|
||||
|
||||
export interface Resize {
|
||||
start: Frame
|
||||
handle: FrameHandle
|
||||
delta: XY
|
||||
/** The smallest side, in drawing units (`MIN_FRAME_PX` times the units per px). */
|
||||
min: number
|
||||
keepRatio: boolean
|
||||
bounds: Frame
|
||||
}
|
||||
|
||||
/** D8: `start` after dragging `handle` by `delta`: the opposite side or corner stays put, each
|
||||
* side keeps `min`, the box stays in `bounds`; `keepRatio` (Maj, « Garder les proportions »)
|
||||
* keeps the ratio of `start`. */
|
||||
export function resizeBox(input: Resize): Frame {
|
||||
const moved = moveEdges(input)
|
||||
return input.keepRatio ? frameOf(withRatio(moved, input)) : frameOf(moved)
|
||||
}
|
||||
|
||||
function moveEdges({ start, handle, delta, min, bounds }: Resize): Edges {
|
||||
const e = edgesOf(start)
|
||||
const lim = edgesOf(bounds)
|
||||
const next = { ...e }
|
||||
if (handle.includes('w'))
|
||||
next.l = Math.min(Math.max(e.l + delta.x, lim.l), e.r - min)
|
||||
if (handle.includes('e'))
|
||||
next.r = Math.max(Math.min(e.r + delta.x, lim.r), e.l + min)
|
||||
if (handle.includes('n'))
|
||||
next.t = Math.min(Math.max(e.t + delta.y, lim.t), e.b - min)
|
||||
if (handle.includes('s'))
|
||||
next.b = Math.max(Math.min(e.b + delta.y, lim.b), e.t + min)
|
||||
return next
|
||||
}
|
||||
|
||||
interface Size {
|
||||
w: number
|
||||
h: number
|
||||
}
|
||||
|
||||
function withRatio(
|
||||
moved: Edges,
|
||||
{ start, handle, min, bounds }: Resize,
|
||||
): Edges {
|
||||
const ratio = start.w / start.h
|
||||
const w = moved.r - moved.l
|
||||
const h = moved.b - moved.t
|
||||
const byWidth =
|
||||
handle === 'e' ||
|
||||
handle === 'w' ||
|
||||
(handle.length === 2 && w / start.w >= h / start.h)
|
||||
let size: Size = byWidth ? { w, h: w / ratio } : { w: h * ratio, h }
|
||||
const grow = Math.max(1, min / size.w, min / size.h)
|
||||
size = { w: size.w * grow, h: size.h * grow }
|
||||
const fit = fitScale(handle, edgesOf(start), size, edgesOf(bounds))
|
||||
return anchored(handle, edgesOf(start), { w: size.w * fit, h: size.h * fit })
|
||||
}
|
||||
|
||||
/** Scale (at most 1) that keeps the anchored box inside `lim`. */
|
||||
function fitScale(
|
||||
handle: FrameHandle,
|
||||
s: Edges,
|
||||
size: Size,
|
||||
lim: Edges,
|
||||
): number {
|
||||
const cx = (s.l + s.r) / 2
|
||||
const cy = (s.t + s.b) / 2
|
||||
const centredW = 2 * Math.min(cx - lim.l, lim.r - cx)
|
||||
const centredH = 2 * Math.min(cy - lim.t, lim.b - cy)
|
||||
const roomW = handle.includes('w')
|
||||
? s.r - lim.l
|
||||
: handle.includes('e')
|
||||
? lim.r - s.l
|
||||
: centredW
|
||||
const roomH = handle.includes('n')
|
||||
? s.b - lim.t
|
||||
: handle.includes('s')
|
||||
? lim.b - s.t
|
||||
: centredH
|
||||
return Math.min(1, roomW / size.w, roomH / size.h)
|
||||
}
|
||||
|
||||
/** The box of `size` held by the side or corner opposite `handle`, centred on the other axis. */
|
||||
function anchored(handle: FrameHandle, s: Edges, size: Size): Edges {
|
||||
const cx = (s.l + s.r) / 2
|
||||
const cy = (s.t + s.b) / 2
|
||||
const l = handle.includes('w')
|
||||
? s.r - size.w
|
||||
: handle.includes('e')
|
||||
? s.l
|
||||
: cx - size.w / 2
|
||||
const t = handle.includes('n')
|
||||
? s.b - size.h
|
||||
: handle.includes('s')
|
||||
? s.t
|
||||
: cy - size.h / 2
|
||||
return { l, t, r: l + size.w, b: t + size.h }
|
||||
}
|
||||
|
||||
/** D6: « 3:4 », « 7:1 »: the simplest p:q within 2 % of w / h with p or q from 1 to 4, else
|
||||
* one decimal against 1 (« 2,5:1 » in French). */
|
||||
export function ratioLabel(w: number, h: number, locale: 'fr' | 'en'): string {
|
||||
const r = w / h
|
||||
const near = (p: number, q: number) =>
|
||||
p >= 1 && q >= 1 && Math.abs(p / q - r) / r <= 0.02
|
||||
let best: [number, number] | null = null
|
||||
for (let k = 1; k <= 4; k++) {
|
||||
const pairs: [number, number][] = [
|
||||
[Math.round(r * k), k],
|
||||
[k, Math.round(k / r)],
|
||||
]
|
||||
for (const [p, q] of pairs) {
|
||||
if (
|
||||
near(p, q) &&
|
||||
(best === null || Math.max(p, q) < Math.max(best[0], best[1]))
|
||||
)
|
||||
best = [p, q]
|
||||
}
|
||||
}
|
||||
if (best !== null) return `${best[0]}:${best[1]}`
|
||||
const decimal = (n: number) =>
|
||||
n.toFixed(1).replace('.', locale === 'fr' ? ',' : '.')
|
||||
return r >= 1 ? `${decimal(r)}:1` : `1:${decimal(1 / r)}`
|
||||
}
|
||||
|
||||
/** D6: the handles shown for a frame of this size on screen. */
|
||||
export function shownHandles(widthPx: number, heightPx: number): FrameHandle[] {
|
||||
return FRAME_HANDLES.filter((handle) => {
|
||||
if (handle.length === 2) return true
|
||||
return handle === 'n' || handle === 's'
|
||||
? widthPx >= SIDE_HANDLES_MIN_PX
|
||||
: heightPx >= SIDE_HANDLES_MIN_PX
|
||||
})
|
||||
}
|
||||
|
||||
/** The frame as a `view_box`, rounded inward to 0.01, so a frame held against the parent's edge never leaves it
|
||||
* (the core's edge slack is 1e-9 of the span, `frame.rs:115-121`). */
|
||||
export function toViewBox(box: Frame): ViewBox {
|
||||
const l = ceil2(box.x)
|
||||
const t = ceil2(box.y)
|
||||
const width = round2(floor2(box.x + box.w) - l)
|
||||
const height = round2(floor2(box.y + box.h) - t)
|
||||
return { min_x: l, min_y: t, width, height }
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import {
|
||||
FRAME_HANDLES,
|
||||
boxFromCorners,
|
||||
clampInto,
|
||||
defaultBox,
|
||||
drawnBox,
|
||||
moveBox,
|
||||
ratioLabel,
|
||||
resizeBox,
|
||||
shownHandles,
|
||||
toViewBox,
|
||||
} from '../../src/editor/annotations/frameBox'
|
||||
|
||||
const BOUNDS = { x: 0, y: 0, w: 500, h: 500 }
|
||||
const START = { x: 100, y: 100, w: 200, h: 100 }
|
||||
const resize = (over: object) => ({
|
||||
start: START,
|
||||
handle: 'e' as const,
|
||||
delta: { x: 0, y: 0 },
|
||||
min: 20,
|
||||
keepRatio: false,
|
||||
bounds: BOUNDS,
|
||||
...over,
|
||||
})
|
||||
|
||||
describe('frameBox', () => {
|
||||
it('boxFromCorners orders the corners', () => {
|
||||
expect(boxFromCorners({ x: 10, y: 40 }, { x: 4, y: 2 })).toEqual({
|
||||
x: 4,
|
||||
y: 2,
|
||||
w: 6,
|
||||
h: 38,
|
||||
})
|
||||
})
|
||||
|
||||
it('defaultBox is a third of the visible width, centred on the point', () => {
|
||||
const visible = { x: 0, y: 0, w: 900, h: 675 }
|
||||
const bounds = { x: 0, y: 0, w: 2073.4, h: 1629.4 }
|
||||
expect(defaultBox({ x: 500, y: 400 }, visible, 4 / 3, bounds)).toEqual({
|
||||
x: 350,
|
||||
y: 287.5,
|
||||
w: 300,
|
||||
h: 225,
|
||||
})
|
||||
})
|
||||
|
||||
it('defaultBox near the corner stays inside the visible part and the bounds', () => {
|
||||
const visible = { x: 100, y: 100, w: 900, h: 675 }
|
||||
const bounds = { x: 0, y: 0, w: 2000, h: 1600 }
|
||||
expect(defaultBox({ x: 110, y: 110 }, visible, 4 / 3, bounds)).toEqual({
|
||||
x: 100,
|
||||
y: 100,
|
||||
w: 300,
|
||||
h: 225,
|
||||
})
|
||||
})
|
||||
|
||||
it('drawnBox with a small drag returns the fallback', () => {
|
||||
const fallback = { x: 1, y: 2, w: 3, h: 4 }
|
||||
const box = drawnBox(
|
||||
{ x: 0, y: 0 },
|
||||
{ x: 10, y: 10 },
|
||||
20,
|
||||
BOUNDS,
|
||||
() => fallback,
|
||||
)
|
||||
expect(box).toBe(fallback)
|
||||
})
|
||||
|
||||
it('drawnBox cuts a drag partly outside to the bounds', () => {
|
||||
const box = drawnBox(
|
||||
{ x: -30, y: -20 },
|
||||
{ x: 70, y: 60 },
|
||||
20,
|
||||
BOUNDS,
|
||||
() => START,
|
||||
)
|
||||
expect(box).toEqual({ x: 0, y: 0, w: 70, h: 60 })
|
||||
})
|
||||
|
||||
it('resizeBox on e moves only the right edge, stopping at bounds and min', () => {
|
||||
expect(resizeBox(resize({ delta: { x: 50, y: 30 } }))).toEqual({
|
||||
x: 100,
|
||||
y: 100,
|
||||
w: 250,
|
||||
h: 100,
|
||||
})
|
||||
expect(resizeBox(resize({ delta: { x: 1000, y: 0 } }))).toEqual({
|
||||
x: 100,
|
||||
y: 100,
|
||||
w: 400,
|
||||
h: 100,
|
||||
})
|
||||
expect(resizeBox(resize({ delta: { x: -1000, y: 0 } }))).toEqual({
|
||||
x: 100,
|
||||
y: 100,
|
||||
w: 20,
|
||||
h: 100,
|
||||
})
|
||||
})
|
||||
|
||||
it('resizeBox on nw keeps the bottom-right corner', () => {
|
||||
expect(
|
||||
resizeBox(resize({ handle: 'nw', delta: { x: 30, y: 20 } })),
|
||||
).toEqual({ x: 130, y: 120, w: 170, h: 80 })
|
||||
})
|
||||
|
||||
it('resizeBox keepRatio keeps the ratio on se and the centre on e', () => {
|
||||
expect(
|
||||
resizeBox(
|
||||
resize({ handle: 'se', delta: { x: 100, y: 10 }, keepRatio: true }),
|
||||
),
|
||||
).toEqual({ x: 100, y: 100, w: 300, h: 150 })
|
||||
expect(
|
||||
resizeBox(
|
||||
resize({ handle: 'e', delta: { x: 100, y: 0 }, keepRatio: true }),
|
||||
),
|
||||
).toEqual({ x: 100, y: 75, w: 300, h: 150 })
|
||||
})
|
||||
|
||||
it('resizeBox keepRatio shrinks inside the bounds and keeps the ratio', () => {
|
||||
const box = resizeBox(
|
||||
resize({
|
||||
handle: 'se',
|
||||
delta: { x: 500, y: 500 },
|
||||
keepRatio: true,
|
||||
bounds: { x: 0, y: 0, w: 400, h: 300 },
|
||||
}),
|
||||
)
|
||||
expect(box).toEqual({ x: 100, y: 100, w: 300, h: 150 })
|
||||
expect(box.w / box.h).toBe(2)
|
||||
})
|
||||
|
||||
it('moveBox clamps into the bounds and never resizes', () => {
|
||||
expect(
|
||||
moveBox({ x: 10, y: 10, w: 50, h: 40 }, { x: -100, y: 1000 }, BOUNDS),
|
||||
).toEqual({ x: 0, y: 460, w: 50, h: 40 })
|
||||
expect(clampInto({ x: 0, y: 0, w: 900, h: 10 }, BOUNDS).w).toBe(500)
|
||||
})
|
||||
|
||||
it('ratioLabel names the simple ratios', () => {
|
||||
expect(ratioLabel(300, 400, 'fr')).toBe('3:4')
|
||||
expect(ratioLabel(7, 1, 'fr')).toBe('7:1')
|
||||
expect(ratioLabel(220, 660, 'fr')).toBe('1:3')
|
||||
expect(ratioLabel(500, 380, 'fr')).toBe('4:3')
|
||||
expect(ratioLabel(980, 140, 'fr')).toBe('7:1')
|
||||
})
|
||||
|
||||
it('ratioLabel falls back to a decimal for extreme ratios', () => {
|
||||
expect(ratioLabel(100, 1, 'fr')).toBe('100:1')
|
||||
expect(ratioLabel(1, 100, 'fr')).toBe('1:100')
|
||||
})
|
||||
|
||||
it('ratioLabel accepts a ratio within 2 %', () => {
|
||||
expect(ratioLabel(2.718, 1, 'fr')).toBe('8:3')
|
||||
})
|
||||
|
||||
it('ratioLabel writes the decimal in the locale', () => {
|
||||
expect(ratioLabel(2.45, 1, 'fr')).toBe('2,5:1')
|
||||
expect(ratioLabel(2.45, 1, 'en')).toBe('2.5:1')
|
||||
})
|
||||
|
||||
it('shownHandles hides n and s on a narrow frame', () => {
|
||||
expect(FRAME_HANDLES).toHaveLength(8)
|
||||
expect(shownHandles(40, 200)).toEqual(['nw', 'ne', 'e', 'se', 'sw', 'w'])
|
||||
})
|
||||
|
||||
it('toViewBox rounds inward to 0.01', () => {
|
||||
expect(toViewBox({ x: 0.001, y: 0.004, w: 10, h: 5 })).toEqual({
|
||||
min_x: 0.01,
|
||||
min_y: 0.01,
|
||||
width: 9.99,
|
||||
height: 4.99,
|
||||
})
|
||||
expect(toViewBox({ x: 1.25, y: 2.5, w: 3.5, h: 4 })).toEqual({
|
||||
min_x: 1.25,
|
||||
min_y: 2.5,
|
||||
width: 3.5,
|
||||
height: 4,
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user