Skip to content

Commit cafcd52

Browse files
committed
feat: Graphing canvas resizing
1 parent 05a1547 commit cafcd52

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

src/renderer/Graphics.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,17 @@ export default class Graphics {
221221
this.canvas.height = this.canvas.height;
222222
}
223223

224+
protected resize(width: number, height: number) {
225+
const oldWidth = this.canvas.width;
226+
const oldHeight = this.canvas.height;
227+
228+
this.canvas.width = width;
229+
this.canvas.height = height;
230+
231+
this.center.x = width * (this.center.x / oldWidth);
232+
this.center.y = height * (this.center.y / oldHeight);
233+
}
234+
224235
// MARK: Utilities
225236

226237
private getMaxDrawingRange(): number {

src/renderer/Render.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ export default class Render extends Graphics {
208208
this.center.y -= centerDy * this.scale;
209209
}
210210

211+
public handleResize(width: number, height: number) {
212+
this.resize(width, height);
213+
}
214+
211215
private refreshMousePoint(rect: DOMRect, cx: number, cy: number) {
212216
var mousePoint = this.createPoint(cx - rect.left, cy - rect.top);
213217
this.mousePoint = mousePoint;

src/views/graphing/index.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ const Graphing: React.FC = memo(() => {
111111
if(!workerRef.current) return;
112112
workerRef.current.postMessage({ type: "wheel", dy: e.deltaY });
113113
});
114+
window.addEventListener("resize", () => {
115+
if(!workerRef.current) return;
116+
117+
const width = Utils.getElem("display-frame").clientWidth;
118+
const height = Utils.getElem("display-frame").clientHeight;
119+
canvas.style.width = width +"px";
120+
canvas.style.height = height +"px";
121+
canvas.width = width * window.devicePixelRatio;
122+
canvas.height = height * window.devicePixelRatio;
123+
124+
workerRef.current.postMessage({ type: "resize", width, height });
125+
});
114126

115127
// Init mobile events
116128
var lastTouch: Touch | null = null;

src/workers/graphing.worker.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ ctx.addEventListener("message", (e) => {
5050
case "touch-zoom":
5151
renderer.handleTouchZoom(req.rect, req.cxA, req.cyA, req.cxB, req.cyB);
5252
break;
53+
case "resize":
54+
renderer.handleResize(req.width, req.height);
55+
break;
5356
case "theme-change":
5457
req.isDarkMode
5558
? renderer.config.setTheme(Theme.DARK)

0 commit comments

Comments
 (0)