fix: stop flooring scroll positions (#2883)

This commit is contained in:
David Luzar 2021-01-31 10:47:43 +01:00 committed by GitHub
parent 10cd6a24b0
commit 1973ae9444
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 41 additions and 58 deletions

View File

@ -5,7 +5,7 @@ import {
DEFAULT_TEXT_ALIGN, DEFAULT_TEXT_ALIGN,
} from "./constants"; } from "./constants";
import { t } from "./i18n"; import { t } from "./i18n";
import { AppState, FlooredNumber, NormalizedZoomValue } from "./types"; import { AppState, NormalizedZoomValue } from "./types";
import { getDateTime } from "./utils"; import { getDateTime } from "./utils";
export const getDefaultAppState = (): Omit< export const getDefaultAppState = (): Omit<
@ -56,8 +56,8 @@ export const getDefaultAppState = (): Omit<
previousSelectedElementIds: {}, previousSelectedElementIds: {},
resizingElement: null, resizingElement: null,
scrolledOutside: false, scrolledOutside: false,
scrollX: 0 as FlooredNumber, scrollX: 0,
scrollY: 0 as FlooredNumber, scrollY: 0,
selectedElementIds: {}, selectedElementIds: {},
selectedGroupIds: {}, selectedGroupIds: {},
selectionElement: null, selectionElement: null,

View File

