Skip to content

Commit

Permalink
feat: Graphing canvas resizing
Browse files Browse the repository at this point in the history
  • Loading branch information
NriotHrreion committed May 26, 2024
1 parent 05a1547 commit cafcd52
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/renderer/Graphics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@ export default class Graphics {
this.canvas.height = this.canvas.height;
}

protected resize(width: number, height: number) {
const oldWidth = this.canvas.width;
const oldHeight = this.canvas.height;

this.canvas.width = width;
this.canvas.height = height;

this.center.x = width * (this.center.x / oldWidth);
this.center.y = height * (this.center.y / oldHeight);
}

// MARK: Utilities

private getMaxDrawingRange(): number {
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/Render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ export default class Render extends Graphics {
this.center.y -= centerDy * this.scale;
}

public handleResize(width: number, height: number) {
this.resize(width, height);
}

private refreshMousePoint(rect: DOMRect, cx: number, cy: number) {
var mousePoint = this.createPoint(cx - rect.left, cy - rect.top);
this.mousePoint = mousePoint;
Expand Down
12 changes: 12 additions & 0 deletions src/views/graphing/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ const Graphing: React.FC = memo(() => {
if(!workerRef.current) return;
workerRef.current.postMessage({ type: "wheel", dy: e.deltaY });
});
window.addEventListener("resize", () => {
if(!workerRef.current) return;

const width = Utils.getElem("display-frame").clientWidth;
const height = Utils.getElem("display-frame").clientHeight;
canvas.style.width = width +"px";
canvas.style.height = height +"px";
canvas.width = width * window.devicePixelRatio;
canvas.height = height * window.devicePixelRatio;

workerRef.current.postMessage({ type: "resize", width, height });
});

// Init mobile events
var lastTouch: Touch | null = null;
Expand Down
3 changes: 3 additions & 0 deletions src/workers/graphing.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ ctx.addEventListener("message", (e) => {
case "touch-zoom":
renderer.handleTouchZoom(req.rect, req.cxA, req.cyA, req.cxB, req.cyB);
break;
case "resize":
renderer.handleResize(req.width, req.height);
break;
case "theme-change":
req.isDarkMode
? renderer.config.setTheme(Theme.DARK)
Expand Down

1 comment on commit cafcd52

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.