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

后端 未结 3 966
死守一世寂寞
死守一世寂寞 2020-12-01 07:14

I am wondering if there is a way to combine multiple images into a single image using only JavaScript. Is this something that Canvas will be able to do. The effect can be

相关标签:
3条回答
  • 2020-12-01 07:31

    I don't think you can or would want to do this with client side javascript ("combing them into a single image for download"), because it's running on the client: even if you could combine them into a single image file on the client, at that point you've already downloaded all of the individual images, so the merge is pointless.

    0 讨论(0)
  • 2020-12-01 07:48

    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>
    
    0 讨论(0)
  • 2020-12-01 07:48

    MarvinJ provides the method combineByAlpha() in which combines multiple images using its alpha channel. Therefore, you just need to have your images in a format that supports transparency, like PNG, and use that method, as follow:

    Marvin.combineByAlpha(image, imageOver, imageOutput, x, y);
    

    image1:

    image2:

    image3:

    Result:

    Runnable Example:

    var canvas = document.getElementById("canvas");
    image1 = new MarvinImage();
    image1.load("https://i.imgur.com/ChdMiH7.jpg", imageLoaded);
    image2 = new MarvinImage();
    image2.load("https://i.imgur.com/h3HBUBt.png", imageLoaded);
    image3 = new MarvinImage();
    image3.load("https://i.imgur.com/UoISVdT.png", imageLoaded);
    
    var loaded=0;
    
    function imageLoaded(){
      if(++loaded == 3){
        var image = new MarvinImage(image1.getWidth(), image1.getHeight());
        Marvin.combineByAlpha(image1, image2, image, 0, 0);
        Marvin.combineByAlpha(image, image3, image, 190, 120);
        image.draw(canvas);
      }
    }
    <script src="https://www.marvinj.org/releases/marvinj-0.8.js"></script>
    <canvas id="canvas" width="450" height="297"></canvas>

    0 讨论(0)
提交回复
热议问题