Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

refactor(math): one clamp instead of sixteen

Etienne Lescot80b179167f35
diff --git a/electron/native-bridge/cursor/recording/macNativeCursorRecordingSession.ts b/electron/native-bridge/cursor/recording/macNativeCursorRecordingSession.ts
index 27a8a870c..e274b681f 100644
--- a/electron/native-bridge/cursor/recording/macNativeCursorRecordingSession.ts
+++ b/electron/native-bridge/cursor/recording/macNativeCursorRecordingSession.ts
@@ -9,6 +9,7 @@ import type {
NativeCursorAsset,
NativeCursorType,
} from "../../../../src/native/contracts";
+import { clamp } from "../../../../src/utils/math";
import type { CursorRecordingSession } from "./session";
interface MacCursorAssetPayload {
@@ -173,10 +174,6 @@ export async function requestMacCursorAccessibilityAccess() {
);
}
-function clamp(value: number, min: number, max: number) {
- return Math.min(max, Math.max(min, value));
-}
-
function normalizeCursorType(value: unknown): NativeCursorType | null {
return value === "arrow" || value === "pointer" || value === "text" ? value : null;
}
diff --git a/electron/native-bridge/cursor/recording/telemetryRecordingSession.ts b/electron/native-bridge/cursor/recording/telemetryRecordingSession.ts
index e719d8ee3..a6b58d445 100644
--- a/electron/native-bridge/cursor/recording/telemetryRecordingSession.ts
+++ b/electron/native-bridge/cursor/recording/telemetryRecordingSession.ts
@@ -1,5 +1,6 @@
import { type Rectangle, screen } from "electron";
import type { CursorRecordingData, CursorRecordingSample } from "../../../../src/native/contracts";
+import { clamp } from "../../../../src/utils/math";
import type { CursorRecordingSession } from "./session";
interface TelemetryRecordingSessionOptions {
@@ -9,10 +10,6 @@ interface TelemetryRecordingSessionOptions {
startTimeMs?: number;
}
-function clamp(value: number, min: number, max: number) {
- return Math.min(max, Math.max(min, value));
-}
-
export class TelemetryRecordingSession implements CursorRecordingSession {
private samples: CursorRecordingSample[] = [];
private interval: NodeJS.Timeout | null = null;
diff --git a/src/components/ai-edition/PreviewCanvas.tsx b/src/components/ai-edition/PreviewCanvas.tsx
index 90744014c..80800f309 100644
--- a/src/components/ai-edition/PreviewCanvas.tsx
+++ b/src/components/ai-edition/PreviewCanvas.tsx
@@ -53,6 +53,7 @@ import {
} from "@/lib/compositeLayout";
import { classifyWallpaper, resolveImageWallpaperUrl } from "@/lib/wallpaper";
import { getCssClipPath } from "@/lib/webcamMaskShapes";
+import { clamp, clamp01 } from "@/utils/math";
import { AnnotationLayer } from "./AnnotationLayer";
import { CaptionLayer } from "./CaptionLayer";
import { NativeCompositorOverlay } from "./NativeCompositorOverlay";
@@ -580,11 +581,3 @@ function buildWebcamStyle(
};
return clipPath ? { ...base, clipPath } : base;
}
-
-function clamp(v: number, min: number, max: number): number {
- return Math.min(Math.max(v, min), max);
-}
-
-function clamp01(v: number, min = 0, max = 1): number {
- return clamp(v, min, max);
-}
diff --git a/src/components/ai-edition/ZoomFocusOverlay.tsx b/src/components/ai-edition/ZoomFocusOverlay.tsx
index adf1dd827..0bfc0670d 100644
--- a/src/components/ai-edition/ZoomFocusOverlay.tsx
+++ b/src/components/ai-edition/ZoomFocusOverlay.tsx
@@ -12,6 +12,7 @@ import { useCallback, useRef } from "react";
import { getZoomScale, type ZoomFocus } from "@/components/video-editor/types";
import { getFocusBoundsForScale } from "@/components/video-editor/videoPlayback/focusUtils";
import type { AxcutZoomRegion } from "@/lib/ai-edition/schema";
+import { clamp01 } from "@/utils/math";
import styles from "./ZoomFocusOverlay.module.css";
interface ZoomFocusOverlayProps {
@@ -23,10 +24,6 @@ interface ZoomFocusOverlayProps {
onFocusCommit?: () => void;
}
-function clamp01(v: number): number {
- return Math.min(1, Math.max(0, v));
-}
-
export function ZoomFocusOverlay({
region,
isPlaying,
diff --git a/src/components/ui/gradient-editor.tsx b/src/components/ui/gradient-editor.tsx
index 19bb98603..a29e1b923 100644
--- a/src/components/ui/gradient-editor.tsx
+++ b/src/components/ui/gradient-editor.tsx
@@ -1,5 +1,6 @@
import type { PointerEvent as ReactPointerEvent } from "react";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
+import { clamp } from "@/utils/math";
/* ---------- icons ---------- */
const PlusIcon = () => (
@@ -22,8 +23,6 @@ const RingsIcon = () => (
</svg>
);
-const clamp = (v: number, min: number, max: number) => Math.min(max, Math.max(min, v));
-
const MAX_COLORS = 3;
const MAIN_ID = "main";
const MAIN_MAX_RADIUS = 40;
diff --git a/src/components/video-editor/projectPersistence.ts b/src/components/video-editor/projectPersistence.ts
index 1528d75d0..dbb1fc937 100644
--- a/src/components/video-editor/projectPersistence.ts
+++ b/src/components/video-editor/projectPersistence.ts
@@ -6,6 +6,7 @@ import type { ProjectMedia } from "@/lib/recordingSession";
import { normalizeProjectMedia } from "@/lib/recordingSession";
import { DEFAULT_WALLPAPER, WALLPAPER_PATHS } from "@/lib/wallpaper";
import { type AspectRatio, isAspectRatio, isPortraitAspectRatio } from "@/utils/aspectRatioUtils";
+import { clamp } from "@/utils/math";
import {
DEFAULT_EDITOR_APPEARANCE_SETTINGS,
DEFAULT_EDITOR_LAYOUT_SETTINGS,
@@ -128,10 +129,6 @@ function computeNormalizedWebcamLayoutPreset(
}
}
-function clamp(value: number, min: number, max: number) {
- return Math.min(max, Math.max(min, value));
-}
-
function encodePathSegments(pathname: string, keepWindowsDrive = false): string {
return pathname
.split("/")
diff --git a/src/components/video-editor/types.ts b/src/components/video-editor/types.ts
index 9fc45ab0d..c6aa73426 100644
--- a/src/components/video-editor/types.ts
+++ b/src/components/video-editor/types.ts
@@ -1,4 +1,5 @@
import type { WebcamLayoutPreset } from "@/lib/compositeLayout";
+import { clamp } from "@/utils/math";
export type ZoomDepth = 1 | 2 | 3 | 4 | 5 | 6;
export type ZoomFocusMode = "manual" | "auto";
@@ -450,8 +451,3 @@ export function clampFocusToDepth(focus: ZoomFocus, _depth: ZoomDepth): ZoomFocu
cy: clamp(focus.cy, 0, 1),
};
}
-
-function clamp(value: number, min: number, max: number) {
- if (Number.isNaN(value)) return (min + max) / 2;
- return Math.min(max, Math.max(min, value));
-}
diff --git a/src/components/video-editor/videoPlayback/focusUtils.ts b/src/components/video-editor/videoPlayback/focusUtils.ts
index a0973ecda..a625df405 100644
--- a/src/components/video-editor/videoPlayback/focusUtils.ts
+++ b/src/components/video-editor/videoPlayback/focusUtils.ts
@@ -1,3 +1,4 @@
+import { clamp } from "@/utils/math";
import { clampFocusToDepth, ZOOM_DEPTH_SCALES, type ZoomDepth, type ZoomFocus } from "../types";
interface StageSize {
@@ -5,10 +6,6 @@ interface StageSize {
height: number;
}
-function clamp(value: number, min: number, max: number) {
- return Math.max(min, Math.min(max, value));
-}
-
function easeIntoBoundary(normalized: number) {
const t = clamp(normalized, 0, 1);
return -t * t * t + 2 * t * t;
diff --git a/src/components/video-editor/videoPlayback/mathUtils.ts b/src/components/video-editor/videoPlayback/mathUtils.ts
index 6553c622c..fdf1afb4f 100644
--- a/src/components/video-editor/videoPlayback/mathUtils.ts
+++ b/src/components/video-editor/videoPlayback/mathUtils.ts
@@ -1,6 +1,4 @@
-export function clamp01(value: number) {
- return Math.max(0, Math.min(1, value));
-}
+import { clamp01 } from "@/utils/math";
function sampleCubicBezier(a1: number, a2: number, t: number) {
const oneMinusT = 1 - t;
@@ -49,32 +47,10 @@ export function cubicBezier(x1: number, y1: number, x2: number, y2: number, t: n
return sampleCubicBezier(y1, y2, solvedT);
}
-export function easeOutExpo(t: number) {
- const clamped = clamp01(t);
- if (clamped === 1) {
- return 1;
- }
-
- return 1 - Math.pow(2, -7 * clamped);
-}
-
export function easeOutScreenStudio(t: number) {
return cubicBezier(0.16, 1, 0.3, 1, t);
}
-export function smoothStep(t: number) {
- const clamped = clamp01(t);
- return clamped * clamped * (3 - 2 * clamped);
-}
-
-/**
- * Ease-in-out cubic. Used for zoom-in transitions.
- */
-export function easeInOutCubic(t: number) {
- const x = clamp01(t);
- return x < 0.5 ? 4 * x * x * x : 1 - Math.pow(-2 * x + 2, 3) / 2;
-}
-
/**
* Ease-out cubic. Used for zoom-out transitions so strength eases to zero.
*/
diff --git a/src/components/video-editor/videoPlayback/zoomRegionUtils.ts b/src/components/video-editor/videoPlayback/zoomRegionUtils.ts
index 54edfbe2e..f4b6efa1b 100644
--- a/src/components/video-editor/videoPlayback/zoomRegionUtils.ts
+++ b/src/components/video-editor/videoPlayback/zoomRegionUtils.ts
@@ -1,9 +1,10 @@
+import { clamp01 } from "@/utils/math";
import type { CursorTelemetryPoint, Rotation3D, ZoomFocus, ZoomRegion } from "../types";
import { DEFAULT_ROTATION_3D, getRotation3D, getZoomScale, lerpRotation3D } from "../types";
import { TRANSITION_WINDOW_MS, ZOOM_IN_TRANSITION_WINDOW_MS } from "./constants";
import { interpolateCursorAt } from "./cursorFollowUtils";
import { clampFocusToScale } from "./focusUtils";
-import { clamp01, cubicBezier, easeOutScreenStudio } from "./mathUtils";
+import { cubicBezier, easeOutScreenStudio } from "./mathUtils";
const CHAINED_ZOOM_PAN_GAP_MS = 1500;
const CONNECTED_ZOOM_PAN_DURATION_MS = 1000;
diff --git a/src/lib/ai-edition/annotations/textAnimation.ts b/src/lib/ai-edition/annotations/textAnimation.ts
index 61399d440..83c667b8b 100644
--- a/src/lib/ai-edition/annotations/textAnimation.ts
+++ b/src/lib/ai-edition/annotations/textAnimation.ts
@@ -1,3 +1,4 @@
+import { clamp01 } from "@/utils/math";
// Port of `src/lib/annotationTextAnimation.ts` — pure, no Pixi/React deps,
// copied verbatim (only the annotation param type changed to structurally
// match `AxcutAnnotationRegion` instead of the legacy `AnnotationRegion`).
@@ -32,17 +33,13 @@ export const TEXT_ANIMATION_VALUES: AnnotationTextAnimation[] = [
"pulse",
];
-function clamp(value: number, min = 0, max = 1) {
- return Math.min(max, Math.max(min, value));
-}
-
function easeOutCubic(value: number) {
- const t = clamp(value);
+ const t = clamp01(value);
return 1 - (1 - t) ** 3;
}
function easeOutBack(value: number) {
- const t = clamp(value);
+ const t = clamp01(value);
const c1 = 1.70158;
const c3 = c1 + 1;
return 1 + c3 * (t - 1) ** 3 + c1 * (t - 1) ** 2;
@@ -64,7 +61,7 @@ export function getTextAnimationState(
}
const elapsedMs = Math.max(0, currentTimeMs - annotation.startMs);
- const progress = clamp(elapsedMs / TEXT_ANIMATION_DURATION_MS);
+ const progress = clamp01(elapsedMs / TEXT_ANIMATION_DURATION_MS);
const eased = easeOutCubic(progress);
switch (animation) {
diff --git a/src/lib/ai-edition/captions/settings.ts b/src/lib/ai-edition/captions/settings.ts
index 1efe599c2..e3501d2c6 100644
--- a/src/lib/ai-edition/captions/settings.ts
+++ b/src/lib/ai-edition/captions/settings.ts
@@ -9,6 +9,7 @@
// `legacyEditor` passthrough blob), so caption settings round-trip through save
// / load / undo with every other appearance setting and need no schema bump.
+import { clamp } from "@/utils/math";
import type { AxcutDocument } from "../schema";
/** Vertical anchor of the caption band inside the frame. */
@@ -83,10 +84,6 @@ function isFiniteNumber(value: unknown): value is number {
return typeof value === "number" && Number.isFinite(value);
}
-function clamp(value: number, min: number, max: number): number {
- return Math.min(max, Math.max(min, value));
-}
-
function readNumber(value: unknown, fallback: number, min: number, max: number): number {
return isFiniteNumber(value) ? clamp(value, min, max) : fallback;
}
diff --git a/src/lib/ai-edition/store/useTimeline.ts b/src/lib/ai-edition/store/useTimeline.ts
index b972b5922..c9af6aa4b 100644
--- a/src/lib/ai-edition/store/useTimeline.ts
+++ b/src/lib/ai-edition/store/useTimeline.ts
@@ -30,6 +30,12 @@ import { resolveTimelineSpanToTrim } from "../timeline/trim-mapping";
import type { AutoZoomSuggestion } from "../timeline/zoom-suggestions";
import { useProjectStore } from "./projectStore";
+// NaN-guarded floors. Timeline inputs arrive from drag deltas and persisted
+// documents, both of which can carry NaN; every action needs the same guard.
+const finiteSec = (n: number) => (Number.isFinite(n) ? Math.max(0, n) : 0);
+const finiteMs = (n: number) => (Number.isFinite(n) ? Math.max(0, Math.round(n)) : 0);
+const finiteFraction = (n: number) => (Number.isFinite(n) ? Math.min(1, Math.max(0, n)) : 0.5);
+
// Placeholder duration applied to a freshly-inserted clip whose source asset hasn't
// reported its real duration yet (media drag → drop before the preview video fires
// `loadedmetadata`). `applyProbedDuration` (document layer) swaps it — and the
@@ -340,9 +346,8 @@ export function useTimeline() {
const updateTrimRange = useCallback(
async (trimId: string, startSec: number, endSec: number) => {
if (!document) return;
- const clamp = (n: number) => (Number.isFinite(n) ? Math.max(0, n) : 0);
- const s = clamp(startSec);
- const e = clamp(endSec);
+ const s = finiteSec(startSec);
+ const e = finiteSec(endSec);
const next: AxcutDocument = {
...document,
timeline: {
@@ -364,9 +369,8 @@ export function useTimeline() {
const updateTrim = useCallback(
async (trimId: string, next: { assetId: string; startSec: number; endSec: number }) => {
if (!document) return;
- const clamp = (n: number) => (Number.isFinite(n) ? Math.max(0, n) : 0);
- const s = clamp(next.startSec);
- const e = clamp(next.endSec);
+ const s = finiteSec(next.startSec);
+ const e = finiteSec(next.endSec);
const nextDoc: AxcutDocument = {
...document,
timeline: {
@@ -403,11 +407,10 @@ export function useTimeline() {
if (!doc) return;
const managed = new Set<string>([...entries.map((e) => e.id), ...dropIds]);
const others = doc.timeline.trimRanges.filter((r) => !managed.has(r.id));
- const clamp = (n: number) => (Number.isFinite(n) ? Math.max(0, n) : 0);
const rebuilt = entries.map((e) => {
const prev = doc.timeline.trimRanges.find((r) => r.id === e.id);
- const s = clamp(e.sourceStartSec);
- const en = clamp(e.sourceEndSec);
+ const s = finiteSec(e.sourceStartSec);
+ const en = finiteSec(e.sourceEndSec);
return {
id: e.id,
assetId: e.assetId,
@@ -432,9 +435,8 @@ export function useTimeline() {
const updateZoomSpan = useCallback(
async (id: string, startMs: number, endMs: number) => {
if (!document) return;
- const clampMs = (n: number) => (Number.isFinite(n) ? Math.max(0, Math.round(n)) : 0);
- const s = clampMs(startMs);
- const e = clampMs(endMs);
+ const s = finiteMs(startMs);
+ const e = finiteMs(endMs);
const next: AxcutDocument = {
...document,
zoomRanges: replacePillSpan(
@@ -460,11 +462,10 @@ export function useTimeline() {
(id: string, focus: { cx: number; cy: number }) => {
const doc = useProjectStore.getState().document;
if (!doc) return;
- const clamp01 = (n: number) => (Number.isFinite(n) ? Math.min(1, Math.max(0, n)) : 0.5);
const next: AxcutDocument = {
...doc,
zoomRanges: patchPillById(doc.zoomRanges, id, {
- focus: { cx: clamp01(focus.cx), cy: clamp01(focus.cy) },
+ focus: { cx: finiteFraction(focus.cx), cy: finiteFraction(focus.cy) },
}) as AxcutDocument["zoomRanges"],
};
setDocument(next);
@@ -539,9 +540,8 @@ export function useTimeline() {
const updateAnnotationSpan = useCallback(
async (id: string, startMs: number, endMs: number) => {
if (!document) return;
- const clampMs = (n: number) => (Number.isFinite(n) ? Math.max(0, Math.round(n)) : 0);
- const s = clampMs(startMs);
- const e = clampMs(endMs);
+ const s = finiteMs(startMs);
+ const e = finiteMs(endMs);
const next: AxcutDocument = {
...document,
annotations: replacePillSpan(
@@ -583,9 +583,8 @@ export function useTimeline() {
const updateSpeedSpan = useCallback(
async (id: string, startMs: number, endMs: number) => {
if (!document) return;
- const clampMs = (n: number) => (Number.isFinite(n) ? Math.max(0, Math.round(n)) : 0);
- const s = clampMs(startMs);
- const e = clampMs(endMs);
+ const s = finiteMs(startMs);
+ const e = finiteMs(endMs);
const legacy = (document.legacyEditor as Record<string, unknown>) ?? {};
const prev = ((legacy.speedRegions as unknown[]) ?? []) as Array<{
id: string;
@@ -615,9 +614,8 @@ export function useTimeline() {
const updateCameraFullscreenSpan = useCallback(
async (id: string, startMs: number, endMs: number) => {
if (!document) return;
- const clampMs = (n: number) => (Number.isFinite(n) ? Math.max(0, Math.round(n)) : 0);
- const s = clampMs(startMs);
- const e = clampMs(endMs);
+ const s = finiteMs(startMs);
+ const e = finiteMs(endMs);
const legacy = (document.legacyEditor as Record<string, unknown>) ?? {};
const prev = ((legacy.cameraFullscreenRegions as unknown[]) ?? []) as Array<{
id: string;
@@ -822,17 +820,16 @@ export function useTimeline() {
// refine doesn't reject the save. Swap when end < start instead of
// throwing — a user typing into a number input is expected to be
// able to type in any order.
- const clamp = (n: number) => (Number.isFinite(n) ? Math.max(0, n) : 0);
const next: AxcutDocument["timeline"]["clips"][number] = {
...(document.timeline.clips.find((c) => c.id === clipId) as
| AxcutDocument["timeline"]["clips"][number]
| undefined),
} as AxcutDocument["timeline"]["clips"][number];
if (!next?.id) return;
- const sStart = clamp(patch.sourceStartSec ?? next.sourceStartSec);
- const sEnd = clamp(patch.sourceEndSec ?? next.sourceEndSec ?? 0);
- const tStart = clamp(patch.timelineStartSec ?? next.timelineStartSec);
- const tEnd = clamp(patch.timelineEndSec ?? next.timelineEndSec);
+ const sStart = finiteSec(patch.sourceStartSec ?? next.sourceStartSec);
+ const sEnd = finiteSec(patch.sourceEndSec ?? next.sourceEndSec ?? 0);
+ const tStart = finiteSec(patch.timelineStartSec ?? next.timelineStartSec);
+ const tEnd = finiteSec(patch.timelineEndSec ?? next.timelineEndSec);
const updated: AxcutDocument["timeline"]["clips"][number] = {
...next,
sourceStartSec: Math.min(sStart, sEnd),
diff --git a/src/lib/annotationTextAnimation.ts b/src/lib/annotationTextAnimation.ts
index 1993dbcba..f7be13eb7 100644
--- a/src/lib/annotationTextAnimation.ts
+++ b/src/lib/annotationTextAnimation.ts
@@ -1,4 +1,5 @@
import type { AnnotationTextAnimation } from "@/components/video-editor/types";
+import { clamp01 } from "@/utils/math";
export const TEXT_ANIMATION_DURATION_MS = 700;
@@ -23,17 +24,13 @@ export const TEXT_ANIMATION_OPTIONS: Array<{
{ value: "pulse", translationKey: "textAnimation.pulse" },
];
-function clamp(value: number, min = 0, max = 1) {
- return Math.min(max, Math.max(min, value));
-}
-
function easeOutCubic(value: number) {
- const t = clamp(value);
+ const t = clamp01(value);
return 1 - (1 - t) ** 3;
}
function easeOutBack(value: number) {
- const t = clamp(value);
+ const t = clamp01(value);
const c1 = 1.70158;
const c3 = c1 + 1;
return 1 + c3 * (t - 1) ** 3 + c1 * (t - 1) ** 2;
@@ -66,7 +63,7 @@ export function getTextAnimationState(
}
const elapsedMs = Math.max(0, currentTimeMs - annotation.startMs);
- const progress = clamp(elapsedMs / TEXT_ANIMATION_DURATION_MS);
+ const progress = clamp01(elapsedMs / TEXT_ANIMATION_DURATION_MS);
const eased = easeOutCubic(progress);
switch (animation) {
diff --git a/src/lib/blurEffects.ts b/src/lib/blurEffects.ts
index 88bcfb0cb..99021281b 100644
--- a/src/lib/blurEffects.ts
+++ b/src/lib/blurEffects.ts
@@ -9,11 +9,7 @@ import {
MIN_BLUR_BLOCK_SIZE,
MIN_BLUR_INTENSITY,
} from "@/components/video-editor/types";
-
-function clamp(value: number, min: number, max: number) {
- if (!Number.isFinite(value)) return min;
- return Math.min(max, Math.max(min, value));
-}
+import { clamp } from "@/utils/math";
export function normalizeBlurType(value: unknown): BlurType {
void value;
diff --git a/src/lib/cursor/cursorPathSmoothing.ts b/src/lib/cursor/cursorPathSmoothing.ts
index e89b9575d..c550fa623 100644
--- a/src/lib/cursor/cursorPathSmoothing.ts
+++ b/src/lib/cursor/cursorPathSmoothing.ts
@@ -1,5 +1,6 @@
import { getCursorSpringConfig } from "@/components/video-editor/videoPlayback/motionSmoothing";
import type { CursorRecordingData, CursorRecordingSample } from "@/native/contracts";
+import { clamp } from "@/utils/math";
/**
* Offline cursor-path smoothing for native recordings.
@@ -31,10 +32,6 @@ interface SmoothedRun {
ys: Float32Array;
}
-function clamp(value: number, min: number, max: number) {
- return Math.min(max, Math.max(min, value));
-}
-
function binarySearchAtOrBefore(
times: Float32Array | number[],
timeMs: number,
diff --git a/src/lib/cursor/nativeCursor.ts b/src/lib/cursor/nativeCursor.ts
index dcf55eea5..746492932 100644
--- a/src/lib/cursor/nativeCursor.ts
+++ b/src/lib/cursor/nativeCursor.ts
@@ -24,6 +24,7 @@ import type {
NativeCursorAsset,
NativeCursorType,
} from "@/native/contracts";
+import { clamp } from "@/utils/math";
export interface ActiveNativeCursorFrame {
asset: NativeCursorAsset;
@@ -48,10 +49,6 @@ interface ProjectNativeCursorToStageOptions extends ProjectNativeCursorOptions {
videoContainerPosition: { x: number; y: number };
}
-function clamp(value: number, min: number, max: number) {
- return Math.min(max, Math.max(min, value));
-}
-
const NATIVE_CURSOR_CLICK_ANIMATION_MS = 260;
const NATIVE_CURSOR_MOTION_BLUR_MAX_PX = 6;
const nativeCursorAssetMapCache = new WeakMap<
diff --git a/src/lib/cursor/pixiCursorRenderer.ts b/src/lib/cursor/pixiCursorRenderer.ts
index d3bf6d8ab..1b4f567f9 100644
--- a/src/lib/cursor/pixiCursorRenderer.ts
+++ b/src/lib/cursor/pixiCursorRenderer.ts
@@ -7,6 +7,7 @@ import {
resetSpringState,
stepSpringValue,
} from "@/components/video-editor/videoPlayback/motionSmoothing";
+import { clamp } from "@/utils/math";
import { UPLOADED_CURSOR_SAMPLE_SIZE, uploadedCursorAssets } from "./uploadedCursorAssets";
type CursorAssetKey = NonNullable<CursorTelemetryPoint["cursorType"]>;
@@ -103,10 +104,6 @@ function loadImage(dataUrl: string) {
});
}
-function clamp(value: number, min: number, max: number) {
- return Math.min(max, Math.max(min, value));
-}
-
function getNormalizedAnchor(
systemAsset: SystemCursorAsset | undefined,
fallbackAnchor: { x: number; y: number },
diff --git a/src/lib/exporter/gradientParser.ts b/src/lib/exporter/gradientParser.ts
index c643edcb4..79724af5e 100644
--- a/src/lib/exporter/gradientParser.ts
+++ b/src/lib/exporter/gradientParser.ts
@@ -1,3 +1,4 @@
+import { clamp } from "@/utils/math";
export interface ParsedGradientStop {
color: string;
offset: number;
@@ -221,10 +222,6 @@ function normalizeStopOffsets(
}));
}
-function clamp(value: number, min: number, max: number) {
- return Math.min(max, Math.max(min, value));
-}
-
function findLastDefinedIndex(values: Array<number | null>) {
for (let index = values.length - 1; index >= 0; index -= 1) {
if (values[index] !== null) {
diff --git a/src/lib/gradientBuilder.ts b/src/lib/gradientBuilder.ts
index 69b44f416..b45da7975 100644
--- a/src/lib/gradientBuilder.ts
+++ b/src/lib/gradientBuilder.ts
@@ -1,4 +1,5 @@
import type { GradientEditorState } from "@/components/ui/gradient-editor";
+import { clamp } from "@/utils/math";
/* ---------- editor state -> CSS background ----------
The editor produces a clean 3-stop LINEAR gradient - the same shape
@@ -35,7 +36,3 @@ function baseForBrightness(brightness: number): string {
const l = Math.round((brightness / 100) * 18);
return `hsl(0, 0%, ${clamp(l, 0, 22)}%)`;
}
-
-function clamp(v: number, min: number, max: number): number {
- return Math.min(max, Math.max(min, v));
-}
diff --git a/src/utils/math.ts b/src/utils/math.ts
new file mode 100644
index 000000000..ac44b9c08
--- /dev/null
+++ b/src/utils/math.ts
@@ -0,0 +1,12 @@
+// Numeric helpers shared by the renderer, the electron main process and the
+// native glue. `clamp` had 16 identical private copies before this file existed.
+
+/** Constrains `value` to [min, max]. */
+export function clamp(value: number, min: number, max: number): number {
+ return Math.min(max, Math.max(min, value));
+}
+
+/** `clamp(value, 0, 1)` — the normalized-fraction case, common enough to name. */
+export function clamp01(value: number): number {
+ return clamp(value, 0, 1);
+}