From ef6acaafee19abb8a5d932ca5f3e2bf3ca096c7e Mon Sep 17 00:00:00 2001 From: leomcelroy Date: Tue, 21 Nov 2023 11:47:42 -0500 Subject: [PATCH] fix init view centering --- astro/src/components/Preview.tsx | 34 ++++++++++++-------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/astro/src/components/Preview.tsx b/astro/src/components/Preview.tsx index d6b7aa4e2..0e631d136 100644 --- a/astro/src/components/Preview.tsx +++ b/astro/src/components/Preview.tsx @@ -38,31 +38,23 @@ function init() { const bodyListener = createListener(document.body) const canvasListener = createListener(canvas) - const { docDimensions } = getStore() - if (!canvas) return - const br = canvas.getBoundingClientRect() - panZoomParams.scale = Math.min( - (br.width - 20) / docDimensions.width, - (br.height - 20) / docDimensions.height - ) - - panZoomParams.panX = - br.width / 2 - (docDimensions.width * panZoomParams.scale) / 2 - panZoomParams.panY = - br.height / 2 + (docDimensions.height * panZoomParams.scale) / 2 + // center view + const { docDimensions } = getStore() - requestRedraw(canvas) - }) + const br = canvas.getBoundingClientRect() + panZoomParams.scale = Math.min( + (br.width - 20) / docDimensions.width, + (br.height - 20) / docDimensions.height + ) - const resizeObserver = new ResizeObserver(entries => { - const { width, height } = entries[0].contentRect - dpr = window.devicePixelRatio || 1 - canvas.width = width * dpr - canvas.height = height * dpr - setCtxProperties() // setting width/height clears ctx state + panZoomParams.panX = + br.width / 2 - (docDimensions.width * panZoomParams.scale) / 2 + panZoomParams.panY = + br.height / 2 + (docDimensions.height * panZoomParams.scale) / 2 - requestRedraw(canvas) + requestRedraw(canvas) + canvasListener( 'wheel',