Skip to content

Commit

Permalink
✨ Use useWindowSize in canvas components
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerrylum committed Jul 5, 2023
1 parent 0d10708 commit 455034b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/app/FieldCanvasElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ControlElement } from "./ControlElement";
import { AreaElement } from "./AreaElement";
import { UnitConverter, UnitOfLength } from "../core/Unit";
import { FieldCanvasConverter } from "../core/Canvas";
import { clamp } from "../core/Util";
import { clamp, useWindowSize } from "../core/Util";
import { AddPath, AddSegment } from "../core/Command";
import { useAppStores } from "../core/MainApp";
import { RobotElement } from "./RobotElement";
Expand Down Expand Up @@ -73,8 +73,13 @@ const PathControls = observer((props: { path: Path; fcc: FieldCanvasConverter; i
const FieldCanvasElement = observer((props: {}) => {
const { app } = useAppStores();

const windowSize = useWindowSize((newSize: Vector, oldSize: Vector) => {
const ratio = (newSize.y + oldSize.y) / 2 / oldSize.y;
app.fieldOffset = app.fieldOffset.multiply(new Vector(ratio, ratio));
});

const uc = new UnitConverter(UnitOfLength.Foot, app.gc.uol);
const canvasSizeInPx = window.innerHeight * (app.view.showSpeedCanvas ? 0.78 : 0.94);
const canvasSizeInPx = windowSize.y * (app.view.showSpeedCanvas ? 0.78 : 0.94);
const canvasSizeInUOL = uc.fromAtoB(12);

const paths = app.paths;
Expand Down
8 changes: 5 additions & 3 deletions src/app/GraphCanvasElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Konva from "konva";
import { Circle, Layer, Line, Rect, Stage, Text } from "react-konva";
import React from "react";
import { PathConfig } from "../format/Config";
import { clamp } from "../core/Util";
import { clamp, useWindowSize } from "../core/Util";
import { AddKeyframe, MoveKeyframe, RemoveKeyframe, UpdateProperties } from "../core/Command";
import { useAppStores } from "../core/MainApp";
import { KeyframeIndexing } from "../core/Calculation";
Expand Down Expand Up @@ -131,6 +131,8 @@ const KeyframeElement = observer((props: { ikf: KeyframeIndexing; gcc: GraphCanv
const GraphCanvasElement = observer((props: {}) => {
const { app, appPreferences: preferences } = useAppStores();

const windowSize = useWindowSize();

const [xOffset, setXOffset] = React.useState(0);

const path = app.interestedPath();
Expand All @@ -141,8 +143,8 @@ const GraphCanvasElement = observer((props: {}) => {

if (path === undefined) return null;

const canvasWidth = window.innerHeight * 0.78;
const canvasHeight = window.innerHeight * 0.12;
const canvasWidth = windowSize.y * 0.78;
const canvasHeight = windowSize.y * 0.12;
const gcc = new GraphCanvasConverter(canvasWidth, canvasHeight, xOffset, path);

const fontSize = canvasHeight / 8;
Expand Down

0 comments on commit 455034b

Please sign in to comment.