From 6ec6e5d82e8dfc72f24426d292e8688e6211c3db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Bastos?= Date: Thu, 2 Jan 2020 17:04:02 +0000 Subject: [PATCH] Added the elements array to the react state --- src/index.js | 405 +++++++++++++++++++++++++++------------------------ 1 file changed, 213 insertions(+), 192 deletions(-) diff --git a/src/index.js b/src/index.js index 89c34d8f4..e18113a56 100644 --- a/src/index.js +++ b/src/index.js @@ -4,17 +4,15 @@ import rough from "roughjs/dist/rough.umd.js"; import "./styles.css"; -var elements = []; - function isInsideAnElement(x, y) { - return (element) => { - const x1 = getElementAbsoluteX1(element) - const x2 = getElementAbsoluteX2(element) - const y1 = getElementAbsoluteY1(element) - const y2 = getElementAbsoluteY2(element) + return element => { + const x1 = getElementAbsoluteX1(element); + const x2 = getElementAbsoluteX2(element); + const y1 = getElementAbsoluteY1(element); + const y2 = getElementAbsoluteY2(element); - return (x >= x1 && x <= x2) && (y >= y1 && y <= y2) - } + return x >= x1 && x <= x2 && y >= y1 && y <= y2; + }; } function newElement(type, x, y, width = 0, height = 0) { @@ -29,16 +27,16 @@ function newElement(type, x, y, width = 0, height = 0) { return element; } -function exportAsPNG({ background, visibleOnly, padding = 10 }) { - clearSelection(); - drawScene(); +function exportAsPNG(elements, { background, visibleOnly, padding = 10 }) { + const clearedElements = clearSelection(elements); + drawScene(clearedElements); let subCanvasX1 = Infinity; let subCanvasX2 = 0; let subCanvasY1 = Infinity; let subCanvasY2 = 0; - elements.forEach(element => { + clearedElements.forEach(element => { subCanvasX1 = Math.min(subCanvasX1, getElementAbsoluteX1(element)); subCanvasX2 = Math.max(subCanvasX2, getElementAbsoluteX2(element)); subCanvasY1 = Math.min(subCanvasY1, getElementAbsoluteY1(element)); @@ -47,15 +45,15 @@ function exportAsPNG({ background, visibleOnly, padding = 10 }) { let targetCanvas = canvas; - if ( visibleOnly ) { - targetCanvas = document.createElement('canvas'); - targetCanvas.style.display = 'none'; + if (visibleOnly) { + targetCanvas = document.createElement("canvas"); + targetCanvas.style.display = "none"; document.body.appendChild(targetCanvas); targetCanvas.width = subCanvasX2 - subCanvasX1 + padding * 2; targetCanvas.height = subCanvasY2 - subCanvasY1 + padding * 2; - const targetCanvas_ctx = targetCanvas.getContext('2d'); + const targetCanvas_ctx = targetCanvas.getContext("2d"); - if ( background ) { + if (background) { targetCanvas_ctx.fillStyle = "#FFF"; targetCanvas_ctx.fillRect(0, 0, canvas.width, canvas.height); } @@ -73,12 +71,12 @@ function exportAsPNG({ background, visibleOnly, padding = 10 }) { ); } - const link = document.createElement('a'); - link.setAttribute('download', 'excalibur.png'); - link.setAttribute('href', targetCanvas.toDataURL("image/png")); + const link = document.createElement("a"); + link.setAttribute("download", "excalibur.png"); + link.setAttribute("href", targetCanvas.toDataURL("image/png")); link.click(); link.remove(); - if ( targetCanvas !== canvas ) targetCanvas.remove(); + if (targetCanvas !== canvas) targetCanvas.remove(); } function rotate(x1, y1, x2, y2, angle) { @@ -185,7 +183,7 @@ function getElementAbsoluteY2(element) { return element.height >= 0 ? element.y + element.height : element.y; } -function setSelection(selection) { +function setSelection(elements, selection) { const selectionX1 = getElementAbsoluteX1(selection); const selectionX2 = getElementAbsoluteX2(selection); const selectionY1 = getElementAbsoluteY1(selection); @@ -204,14 +202,13 @@ function setSelection(selection) { }); } -function clearSelection() { - elements.forEach(element => { - element.isSelected = false; - }); +function clearSelection(elements) { + return elements.map(e => ({ ...e, isSelected: false })); } class App extends React.Component { componentDidMount() { + const { elements } = this.state; this.onKeyDown = event => { if (event.key === "Backspace" && event.target.nodeName !== "INPUT") { for (var i = elements.length - 1; i >= 0; --i) { @@ -219,7 +216,7 @@ class App extends React.Component { elements.splice(i, 1); } } - drawScene(); + drawScene(elements); event.preventDefault(); } else if ( event.key === "ArrowLeft" || @@ -236,7 +233,7 @@ class App extends React.Component { else if (event.key === "ArrowDown") element.y += step; } }); - drawScene(); + drawScene(elements); event.preventDefault(); } }; @@ -250,6 +247,7 @@ class App extends React.Component { constructor() { super(); this.state = { + elements: [], draggingElement: null, elementType: "selection", exportBackground: false, @@ -267,8 +265,8 @@ class App extends React.Component { checked={this.state.elementType === type} onChange={() => { this.setState({ elementType: type }); - clearSelection(); - drawScene(); + clearSelection(this.state.elements); + drawScene(this.state.elements); }} /> {children} @@ -276,185 +274,208 @@ class App extends React.Component { ); }; - return <> -
- -
+ + ); } } @@ -468,7 +489,7 @@ const context = canvas.getContext("2d"); // https://stackoverflow.com/questions/13879322/drawing-a-1px-thick-line-in-canvas-creates-a-2px-thick-line/13879402#comment90766599_13879402 context.translate(0.5, 0.5); -function drawScene() { +function drawScene(elements) { ReactDOM.render(, rootElement); context.clearRect(-0.5, -0.5, canvas.width, canvas.height); @@ -495,4 +516,4 @@ function drawScene() { }); } -drawScene(); +drawScene([]);