* Initial factoring out of parts of the LayerUI component

2360 → 2224 LOC

* Create a Section component

* Break up src/index.tsx

* Refactor actions to reduce duplication, fix CSS

Also consolidate icons

* Move scene/data.ts to its own directory

* Fix accidental reverts, banish further single-character variables

* ACTIVE_ELEM_COLOR → ACTIVE_ELEMENT_COLOR

* Further refactoring the icons file

* Log all errors

* Pointer Event polyfill to make the tests work

* add test hooks & fix tests

Co-authored-by: dwelle <luzar.david@gmail.com>
This commit is contained in:
Jed Fox 2020-03-07 10:20:38 -05:00 committed by GitHub
parent 1a6431a04a
commit c6a0cfc2b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
49 changed files with 3498 additions and 3372 deletions

View file

@ -1,3 +1,6 @@
import { FlooredNumber } from "./types";
import { getZoomOrigin } from "./scene";
export const SVG_NS = "http://www.w3.org/2000/svg";
export function getDateTime() {
@ -124,9 +127,57 @@ export function distance(x: number, y: number) {
export function distance2d(x1: number, y1: number, x2: number, y2: number) {
const xd = x2 - x1;
const yd = y2 - y1;
return Math.sqrt(xd * xd + yd * yd);
return Math.hypot(xd, yd);
}
export function resetCursor() {
document.documentElement.style.cursor = "";
}
export function viewportCoordsToSceneCoords(
{ clientX, clientY }: { clientX: number; clientY: number },
{
scrollX,
scrollY,
zoom,
}: {
scrollX: FlooredNumber;
scrollY: FlooredNumber;
zoom: number;
},
canvas: HTMLCanvasElement | null,
) {
const zoomOrigin = getZoomOrigin(canvas);
const clientXWithZoom = zoomOrigin.x + (clientX - zoomOrigin.x) / zoom;
const clientYWithZoom = zoomOrigin.y + (clientY - zoomOrigin.y) / zoom;
const x = clientXWithZoom - scrollX;
const y = clientYWithZoom - scrollY;
return { x, y };
}
export function sceneCoordsToViewportCoords(
{ sceneX, sceneY }: { sceneX: number; sceneY: number },
{
scrollX,
scrollY,
zoom,
}: {
scrollX: FlooredNumber;
scrollY: FlooredNumber;
zoom: number;
},
canvas: HTMLCanvasElement | null,
) {
const zoomOrigin = getZoomOrigin(canvas);
const sceneXWithZoomAndScroll =
zoomOrigin.x - (zoomOrigin.x - sceneX - scrollX) * zoom;
const sceneYWithZoomAndScroll =
zoomOrigin.y - (zoomOrigin.y - sceneY - scrollY) * zoom;
const x = sceneXWithZoomAndScroll;
const y = sceneYWithZoomAndScroll;
return { x, y };
}