mirror of
https://github.com/documenso/documenso.git
synced 2026-06-22 04:12:06 +10:00
fix: load typed signature font on profile page (#2963)
This commit is contained in:
@@ -3,6 +3,8 @@ import { useEffect, useRef } from 'react';
|
|||||||
|
|
||||||
import { cn } from '../../lib/utils';
|
import { cn } from '../../lib/utils';
|
||||||
|
|
||||||
|
const SIGNATURE_FONT_FAMILY = 'Caveat';
|
||||||
|
|
||||||
export type SignatureRenderProps = {
|
export type SignatureRenderProps = {
|
||||||
className?: string;
|
className?: string;
|
||||||
value: string;
|
value: string;
|
||||||
@@ -30,7 +32,6 @@ export const SignatureRender = ({ className, value }: SignatureRenderProps) => {
|
|||||||
|
|
||||||
const canvasWidth = $el.current.width;
|
const canvasWidth = $el.current.width;
|
||||||
const canvasHeight = $el.current.height;
|
const canvasHeight = $el.current.height;
|
||||||
const fontFamily = 'Caveat';
|
|
||||||
|
|
||||||
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
|
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
|
||||||
ctx.textAlign = 'center';
|
ctx.textAlign = 'center';
|
||||||
@@ -42,7 +43,7 @@ export const SignatureRender = ({ className, value }: SignatureRenderProps) => {
|
|||||||
|
|
||||||
// Start with a base font size
|
// Start with a base font size
|
||||||
let fontSize = 18;
|
let fontSize = 18;
|
||||||
ctx.font = `${fontSize}px ${fontFamily}`;
|
ctx.font = `${fontSize}px ${SIGNATURE_FONT_FAMILY}`;
|
||||||
|
|
||||||
// Measure 10 characters and calculate scale factor
|
// Measure 10 characters and calculate scale factor
|
||||||
const characterWidth = ctx.measureText('m'.repeat(10)).width;
|
const characterWidth = ctx.measureText('m'.repeat(10)).width;
|
||||||
@@ -52,7 +53,7 @@ export const SignatureRender = ({ className, value }: SignatureRenderProps) => {
|
|||||||
fontSize = fontSize * scaleFactor;
|
fontSize = fontSize * scaleFactor;
|
||||||
|
|
||||||
// Adjust font size if it exceeds canvas width
|
// Adjust font size if it exceeds canvas width
|
||||||
ctx.font = `${fontSize}px ${fontFamily}`;
|
ctx.font = `${fontSize}px ${SIGNATURE_FONT_FAMILY}`;
|
||||||
|
|
||||||
const textWidth = ctx.measureText(value).width;
|
const textWidth = ctx.measureText(value).width;
|
||||||
|
|
||||||
@@ -61,7 +62,7 @@ export const SignatureRender = ({ className, value }: SignatureRenderProps) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set final font and render text
|
// Set final font and render text
|
||||||
ctx.font = `${fontSize}px ${fontFamily}`;
|
ctx.font = `${fontSize}px ${SIGNATURE_FONT_FAMILY}`;
|
||||||
ctx.fillText(value, canvasWidth / 2, canvasHeight / 2);
|
ctx.fillText(value, canvasWidth / 2, canvasHeight / 2);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -110,11 +111,28 @@ export const SignatureRender = ({ className, value }: SignatureRenderProps) => {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
let isMounted = true;
|
||||||
|
|
||||||
if (isBase64Image(value)) {
|
if (isBase64Image(value)) {
|
||||||
renderImageSignature();
|
renderImageSignature();
|
||||||
} else {
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const renderWhenFontIsReady = async () => {
|
||||||
|
try {
|
||||||
|
await document.fonts?.load(`18px ${SIGNATURE_FONT_FAMILY}`);
|
||||||
|
} finally {
|
||||||
|
if (isMounted) {
|
||||||
renderTypedSignature();
|
renderTypedSignature();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void renderWhenFontIsReady();
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
isMounted = false;
|
||||||
|
};
|
||||||
}, [value]);
|
}, [value]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user