diff --git a/src/ui/photo.ts b/src/ui/photo.ts index 68c0c2c..41eaf61 100644 --- a/src/ui/photo.ts +++ b/src/ui/photo.ts @@ -17,11 +17,13 @@ export const COMPOSITION_DEFAUT: CompositionPhoto = { x: 0, y: 0, scale: 1, corn const borne = (valeur: number, min: number, max: number) => Math.min(max, Math.max(min, Number.isFinite(valeur) ? valeur : min)) +const arrondi = (valeur: number, decimales = 1) => Number(valeur.toFixed(decimales)) + export const normaliseComposition = (c: CompositionPhoto): CompositionPhoto => ({ - x: borne(c.x, -180, 180), - y: borne(c.y, -180, 180), - scale: borne(c.scale, 0.4, 3), - cornerRadius: borne(c.cornerRadius, 0, 50) + x: arrondi(borne(c.x, -180, 180)), + y: arrondi(borne(c.y, -180, 180)), + scale: arrondi(borne(c.scale, 0.4, 3), 3), + cornerRadius: arrondi(borne(c.cornerRadius, 0, 50)) }) /** Pourcent (0-50) vers unites viewBox du cadre 300. */ diff --git a/src/ui/pose/model.ts b/src/ui/pose/model.ts index 5cd0ad4..38d6a23 100644 --- a/src/ui/pose/model.ts +++ b/src/ui/pose/model.ts @@ -60,42 +60,46 @@ export const defaultManual = (): ManualState => ({ const num = (value: unknown, fallback: number) => typeof value === 'number' && Number.isFinite(value) ? value : fallback +const arrondi = (value: number, decimales = 1) => Number(value.toFixed(decimales)) + const eyeOf = (value: unknown, fallback: EyeSide): EyeSide => { const raw = value && typeof value === 'object' ? (value as Record) : {} const width = num(raw.width, fallback.width) const height = num(raw.height, fallback.height) return { - width: clamp(width <= 5 ? width * 20 : width, 10, 110), - height: clamp(height <= 5 ? height * 50 : height, 10, 110), - size: clamp(num(raw.size, fallback.size), 0.35, 2.2), - angle: num(raw.angle, fallback.angle), - x: clamp(num(raw.x, fallback.x), -48, 48), - y: clamp(num(raw.y, fallback.y), -48, 48) + width: arrondi(clamp(width <= 5 ? width * 20 : width, 10, 110)), + height: arrondi(clamp(height <= 5 ? height * 50 : height, 10, 110)), + size: arrondi(clamp(num(raw.size, fallback.size), 0.35, 2.2), 2), + angle: arrondi(num(raw.angle, fallback.angle)), + x: arrondi(clamp(num(raw.x, fallback.x), -48, 48)), + y: arrondi(clamp(num(raw.y, fallback.y), -48, 48)) } } export const clampPose = (pose: HeadPose): HeadPose => ({ - turn: clamp(pose.turn, -365, 365), - tilt: clamp(pose.tilt, -365, 365), - roll: clamp(pose.roll, -365, 365) + turn: arrondi(clamp(pose.turn, -365, 365)), + tilt: arrondi(clamp(pose.tilt, -365, 365)), + roll: arrondi(clamp(pose.roll, -365, 365)) }) export const clampOffset = (offset: BodyOffset): BodyOffset => ({ - tx: clamp(offset.tx, -90, 90), - ty: clamp(offset.ty, -90, 90), - spin: clamp(offset.spin, -180, 180) + tx: arrondi(clamp(offset.tx, -90, 90)), + ty: arrondi(clamp(offset.ty, -90, 90)), + spin: arrondi(clamp(offset.spin, -180, 180)) }) export const clampEyes = (eyes: EyeTune): EyeTune => ({ left: eyeOf(eyes.left, DEFAULT_EYE), right: eyeOf(eyes.right, DEFAULT_EYE), - spacing: clamp( - (() => { - const value = num(eyes.spacing, 35) - return value <= 5 ? value * 35 : value - })(), - 0, - 150 + spacing: arrondi( + clamp( + (() => { + const value = num(eyes.spacing, 35) + return value <= 5 ? value * 35 : value + })(), + 0, + 150 + ) ) })