English 中文(简体)
Canvas - 拍摄两幅图像,回去一只 im子?
原标题:Canvas - Combing two images, return one img html object?

我有两件带有不同北极圈的html子。 我将这两幅图像(利用信使)结合起来,并制作一个合并的图像。

www.un.org/Depts/DGACM/index_spanish.htm 这是可能的吗? 如何?

最佳回答

你可以从信道上提取两幅图像,并将图像与你喜欢的任何超时模式结合起来。 从你可以到DataURL。 仅指出,这两种图像都应来自与网页相同的领域,否则,由于安全原因,你无法获得六氯苯数据。

问题回答

You could use drawImage. Demo. Code:

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

var img1 = loadImage( http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png , main);
var img2 = loadImage( http://introcs.cs.princeton.edu/java/31datatype/peppers.jpg , main);

var imagesLoaded = 0;
function main() {
    imagesLoaded += 1;

    if(imagesLoaded == 2) {
        // composite now
        ctx.drawImage(img1, 0, 0);
        
        ctx.globalAlpha = 0.5;
        ctx.drawImage(img2, 0, 0);
    }
}

function loadImage(src, onload) {
    // http://www.thefutureoftheweb.com/blog/image-onload-isnt-being-called
    var img = new Image();
    
    img.onload = onload;
    img.src = src;

    return img;
}

适应需要:

You can find the solution here. Maybe it will help you.

超文本代码:

<div id="container">
    <input type="file" id="imageLoader" name="imageLoader" />
    <input type="file" id="iimageLoader" name="imageLoader" />
    <canvas id="imageCanvas" width="300" height="300"></canvas> 
    <a id="lnkDownload" href="#">Save image</a>
</div>

JS Code:

var canvas = new fabric.Canvas( imageCanvas , {
  backgroundColor:  rgb(240,240,240) 
});
canvas.setWidth(300);
canvas.setHeight(300);

var imageLoader = document.getElementById( imageLoader );
imageLoader.addEventListener( change , handleImage, false);

function handleImage(e) {
  var objects = canvas.getObjects();
  for (var i in objects) {
    objects[i].remove();
  }
  var reader = new FileReader();
  reader.onload = function(event) {
    var img = new Image();
    img.onload = function() {
      var imgInstance = new fabric.Image(img, {
        selectable: 1
      })
      canvas.add(imgInstance);
      /* var red = new fabric.Rect({ width: 80, height: 50, fill:  red })
      canvas.add(red);
      red.center(); */
      canvas.deactivateAll().renderAll();
    }

    var iimageLoader = document.getElementById( iimageLoader );
    iimageLoader.addEventListener( change , handleImage, false);

    function handleImage(f) {
      var objects = canvas.getObjects();
      /* for (var i in objects) {
        objects[i].remove();
      } */
      var reader = new FileReader();
      reader.onload = function(event) {

        var fimg = new Image();
        fimg.onload = function() {
          var fimgInstance = new fabric.Image(fimg, {
            selectable: 1,
            width: 80,
            height: 50
          })
          canvas.add(fimgInstance);
          fimgInstance.center();
          /* var red = new fabric.Rect({ width: 80, height: 50, fill:  red })
          canvas.add(red);
          red.center(); */
          canvas.deactivateAll().renderAll();
        }
        fimg.src = event.target.result;
      }
      reader.readAsDataURL(f.target.files[0]);
    }

    img.src = event.target.result;

  }
  reader.readAsDataURL(e.target.files[0]);
}



var imageSaver = document.getElementById( lnkDownload );
imageSaver.addEventListener( click , saveImage, false);

function saveImage(e,f) {
  this.href = canvas.toDataURL({
    format:  png ,
    quality: 0.8
  });
  this.download =  canvas.png 
}

I m使用结构。 j 因为它更加方便





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签