From 33ea19fef60f01e165fbd590bffb5ae9244d97f7 Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Fri, 20 Mar 2020 17:56:34 +1100 Subject: [PATCH] Moved tokens to be relatively positioned and scaled to the current map --- src/components/AddMapButton.js | 45 ++++++++++++++--- src/components/Map.js | 88 +++++++++++++++++++++++----------- src/components/ProxyToken.js | 25 +++++++--- src/components/Token.js | 13 +---- src/components/Tokens.js | 6 ++- src/routes/Game.js | 25 +++++----- 6 files changed, 136 insertions(+), 66 deletions(-) diff --git a/src/components/AddMapButton.js b/src/components/AddMapButton.js index 5a67a02..7d8820e 100644 --- a/src/components/AddMapButton.js +++ b/src/components/AddMapButton.js @@ -1,5 +1,13 @@ -import React, { useRef, useState } from "react"; -import { IconButton, Box, Button, Image, Flex, Label, Input } from "theme-ui"; +import React, { useRef, useState, useEffect } from "react"; +import { + IconButton, + Box, + Button, + Image as UIImage, + Flex, + Label, + Input +} from "theme-ui"; import Modal from "./Modal"; @@ -22,11 +30,26 @@ function AddMapButton({ handleMapChange }) { setIsAddModalOpen(false); } + const [imageLoaded, setImageLoaded] = useState(false); + const mapDataRef = useRef(null); - const [mapSource, setImageSource] = useState(null); + const [mapSource, setMapSource] = useState(null); function handleImageUpload(event) { - mapDataRef.current = { file: event.target.files[0], rows, columns }; - setImageSource(URL.createObjectURL(mapDataRef.current.file)); + const file = event.target.files[0]; + const url = URL.createObjectURL(file); + let image = new Image(); + image.onload = function() { + mapDataRef.current = { + file, + rows, + columns, + width: image.width, + height: image.height + }; + setImageLoaded(true); + }; + image.src = url; + setMapSource(url); } function handleDone() { @@ -38,6 +61,12 @@ function AddMapButton({ handleMapChange }) { const [rows, setRows] = useState(defaultMapSize); const [columns, setColumns] = useState(defaultMapSize); + useEffect(() => { + if (mapDataRef.current) { + mapDataRef.current.rows = rows; + mapDataRef.current.columns = columns; + } + }, [rows, columns]); return ( <> @@ -70,7 +99,7 @@ function AddMapButton({ handleMapChange }) { /> - {mapSource ? ( - + ) : (