Recalculate blur data when aspect ratio changes

This commit is contained in:
Sam Becker 2023-12-03 21:06:09 -06:00
parent 7cb21a3816
commit d3ce8f2e91
2 changed files with 31 additions and 36 deletions

View File

@ -24,43 +24,40 @@ export default function CanvasBlurCapture({
quality?: number
}) {
const refCanvas = useRef<HTMLCanvasElement>(null);
const refHasCompleted = useRef(false);
const refTimeouts = useRef<NodeJS.Timeout[]>([]);
useEffect(() => {
const capture = () => {
if (!refHasCompleted.current) {
const canvas = refCanvas.current;
if (canvas) {
canvas.width = width * scale;
canvas.height = height * scale;
canvas.style.width = `${width}px`;
canvas.style.height = `${height}px`;
const context = refCanvas.current?.getContext('2d');
if (context) {
context.scale(scale, scale);
context.filter =
'contrast(1.2) saturate(1.2)' +
`blur(${scale * 10}px)`;
context.drawImage(
image,
-edgeCompensation,
-edgeCompensation,
width + edgeCompensation * 2,
width * image.height / image.width + edgeCompensation * 2,
);
refHasCompleted.current = true;
onCapture(canvas.toDataURL('image/jpeg', quality));
} else {
console.error('Cannot get 2d context');
// Retry capture in case canvas is not available
refTimeouts.current.push(setTimeout(capture, RETRY_DELAY));
}
const canvas = refCanvas.current;
if (canvas) {
canvas.width = width * scale;
canvas.height = height * scale;
canvas.style.width = `${width}px`;
canvas.style.height = `${height}px`;
const context = refCanvas.current?.getContext('2d');
if (context) {
context.scale(scale, scale);
context.filter =
'contrast(1.2) saturate(1.2)' +
`blur(${scale * 10}px)`;
context.drawImage(
image,
-edgeCompensation,
-edgeCompensation,
width + edgeCompensation * 2,
width * image.height / image.width + edgeCompensation * 2,
);
refTimeouts.current.forEach(clearTimeout);
onCapture(canvas.toDataURL('image/jpeg', quality));
} else {
console.error('Cannot generate blur data: canvas not found');
console.error('Cannot get 2d context');
// Retry capture in case canvas is not available
refTimeouts.current.push(setTimeout(capture, RETRY_DELAY));
}
} else {
console.error('Cannot generate blur data: canvas not found');
// Retry capture in case canvas is not available
refTimeouts.current.push(setTimeout(capture, RETRY_DELAY));
}
};

View File

@ -91,13 +91,11 @@ export default function PhotoForm({
const url = formData.url ?? '';
const updateBlurData = useCallback((blurData: string) => {
if (type === 'create') {
setFormData(data => ({
...data,
blurData,
}));
}
}, [type]);
setFormData(data => ({
...data,
blurData,
}));
}, []);
const isFormValid = FORM_METADATA_ENTRIES.every(([key, { required }]) =>
!required || Boolean(formData[key]));