fix:调节数值取整避免精度漂移

This commit is contained in:
Edom
2026-08-24 21:31:10 +08:00
parent e373a1b1e1
commit 6c6e6b6893
2 changed files with 29 additions and 23 deletions
+6 -4
View File
@@ -17,11 +17,13 @@ export const COMPOSITION_DEFAUT: CompositionPhoto = { x: 0, y: 0, scale: 1, corn
const borne = (valeur: number, min: number, max: number) => const borne = (valeur: number, min: number, max: number) =>
Math.min(max, Math.max(min, Number.isFinite(valeur) ? valeur : min)) 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 => ({ export const normaliseComposition = (c: CompositionPhoto): CompositionPhoto => ({
x: borne(c.x, -180, 180), x: arrondi(borne(c.x, -180, 180)),
y: borne(c.y, -180, 180), y: arrondi(borne(c.y, -180, 180)),
scale: borne(c.scale, 0.4, 3), scale: arrondi(borne(c.scale, 0.4, 3), 3),
cornerRadius: borne(c.cornerRadius, 0, 50) cornerRadius: arrondi(borne(c.cornerRadius, 0, 50))
}) })
/** Pourcent (0-50) vers unites viewBox du cadre 300. */ /** Pourcent (0-50) vers unites viewBox du cadre 300. */
+23 -19
View File
@@ -60,42 +60,46 @@ export const defaultManual = (): ManualState => ({
const num = (value: unknown, fallback: number) => const num = (value: unknown, fallback: number) =>
typeof value === 'number' && Number.isFinite(value) ? value : fallback 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 eyeOf = (value: unknown, fallback: EyeSide): EyeSide => {
const raw = value && typeof value === 'object' ? (value as Record<string, unknown>) : {} const raw = value && typeof value === 'object' ? (value as Record<string, unknown>) : {}
const width = num(raw.width, fallback.width) const width = num(raw.width, fallback.width)
const height = num(raw.height, fallback.height) const height = num(raw.height, fallback.height)
return { return {
width: clamp(width <= 5 ? width * 20 : width, 10, 110), width: arrondi(clamp(width <= 5 ? width * 20 : width, 10, 110)),
height: clamp(height <= 5 ? height * 50 : height, 10, 110), height: arrondi(clamp(height <= 5 ? height * 50 : height, 10, 110)),
size: clamp(num(raw.size, fallback.size), 0.35, 2.2), size: arrondi(clamp(num(raw.size, fallback.size), 0.35, 2.2), 2),
angle: num(raw.angle, fallback.angle), angle: arrondi(num(raw.angle, fallback.angle)),
x: clamp(num(raw.x, fallback.x), -48, 48), x: arrondi(clamp(num(raw.x, fallback.x), -48, 48)),
y: clamp(num(raw.y, fallback.y), -48, 48) y: arrondi(clamp(num(raw.y, fallback.y), -48, 48))
} }
} }
export const clampPose = (pose: HeadPose): HeadPose => ({ export const clampPose = (pose: HeadPose): HeadPose => ({
turn: clamp(pose.turn, -365, 365), turn: arrondi(clamp(pose.turn, -365, 365)),
tilt: clamp(pose.tilt, -365, 365), tilt: arrondi(clamp(pose.tilt, -365, 365)),
roll: clamp(pose.roll, -365, 365) roll: arrondi(clamp(pose.roll, -365, 365))
}) })
export const clampOffset = (offset: BodyOffset): BodyOffset => ({ export const clampOffset = (offset: BodyOffset): BodyOffset => ({
tx: clamp(offset.tx, -90, 90), tx: arrondi(clamp(offset.tx, -90, 90)),
ty: clamp(offset.ty, -90, 90), ty: arrondi(clamp(offset.ty, -90, 90)),
spin: clamp(offset.spin, -180, 180) spin: arrondi(clamp(offset.spin, -180, 180))
}) })
export const clampEyes = (eyes: EyeTune): EyeTune => ({ export const clampEyes = (eyes: EyeTune): EyeTune => ({
left: eyeOf(eyes.left, DEFAULT_EYE), left: eyeOf(eyes.left, DEFAULT_EYE),
right: eyeOf(eyes.right, DEFAULT_EYE), right: eyeOf(eyes.right, DEFAULT_EYE),
spacing: clamp( spacing: arrondi(
(() => { clamp(
const value = num(eyes.spacing, 35) (() => {
return value <= 5 ? value * 35 : value const value = num(eyes.spacing, 35)
})(), return value <= 5 ? value * 35 : value
0, })(),
150 0,
150
)
) )
}) })