simply state management (#1628)

* simply viewport size state management

* lint
This commit is contained in:
Bruce Martin
2020-07-15 10:31:42 -07:00
committed by GitHub
parent 1269e188be
commit 68cc0ae16c
2 changed files with 11 additions and 50 deletions
+3 -18
View File
@@ -191,7 +191,7 @@ class Graph extends React.Component {
camera: null, camera: null,
modelTF, modelTF,
modelInvTF: mat3.invert([], modelTF), modelInvTF: mat3.invert([], modelTF),
projectionTF: null, projectionTF: createProjectionTF(viewport.width, viewport.height),
// regl state // regl state
regl: null, regl: null,
@@ -220,14 +220,6 @@ class Graph extends React.Component {
componentDidMount() { componentDidMount() {
window.addEventListener("resize", this.handleResize); window.addEventListener("resize", this.handleResize);
// create all default rendering transformations
const { viewport } = this.state;
const projectionTF = createProjectionTF(viewport.width, viewport.height);
this.setState({
projectionTF,
});
} }
componentDidUpdate(prevProps, prevState) { componentDidUpdate(prevProps, prevState) {
@@ -237,20 +229,11 @@ class Graph extends React.Component {
graphInteractionMode, graphInteractionMode,
} = this.props; } = this.props;
const { toolSVG, viewport } = this.state; const { toolSVG, viewport } = this.state;
let { projectionTF } = this.state;
const hasResized = const hasResized =
prevState.viewport.height !== viewport.height || prevState.viewport.height !== viewport.height ||
prevState.viewport.width !== viewport.width; prevState.viewport.width !== viewport.width;
let stateChanges = {}; let stateChanges = {};
if (hasResized) {
projectionTF = createProjectionTF(viewport.width, viewport.height);
stateChanges = {
...stateChanges,
projectionTF,
};
}
if ( if (
(viewport.height && viewport.width && !toolSVG) || // first time init (viewport.height && viewport.width && !toolSVG) || // first time init
hasResized || // window size has changed we want to recreate all SVGs hasResized || // window size has changed we want to recreate all SVGs
@@ -298,9 +281,11 @@ class Graph extends React.Component {
handleResize = () => { handleResize = () => {
const { state } = this.state; const { state } = this.state;
const viewport = this.getViewportDimensions(); const viewport = this.getViewportDimensions();
const projectionTF = createProjectionTF(viewport.width, viewport.height);
this.setState({ this.setState({
...state, ...state,
viewport, viewport,
projectionTF,
}); });
}; };
@@ -151,6 +151,7 @@ class Scatterplot extends React.PureComponent {
constructor(props) { constructor(props) {
super(props); super(props);
const viewport = this.getViewportDimensions();
this.axes = false; this.axes = false;
this.reglCanvas = null; this.reglCanvas = null;
this.renderCache = null; this.renderCache = null;
@@ -158,32 +159,18 @@ class Scatterplot extends React.PureComponent {
regl: null, regl: null,
drawPoints: null, drawPoints: null,
minimized: null, minimized: null,
viewport: { viewport,
height: null, projectionTF: createProjectionTF(width, height),
width: null,
},
projectionTF: null,
}; };
} }
componentDidMount() { componentDidMount() {
// Create render transform // this affect point render size for the scatterplot
const projectionTF = createProjectionTF(
this.reglCanvas.width,
this.reglCanvas.height
);
window.addEventListener("resize", this.handleResize); window.addEventListener("resize", this.handleResize);
const viewport = this.getViewportDimensions();
this.setState({
projectionTF,
viewport,
});
} }
componentWillUnmount() { componentWillUnmount() {
window.removeEventListener("resize", this.updateViewportDimensions); window.removeEventListener("resize", this.handleResize);
} }
setReglCanvas = (canvas) => { setReglCanvas = (canvas) => {
@@ -195,10 +182,8 @@ class Scatterplot extends React.PureComponent {
getViewportDimensions = () => { getViewportDimensions = () => {
return { return {
viewport: { height: window.innerHeight,
height: window.height, width: window.innerWidth,
width: window.width,
},
}; };
}; };
@@ -211,10 +196,6 @@ class Scatterplot extends React.PureComponent {
}); });
}; };
updateViewportDimensions = () => {
this.setState(this.getViewportDimensions());
};
fetchAsyncProps = async (props) => { fetchAsyncProps = async (props) => {
const { const {
scatterplotXXaccessor, scatterplotXXaccessor,
@@ -421,12 +402,7 @@ class Scatterplot extends React.PureComponent {
crossfilter, crossfilter,
pointDilation, pointDilation,
} = this.props; } = this.props;
const { minimized, status, regl, viewport } = this.state; const { minimized, regl, viewport } = this.state;
if (status === "error") return null;
if (regl) {
this.renderCanvas();
}
return ( return (
<div <div