简体   繁体   English

我如何才能在 <canvas> ?

[英]How can I capture multiple images out of one in the <canvas>?

I wrote some code that draws a semi-transparent rectangle over an image that you can draw with touch: 我写了一些代码,在可以触摸的图像上绘制一个半透明的矩形:

function drawRect() {
  var canvas = document.getElementById('receipt');
  var ctx = canvas.getContext('2d');
  var drag = false;
  var imageObj;
  var rect = { };
  var touch;

  canvas.width = WIDTH;
  canvas.height = HEIGHT;

  function init() {
    imageObj = new Image();

    imageObj.src = 'img.jpg';
    imageObj.onload = function() {
      ctx.drawImage(imageObj, 0, 0);
    };

    canvas.addEventListener('touchstart', handleTouch, false);
    canvas.addEventListener('touchmove', handleTouch, false);
    canvas.addEventListener('touchleave', handleEnd, false);
    canvas.addEventListener('touchend', handleEnd, false);
  }

  function handleTouch(event) {
     if (event.targetTouches.length === 1) {
       touch = event.targetTouches[0];

     if (event.type == 'touchmove') {
        if (drag) {
          rect.w = touch.pageX - rect.startX;
          rect.h = touch.pageY - rect.startY ;
          draw();
        }
      } else {
        rect.startX = touch.pageX;
        rect.startY = touch.pageY;
        drag = true;
      }
    }
  }

  function handleEnd(event) {
    drag = false;
  }

  function draw() {
    drawImageOnCanvas();
    ctx.fillStyle = 'rgba(0, 100, 255, 0.2)';
    ctx.fillRect(rect.startX, rect.startY, rect.w, rect.h);
  } 

  function drawImageOnCanvas() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.drawImage(imageObj, 0, 0);
  }

  init();
}

This works. 这可行。 But, now I'd like to have it so that I can capture each of the parts of the image that are in rectangles as separate images. 但是,现在我想拥有它,以便可以将矩形中的图像的每个部分捕获为单独的图像。

How can I pull this off? 我怎样才能做到这一点? Because I have that redraw stuff, it deletes the previous rectangle, which makes this difficult. 因为我拥有重绘的内容,所以它删除了前一个矩形,这使此操作变得很困难。

A canvas can only save out the whole canvas as an image, so the trick is to create a temporary canvas the size of the region you want to save out. 画布只能将整个画布另存为图像,因此诀窍是创建一个临时画布,其大小与要保存的区域相同。

One way could be to create a function which takes the image and a rectangle object as argument and returns a data-uri (or Blob) of the region: 一种方法是创建一个函数,该函数将图像和矩形对象作为参数并返回该区域的data-uri(或Blob):

function saveRegion(img, rect) {

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

   canvas.width = rect.w;
   canvas.height = rect.h;
   ctx.drawImage(img, rect.startX, rect.startY, rect.w, rect.h, 0, 0, rect.w, rect.h);

   return canvas.toDataURL():
}

You can pass in the original image if don't want any graphics on top, or if you draw elements on top of it just pass in the original canvas element as image source. 如果不需要顶部的任何图形,或者在顶部绘制元素,只需传入原始画布元素作为图像源,就可以传递原始图像。 And of course, CORS-restrictions apply. 当然,CORS限制也适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM