Initial commit
This commit is contained in:
24
web/utils/index.ts
Normal file
24
web/utils/index.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
export const sleep = (ms: number) => {
|
||||
return new Promise(resolve => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
export async function asyncRunSafe<T = any>(fn: Promise<T>): Promise<[Error] | [null, T]> {
|
||||
try {
|
||||
return [null, await fn]
|
||||
}
|
||||
catch (e) {
|
||||
if (e instanceof Error)
|
||||
return [e]
|
||||
return [new Error('unknown error')]
|
||||
}
|
||||
}
|
||||
|
||||
export const getTextWidthWithCanvas = (text: string, font?: string) => {
|
||||
const canvas = document.createElement('canvas');
|
||||
const ctx = canvas.getContext('2d');
|
||||
if (ctx) {
|
||||
ctx.font = font ?? '12px Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"';
|
||||
return Number(ctx.measureText(text).width.toFixed(2));
|
||||
}
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user