This commit is contained in:
2025-12-31 17:18:50 +07:00
parent 772e84c761
commit 16f64c5e4b
53 changed files with 4247 additions and 82 deletions

View File

@@ -4,4 +4,47 @@ import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export function debounce<Func extends (...args: any[]) => any>(func: Func, wait: number): Func {
let timeout: ReturnType<typeof setTimeout> | null;
return function(this: any, ...args: any[]) {
if (timeout) clearTimeout(timeout);
timeout = setTimeout(() => {
func.apply(this, args);
}, wait);
} as Func;
}
type AspectInfo = {
width: number;
height: number;
ratio: string; // ví dụ: "16:9"
float: number; // ví dụ: 1.777...
};
function gcd(a: number, b: number): number {
return b === 0 ? a : gcd(b, a % b);
}
export function getImageAspectRatio(url: string): Promise<AspectInfo> {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => {
const w = img.naturalWidth;
const h = img.naturalHeight;
const g = gcd(w, h);
resolve({
width: w,
height: h,
ratio: `${w / g}:${h / g}`,
float: w / h
});
};
img.onerror = () => reject(new Error("Cannot load image"));
img.src = url;
});
}