fix exporting bg for non-cropped exports

- also refactor and add comments to `exportAsPNG`
This commit is contained in:
dwelle 2020-01-02 18:11:58 +01:00
parent e260904244
commit f091e9813e

View File

@ -29,10 +29,18 @@ function newElement(type, x, y, width = 0, height = 0) {
return element; return element;
} }
function exportAsPNG({ background, visibleOnly, padding = 10 }) { function exportAsPNG({
exportBackground,
exportVisibleOnly,
exportPadding = 10
}) {
// deselect & rerender
clearSelection(); clearSelection();
drawScene(); drawScene();
// calculate visible-area coords
let subCanvasX1 = Infinity; let subCanvasX1 = Infinity;
let subCanvasX2 = 0; let subCanvasX2 = 0;
let subCanvasY1 = Infinity; let subCanvasY1 = Infinity;
@ -45,40 +53,54 @@ function exportAsPNG({ background, visibleOnly, padding = 10 }) {
subCanvasY2 = Math.max(subCanvasY2, getElementAbsoluteY2(element)); subCanvasY2 = Math.max(subCanvasY2, getElementAbsoluteY2(element));
}); });
let targetCanvas = canvas; // create temporary canvas from which we'll export
if ( visibleOnly ) { const tempCanvas = document.createElement("canvas");
targetCanvas = document.createElement('canvas'); const tempCanvasCtx = tempCanvas.getContext("2d");
targetCanvas.style.display = 'none'; tempCanvas.style.display = "none";
document.body.appendChild(targetCanvas); document.body.appendChild(tempCanvas);
targetCanvas.width = subCanvasX2 - subCanvasX1 + padding * 2; tempCanvas.width = exportVisibleOnly
targetCanvas.height = subCanvasY2 - subCanvasY1 + padding * 2; ? subCanvasX2 - subCanvasX1 + exportPadding * 2
const targetCanvas_ctx = targetCanvas.getContext('2d'); : canvas.width;
tempCanvas.height = exportVisibleOnly
? subCanvasY2 - subCanvasY1 + exportPadding * 2
: canvas.height;
if ( background ) { if (exportBackground) {
targetCanvas_ctx.fillStyle = "#FFF"; tempCanvasCtx.fillStyle = "#FFF";
targetCanvas_ctx.fillRect(0, 0, canvas.width, canvas.height); tempCanvasCtx.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'); // copy our original canvas onto the temp canvas
link.setAttribute('download', 'excalibur.png'); tempCanvasCtx.drawImage(
link.setAttribute('href', targetCanvas.toDataURL("image/png")); canvas, // source
exportVisibleOnly // sx
? subCanvasX1 - exportPadding
: 0,
exportVisibleOnly // sy
? subCanvasY1 - exportPadding
: 0,
exportVisibleOnly // sWidth
? subCanvasX2 - subCanvasX1 + exportPadding * 2
: canvas.width,
exportVisibleOnly // sHeight
? subCanvasY2 - subCanvasY1 + exportPadding * 2
: canvas.height,
0, // dx
0, // dy
exportVisibleOnly ? tempCanvas.width : canvas.width, // dWidth
exportVisibleOnly ? tempCanvas.height : canvas.height // dHeight
);
// create a temporary <a> elem which we'll use to download the image
const link = document.createElement("a");
link.setAttribute("download", "excalibur.png");
link.setAttribute("href", tempCanvas.toDataURL("image/png"));
link.click(); link.click();
// clean up the DOM
link.remove(); link.remove();
if ( targetCanvas !== canvas ) targetCanvas.remove(); if (tempCanvas !== canvas) tempCanvas.remove();
} }
function rotate(x1, y1, x2, y2, angle) { function rotate(x1, y1, x2, y2, angle) {
@ -280,9 +302,9 @@ class App extends React.Component {
<div className="exportWrapper"> <div className="exportWrapper">
<button onClick={() => { <button onClick={() => {
exportAsPNG({ exportAsPNG({
background: this.state.exportBackground, exportBackground: this.state.exportBackground,
visibleOnly: this.state.exportVisibleOnly, exportVisibleOnly: this.state.exportVisibleOnly,
padding: this.state.exportPadding exportPadding: this.state.exportPadding
}) })
}}>Export to png</button> }}>Export to png</button>
<label> <label>