@ -147,7 +147,6 @@ import {
getSelectedElements, getSelectedElements,
isOverScrollBars, isOverScrollBars,
isSomeElementSelected, isSomeElementSelected,
normalizeScroll,
} from "../scene"; } from "../scene";
import Scene from "../scene/Scene"; import Scene from "../scene/Scene";
import { SceneState, ScrollBars } from "../scene/types"; import { SceneState, ScrollBars } from "../scene/types";
@ -1745,8 +1744,8 @@ class App extends React.Component<ExcalidrawProps, AppState> {
const scaleFactor = distance / gesture.initialDistance; const scaleFactor = distance / gesture.initialDistance;
this.setState(({ zoom, scrollX, scrollY, offsetLeft, offsetTop }) => ({ this.setState(({ zoom, scrollX, scrollY, offsetLeft, offsetTop }) => ({
scrollX: normalizeScroll(scrollX + deltaX / zoom.value), scrollX: scrollX + deltaX / zoom.value,
scrollY: normalizeScroll(scrollY + deltaY / zoom.value), scrollY: scrollY + deltaY / zoom.value,
zoom: getNewZoom( zoom: getNewZoom(
getNormalizedZoom(initialScale * scaleFactor), getNormalizedZoom(initialScale * scaleFactor),
zoom, zoom,
@ -2157,12 +2156,8 @@ class App extends React.Component<ExcalidrawProps, AppState> {
} }
this.setState({ this.setState({
scrollX: normalizeScroll( scrollX: this.state.scrollX - deltaX / this.state.zoom.value,
this.state.scrollX - deltaX / this.state.zoom.value, scrollY: this.state.scrollY - deltaY / this.state.zoom.value,
),
scrollY: normalizeScroll(
this.state.scrollY - deltaY / this.state.zoom.value,
),
}); });
}); });
const teardown = withBatchedUpdates( const teardown = withBatchedUpdates(
@ -2976,9 +2971,7 @@ class App extends React.Component<ExcalidrawProps, AppState> {
const x = event.clientX; const x = event.clientX;
const dx = x - pointerDownState.lastCoords.x; const dx = x - pointerDownState.lastCoords.x;
this.setState({ this.setState({
scrollX: normalizeScroll( scrollX: this.state.scrollX - dx / this.state.zoom.value,
this.state.scrollX - dx / this.state.zoom.value,
),
}); });
pointerDownState.lastCoords.x = x; pointerDownState.lastCoords.x = x;
return true; return true;
@ -2988,9 +2981,7 @@ class App extends React.Component<ExcalidrawProps, AppState> {
const y = event.clientY; const y = event.clientY;
const dy = y - pointerDownState.lastCoords.y; const dy = y - pointerDownState.lastCoords.y;
this.setState({ this.setState({
scrollY: normalizeScroll( scrollY: this.state.scrollY - dy / this.state.zoom.value,
this.state.scrollY - dy / this.state.zoom.value,
),
}); });
pointerDownState.lastCoords.y = y; pointerDownState.lastCoords.y = y;
return true; return true;
@ -3741,14 +3732,14 @@ class App extends React.Component<ExcalidrawProps, AppState> {
if (event.shiftKey) { if (event.shiftKey) {
this.setState(({ zoom, scrollX }) => ({ this.setState(({ zoom, scrollX }) => ({
// on Mac, shift+wheel tends to result in deltaX // on Mac, shift+wheel tends to result in deltaX
scrollX: normalizeScroll(scrollX - (deltaY || deltaX) / zoom.value), scrollX: scrollX - (deltaY || deltaX) / zoom.value,
})); }));
return; return;
} }
this.setState(({ zoom, scrollX, scrollY }) => ({ this.setState(({ zoom, scrollX, scrollY }) => ({
scrollX: normalizeScroll(scrollX - deltaX / zoom.value), scrollX: scrollX - deltaX / zoom.value,
scrollY: normalizeScroll(scrollY - deltaY / zoom.value), scrollY: scrollY - deltaY / zoom.value,
})); }));
}); });

View File

@ -1,11 +1,10 @@
import { PointerCoords } from "./types"; import { PointerCoords } from "./types";
import { normalizeScroll } from "./scene";
export const getCenter = (pointers: Map<number, PointerCoords>) => { export const getCenter = (pointers: Map<number, PointerCoords>) => {
const allCoords = Array.from(pointers.values()); const allCoords = Array.from(pointers.values());
return { return {
x: normalizeScroll(sum(allCoords, (coords) => coords.x) / allCoords.length), x: sum(allCoords, (coords) => coords.x) / allCoords.length,
y: normalizeScroll(sum(allCoords, (coords) => coords.y) / allCoords.length), y: sum(allCoords, (coords) => coords.y) / allCoords.length,
}; };
}; };

View File

@ -5,7 +5,6 @@ import { NonDeletedExcalidrawElement } from "../element/types";
import { getCommonBounds } from "../element/bounds"; import { getCommonBounds } from "../element/bounds";
import { renderScene, renderSceneToSvg } from "../renderer/renderScene"; import { renderScene, renderSceneToSvg } from "../renderer/renderScene";
import { distance, SVG_NS } from "../utils"; import { distance, SVG_NS } from "../utils";
import { normalizeScroll } from "./scroll";
import { AppState } from "../types"; import { AppState } from "../types";
import { t } from "../i18n"; import { t } from "../i18n";
import { DEFAULT_FONT_FAMILY, DEFAULT_VERTICAL_ALIGN } from "../constants"; import { DEFAULT_FONT_FAMILY, DEFAULT_VERTICAL_ALIGN } from "../constants";
@ -59,8 +58,8 @@ export const exportToCanvas = (
tempCanvas, tempCanvas,
{ {
viewBackgroundColor: exportBackground ? viewBackgroundColor : null, viewBackgroundColor: exportBackground ? viewBackgroundColor : null,
scrollX: normalizeScroll(-minX + exportPadding), scrollX: -minX + exportPadding,
scrollY: normalizeScroll(-minY + exportPadding), scrollY: -minY + exportPadding,
zoom: getDefaultAppState().zoom, zoom: getDefaultAppState().zoom,
remotePointerViewportCoords: {}, remotePointerViewportCoords: {},
remoteSelectedElementIds: {}, remoteSelectedElementIds: {},

View File

@ -6,7 +6,7 @@ export {
getSelectedElements, getSelectedElements,
getTargetElements, getTargetElements,
} from "./selection"; } from "./selection";
export { normalizeScroll, calculateScrollCenter } from "./scroll"; export { calculateScrollCenter } from "./scroll";
export { export {
hasBackground, hasBackground,
hasStroke, hasStroke,

View File

@ -1,4 +1,4 @@
import { AppState, FlooredNumber, PointerCoords, Zoom } from "../types"; import { AppState, PointerCoords, Zoom } from "../types";
import { ExcalidrawElement } from "../element/types"; import { ExcalidrawElement } from "../element/types";
import { getCommonBounds, getClosestElementBounds } from "../element"; import { getCommonBounds, getClosestElementBounds } from "../element";
@ -7,9 +7,6 @@ import {
viewportCoordsToSceneCoords, viewportCoordsToSceneCoords,
} from "../utils"; } from "../utils";
export const normalizeScroll = (pos: number) =>
Math.floor(pos) as FlooredNumber;
const isOutsideViewPort = ( const isOutsideViewPort = (
appState: AppState, appState: AppState,
canvas: HTMLCanvasElement | null, canvas: HTMLCanvasElement | null,
@ -40,16 +37,14 @@ export const centerScrollOn = ({
zoom: Zoom; zoom: Zoom;
}) => { }) => {
return { return {
scrollX: normalizeScroll( scrollX:
(viewportDimensions.width / 2) * (1 / zoom.value) - (viewportDimensions.width / 2) * (1 / zoom.value) -
scenePoint.x - scenePoint.x -
zoom.translation.x * (1 / zoom.value), zoom.translation.x * (1 / zoom.value),
), scrollY:
scrollY: normalizeScroll(
(viewportDimensions.height / 2) * (1 / zoom.value) - (viewportDimensions.height / 2) * (1 / zoom.value) -
scenePoint.y - scenePoint.y -
zoom.translation.y * (1 / zoom.value), zoom.translation.y * (1 / zoom.value),
),
}; };
}; };
@ -57,11 +52,11 @@ export const calculateScrollCenter = (
elements: readonly ExcalidrawElement[], elements: readonly ExcalidrawElement[],
appState: AppState, appState: AppState,
canvas: HTMLCanvasElement | null, canvas: HTMLCanvasElement | null,
): { scrollX: FlooredNumber; scrollY: FlooredNumber } => { ): { scrollX: number; scrollY: number } => {
if (!elements.length) { if (!elements.length) {
return { return {
scrollX: normalizeScroll(0), scrollX: 0,
scrollY: normalizeScroll(0), scrollY: 0,
}; };
} }
let [x1, y1, x2, y2] = getCommonBounds(elements); let [x1, y1, x2, y2] = getCommonBounds(elements);

View File

@ -1,6 +1,6 @@
import { ExcalidrawElement } from "../element/types"; import { ExcalidrawElement } from "../element/types";
import { getCommonBounds } from "../element"; import { getCommonBounds } from "../element";
import { FlooredNumber, Zoom } from "../types"; import { Zoom } from "../types";
import { ScrollBars } from "./types"; import { ScrollBars } from "./types";
import { getGlobalCSSVariable } from "../utils"; import { getGlobalCSSVariable } from "../utils";
import { getLanguage } from "../i18n"; import { getLanguage } from "../i18n";
@ -18,8 +18,8 @@ export const getScrollBars = (
scrollY, scrollY,
zoom, zoom,
}: { }: {
scrollX: FlooredNumber; scrollX: number;
scrollY: FlooredNumber; scrollY: number;
zoom: Zoom; zoom: Zoom;
}, },
): ScrollBars => { ): ScrollBars => {

View File

@ -1,9 +1,9 @@
import { ExcalidrawTextElement } from "../element/types"; import { ExcalidrawTextElement } from "../element/types";
import { FlooredNumber, Zoom } from "../types"; import { Zoom } from "../types";
export type SceneState = { export type SceneState = {
scrollX: FlooredNumber; scrollX: number;
scrollY: FlooredNumber; scrollY: number;
// null indicates transparent bg // null indicates transparent bg
viewBackgroundColor: string | null; viewBackgroundColor: string | null;
zoom: Zoom; zoom: Zoom;
@ -15,8 +15,8 @@ export type SceneState = {
}; };
export type SceneScroll = { export type SceneScroll = {
scrollX: FlooredNumber; scrollX: number;
scrollY: FlooredNumber; scrollY: number;
}; };
export interface Scene { export interface Scene {

View File

@ -12705,7 +12705,7 @@ Object {
}, },
"previousSelectedElementIds": Object {}, "previousSelectedElementIds": Object {},
"resizingElement": null, "resizingElement": null,
"scrollX": -6, "scrollX": -5.416666666666667,
"scrollY": 0, "scrollY": 0,
"scrolledOutside": false, "scrolledOutside": false,
"selectedElementIds": Object { "selectedElementIds": Object {
@ -12725,7 +12725,7 @@ Object {
"zenModeEnabled": false, "zenModeEnabled": false,
"zoom": Object { "zoom": Object {
"translation": Object { "translation": Object {
"x": 0.3333333333333357, "x": 0.4166666666666714,
"y": 0, "y": 0,
}, },
"value": 1, "value": 1,
@ -20330,7 +20330,7 @@ Object {
}, },
"previousSelectedElementIds": Object {}, "previousSelectedElementIds": Object {},
"resizingElement": null, "resizingElement": null,
"scrollX": 11, "scrollX": 11.046099290780141,
"scrollY": -5, "scrollY": -5,
"scrolledOutside": false, "scrolledOutside": false,
"selectedElementIds": Object { "selectedElementIds": Object {
@ -20350,7 +20350,7 @@ Object {
"zenModeEnabled": false, "zenModeEnabled": false,
"zoom": Object { "zoom": Object {
"translation": Object { "translation": Object {
"x": -60.420000000000016, "x": -59.425,
"y": -48.66347517730496, "y": -48.66347517730496,
}, },
"value": 1.99, "value": 1.99,

View File

@ -21,7 +21,6 @@ import type { ResolvablePromise } from "./utils";
import { Spreadsheet } from "./charts"; import { Spreadsheet } from "./charts";
import { Language } from "./i18n"; import { Language } from "./i18n";
export type FlooredNumber = number & { _brand: "FlooredNumber" };
export type Point = Readonly<RoughPoint>; export type Point = Readonly<RoughPoint>;
export type Collaborator = { export type Collaborator = {
@ -68,8 +67,8 @@ export type AppState = {
currentItemEndArrowhead: Arrowhead | null; currentItemEndArrowhead: Arrowhead | null;
currentItemLinearStrokeSharpness: ExcalidrawElement["strokeSharpness"]; currentItemLinearStrokeSharpness: ExcalidrawElement["strokeSharpness"];
viewBackgroundColor: string; viewBackgroundColor: string;
scrollX: FlooredNumber; scrollX: number;
scrollY: FlooredNumber; scrollY: number;
cursorButton: "up" | "down"; cursorButton: "up" | "down";
scrolledOutside: boolean; scrolledOutside: boolean;
name: string; name: string;