Skip to content

Commit

Permalink
Add the merging feature
Browse files Browse the repository at this point in the history
  • Loading branch information
youngjoon-lee committed Jul 22, 2019
1 parent 4bfbdda commit a6433b0
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<form class="form-inline">
<button class="btn btn-outline-success" type="button" id="btn-top">Top</button>
<button class="btn btn-outline-success" type="button" id="btn-bottom">Bottom</button>
<button class="btn btn-outline-success" type="button" id="btn-merge">Merge</button>
</form>
</div>
</nav>
Expand Down Expand Up @@ -205,6 +206,38 @@
};
})();

// Reference: https://zocada.com/compress-resize-images-javascript-browser/
function mergeImagesDisplayed() {
var images = document.getElementById('images');
var children = images.children;

var maxWidth = 0;
var totalHeight = 0;
for (var i = 0; i < children.length; i++) {
var image = children[i].firstChild;
if (maxWidth < image.width) {
maxWidth = image.width;
}
totalHeight += image.height;
}

const canvas = document.createElement('canvas');
canvas.width = maxWidth;
canvas.height = totalHeight;

const ctx = canvas.getContext('2d');
var y = 0;
for (var i = 0; i < children.length; i++) {
var image = children[i].firstChild;
ctx.drawImage(image, 0, y, image.width, image.height);
y += image.height;
}

ctx.canvas.toBlob((blob) => {
window.open(URL.createObjectURL(blob));
}, 'image/png', 1);
}

document.querySelector('#file-input').addEventListener("change", fileInputChanged, false);
document.querySelector('#width-input').addEventListener("keyup", resizeWidths, false);

Expand All @@ -216,4 +249,6 @@
document.body.scrollTop = document.body.scrollHeight; // For Safari
document.documentElement.scrollTop = document.documentElement.scrollHeight; // For Chrome, Firefox, IE and Opera
}, false);

document.querySelector('#btn-merge').addEventListener("click", mergeImagesDisplayed, false);
</script>

0 comments on commit a6433b0

Please sign in to comment.