fix: allow zooming and dragging uploaded signatures

This commit is contained in:
David Nguyen
2026-08-12 18:12:52 +10:00
parent 617f8cc204
commit b5ac3ca7b0
4 changed files with 432 additions and 108 deletions
@@ -1,3 +1,39 @@
import { SIGNATURE_MIN_COVERAGE_THRESHOLD } from '@documenso/lib/constants/signatures';
import type { RefObject } from 'react';
/**
* Checks whether the signature covers enough of the canvas to be considered
* valid, by measuring the percentage of non-transparent pixels against
* SIGNATURE_MIN_COVERAGE_THRESHOLD.
*/
export const checkSignatureValidity = (element: RefObject<HTMLCanvasElement>) => {
if (!element.current) {
return false;
}
const ctx = element.current.getContext('2d');
if (!ctx) {
return false;
}
const imageData = ctx.getImageData(0, 0, element.current.width, element.current.height);
const data = imageData.data;
let filledPixels = 0;
const totalPixels = data.length / 4;
for (let i = 0; i < data.length; i += 4) {
if (data[i + 3] > 0) {
filledPixels++;
}
}
const filledPercentage = filledPixels / totalPixels;
const isValid = filledPercentage > SIGNATURE_MIN_COVERAGE_THRESHOLD;
return isValid;
};
export const average = (a: number, b: number) => (a + b) / 2;
export const getSvgPathFromStroke = (points: number[][], closed = true) => {