Created
March 30, 2020 04:49
-
-
Save phocks/32de710036aefd1e470483a3dc4fc285 to your computer and use it in GitHub Desktop.
Skeleton React component full window canvas auto-dpi-scaling and window-scaling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useRef, useLayoutEffect, useEffect } from "react"; | |
const d3 = { | |
...require("d3-selection"), | |
...require("d3-force"), | |
...require("d3-force-reuse") | |
}; | |
import canvasDpiScaler from "canvas-dpi-scaler"; | |
import { useWindowSize } from "@react-hook/window-size"; | |
import styles from "./styles.scss"; | |
let canvas; | |
let context; | |
const Stage = props => { | |
const canvasEl = useRef(); | |
const [windowWidth, windowHeight] = useWindowSize(); | |
useLayoutEffect(() => { | |
canvas = d3.select(canvasEl.current); | |
context = canvas.node().getContext("2d"); | |
}, []); | |
useEffect(() => {}, [props]); | |
useEffect(() => { | |
canvasDpiScaler(canvas.node(), context, windowWidth, windowHeight); | |
}, [windowWidth, windowHeight]); | |
return ( | |
<div className={styles.root}> | |
<canvas className={styles.canvas} ref={canvasEl} /> | |
</div> | |
); | |
}; | |
export default Stage; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment