diff --git a/src/index.js b/src/index.js index bce89ba8..c8999af9 100644 --- a/src/index.js +++ b/src/index.js @@ -6,18 +6,70 @@ import "./styles.css"; var elements = []; -function newElement(type, x, y) { +function newElement(type, x, y, width = 0, height = 0) { const element = { type: type, x: x, y: y, - width: 0, - height: 0, + width: width, + height: height, isSelected: false }; return element; } +function exportAsPNG({ background, visibleOnly, padding = 10 }) { + clearSelection(); + drawScene(); + + let subCanvasX1 = Infinity; + let subCanvasX2 = 0; + let subCanvasY1 = Infinity; + let subCanvasY2 = 0; + + elements.forEach(element => { + subCanvasX1 = Math.min(subCanvasX1, getElementAbsoluteX1(element)); + subCanvasX2 = Math.max(subCanvasX2, getElementAbsoluteX2(element)); + subCanvasY1 = Math.min(subCanvasY1, getElementAbsoluteY1(element)); + subCanvasY2 = Math.max(subCanvasY2, getElementAbsoluteY2(element)); + }); + + let targetCanvas = canvas; + + 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'); + + if ( background ) { + targetCanvas_ctx.fillStyle = "#FFF"; + targetCanvas_ctx.fillRect(0, 0, canvas.width, canvas.height); + } + + targetCanvas_ctx.drawImage( + canvas, + subCanvasX1 - padding, // x + subCanvasY1 - padding, // y + subCanvasX2 - subCanvasX1 + padding * 2, // width + subCanvasY2 - subCanvasY1 + padding * 2, // height + 0, + 0, + targetCanvas.width, + targetCanvas.height + ); + } + + 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(); +} + function rotate(x1, y1, x2, y2, angle) { // πβ²π₯=(ππ₯βππ₯)cosπβ(ππ¦βππ¦)sinπ+ππ₯ // πβ²π¦=(ππ₯βππ₯)sinπ+(ππ¦βππ¦)cosπ+ππ¦. @@ -150,7 +202,7 @@ function clearSelection() { class App extends React.Component { componentDidMount() { this.onKeyDown = event => { - if (event.key === "Backspace") { + if (event.key === "Backspace" && event.target.nodeName !== "INPUT") { for (var i = elements.length - 1; i >= 0; --i) { if (elements[i].isSelected) { elements.splice(i, 1); @@ -188,7 +240,10 @@ class App extends React.Component { super(); this.state = { draggingElement: null, - elementType: "selection" + elementType: "selection", + exportBackground: false, + exportVisibleOnly: true, + exportPadding: 10 }; } @@ -210,7 +265,40 @@ class App extends React.Component { ); }; - return ( + return <> +