Can You Combine Multiple Images into a Single One Using JavaScript

Can you combine multiple images into a single one using JavaScript?

I know this is an old question and the OP found a workaround solution, but this will work if the images and canvas are already part of the HTML page.

<img id="img1" src="imgfile1.png">
<img id="img2" src="imgfile2.png">
<canvas id="canvas"></canvas>

<script type="text/javascript">
var img1 = document.getElementById('img1');
var img2 = document.getElementById('img2');
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');

canvas.width = img1.width;
canvas.height = img1.height;

context.globalAlpha = 1.0;
context.drawImage(img1, 0, 0);
context.globalAlpha = 0.5; //Remove if pngs have alpha
context.drawImage(img2, 0, 0);
</script>

Or, if you want to load the images on the fly:

<canvas id="canvas"></canvas>
<script type="text/javascript">
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var img1 = new Image();
var img2 = new Image();

img1.onload = function() {
canvas.width = img1.width;
canvas.height = img1.height;
img2.src = 'imgfile2.png';
};
img2.onload = function() {
context.globalAlpha = 1.0;
context.drawImage(img1, 0, 0);
context.globalAlpha = 0.5; //Remove if pngs have alpha
context.drawImage(img2, 0, 0);
};

img1.src = 'imgfile1.png';
</script>

Merge two dataURIs to create a single image

You can create images using your data uris and then draw a new image that includes them using canvas. Here's a simple example:

var nloaded = 0;function checkload(event) {  nloaded++;  if (nloaded < 2) {    return;  }    var canvas = document.querySelector('canvas');  var context = canvas.getContext('2d');  context.drawImage(image1, 0, 0, 50, 50);  context.drawImage(image2, 50, 50, 100, 100);
var combined = new Image; combined.src = canvas.toDataURL('data/gif'); document.body.appendChild(combined);}
var image1 = new Image;image1.onload = checkload;image1.src = 'data:image/gif;base64,R0lGODdhAgACALMAAAAAAP///wAAAAAAAP8AAAAAAAAAAAAAAAAA/wAAAAAAAAAAAAAAAAAAAAAAAAAAACwAAAAAAgACAAAEAxBJFAA7';

var image2 = new Image;image2.onload = checkload;image2.src = 'data:image/gif;base64,R0lGODdhAgACALMAAAAAAP///wAAAAAAAP8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACwAAAAAAgACAAAEA5BIEgA7';
canvas {    display: none;}
<canvas width=100 height=100></canvas>

How can I combine multiple images into one page and print it as a pdf?

Since you didn't mention it, I'll assume html2canvas is coming from https://github.com/niklasvh/html2canvas

In that case, the issue here is that hmtl2canvas returns a Promise and that's what you're passing to verticalCanvases instead of the actual canvas element.

To fix it just transform the function in an asynchronous one so you can use async/await:

// |
// |
// v
async function HTMLtoPDF() {

function verticalCanvases(cnv1, cnv2, cnv3, cnv4) {
var newCanvas = document.createElement('canvas'),
ctx = newCanvas.getContext('2d'),
width = cnv1.width,
height = cnv1.height + cnv2.height + cnv3.height + cnv4.height;

newCanvas.width = width;
newCanvas.height = height;

[{
cnv: cnv1,
y: 0
},
{
cnv: cnv2,
y: cnv1.height
},
{
cnv: cnv3,
y: cnv1.height + cnv2.height
},
{
cnv: cnv4,
y: cnv1.height + cnv2.height + cnv3.height

}].forEach(function(n) {
ctx.beginPath();
ctx.drawImage(n.cnv, 0, n.y, width, n.cnv.height);
});

var imgdata = newCanvas.toDataURL();

return imgdata;
}

var forms = $('[id^=caspioform]');

var canvas1 = await html2canvas(forms[3]); // <--
var canvas2 = await html2canvas(forms[5]); // <--
var canvas3 = await html2canvas(forms[7]); // <--
var canvas4 = await html2canvas(forms[9]); // <--

var dURL = verticalCanvases(canvas1, canvas2, canvas3, canvas4);

var doc = new jsPDF("p", "mm", "a4");

var width = doc.internal.pageSize.getWidth();
var height = doc.internal.pageSize.getHeight();

doc.addImage(dURL, 'PNG', 0, 0, width, height);

doc.save('sample.pdf');
}

How to merge multiple uploaded images by using Multi File Upload control?

For loop iteration not working for imageload functions..

files.map((file) => {
var reader = new FileReader();
reader.onload = (event) => {
var img = new Image();
img.onload = () =>
{
addToCanvas(img);
}
img.src = reader.result;
}
reader.readAsDataURL(file);
}
)

Javascript combine Images

Fixed it like this:

var c = document.createElement('canvas');
c.width = 82;
c.height = 75;
var ctx = c.getContext("2d");
var imageObj2 = new Image();
imageObj2.onload = function (){
ctx.drawImage(imageObj2, 0, 0, 82, 75);
var imageObj1 = new Image();
imageObj1.onload = function (){
imageObj1.style.objectFit = "cover";
ctx.drawImage(imageObj1, 14, 4, 53, 53);
};
imageObj1.src = data.elephant;
};
imageObj2.src = "2.png";


Related Topics



Leave a reply



Submit