From 9936d763a7147cb5a1dbb1821fe76b4be818fa30 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Mon, 14 Oct 2019 21:34:42 -0700 Subject: [PATCH] Auto zoom all images on hover. --- src/App.css | 25 ++++------- src/App.js | 120 +++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 108 insertions(+), 37 deletions(-) diff --git a/src/App.css b/src/App.css index afc3885..e1638af 100644 --- a/src/App.css +++ b/src/App.css @@ -1,22 +1,13 @@ +html, body, #root { + background-color: #000; + color: #fff; + height: 100%; +} + .App { text-align: center; } -.App-logo { - height: 40vmin; -} - -.App-header { - background-color: #282c34; - min-height: 100vh; - display: flex; - flex-direction: column; - align-items: center; - justify-content: center; - font-size: calc(10px + 2vmin); - color: white; -} - -.App-link { - color: #09d3ac; +.frame { + display: inline-block; } diff --git a/src/App.js b/src/App.js index ce9cbd2..955c93a 100644 --- a/src/App.js +++ b/src/App.js @@ -1,26 +1,106 @@ import React from 'react'; -import logo from './logo.svg'; import './App.css'; -function App() { - return ( -
-
- logo -

- Edit src/App.js and save to reload. -

- - Learn React - -
-
- ); +const ROOT = process.env.PUBLIC_URL + '/tracer/'; +const DATA_URL = ROOT + 'data.json'; + +class App extends React.Component { + constructor(props) { + super(props); + this.state = { + data: null, + isFetching: false, + isZoomed: false, + zoomFactor: 16, + x: 0, + y: 0, + }; + } + componentDidMount() { + this.fetchTracerData() + this.timer = setInterval(() => this.fetchTracerData(), 1000); + } + componentWillUnmount() { + this.timer = null; + } + fetchTracerData() { + this.setState({...this.state, isFetching: true}) + fetch(DATA_URL) + .then(response => response.json()) + .then(result => this.setState({data: result, isFetching: false})) + .catch(e => console.log(e)); + } + render() { + let imgs = []; + let { + data, + isZoomed, + x, + y, + zoomFactor, + } = this.state; + if (data !== null) { + let {ratio, timestamp, images, size} = data; + console.log('size', size); + let width = 512; + let height = (ratio * width).toFixed(); + const mouseEnter = (e) => { + console.log('mouseEnter'); + this.setState({isZoomed: true}); + }; + const mouseLeave = (e) => { + console.log('mouseLeave'); + this.setState({isZoomed: false}); + }; + const mouseMove = (e) => { + let rect = e.currentTarget.getBoundingClientRect(); + let x = e.clientX - rect.left; + let y = e.clientY - rect.top; + this.setState({x: x/width, y: y/height}); + }; + let style = { + width: width + 'px', + height: height + 'px', + imageRendering: isZoomed ? 'pixelated' : 'auto', + backgroundSize: isZoomed ? zoomFactor * size[0] + 'px ' + zoomFactor * size[1] + 'px' : 'contain', + backgroundPosition: isZoomed ? 100*x + '% ' + 100*y + '%' : 'center center', + }; + console.log('isZoomed', isZoomed); + imgs = images.map((name) => { + let url = ROOT + name + '?t=' + timestamp; + return ( +
+
) + }); + } + return ( +
+
+ + {zoomFactor} +
+
+ {imgs} +
+
+ ); + } } export default App;