【问题标题】:HTML 5: Canvas copying Pixel Data within a PolygonHTML 5:画布在多边形内复制像素数据
【发布时间】:2015-06-17 07:54:51
【问题描述】:

下午好,

我是 HTML 5 中的 Canvas 以及将其与 Javascript 一起使用的新手。但我可以看到这真的很强大,我需要帮助。我已经进行了搜索,但找不到明确的答案。也许这里有人可以提供帮助。

我在 Canvas 上创建了一个带有背景图像的上下文。现在,我想裁剪或复制包含在多边形数据中的该图像的一部分,并将其放置在屏幕上的其他位置。我知道如何创建背景图像。我知道如何在该图像上创建一个多边形,并且我知道如何将图像数据复制到屏幕的另一部分。

那么我将如何复制该多边形内的所有图像数据/像素?有一个简单的方法吗?在此先感谢您的帮助。非常感谢。

编辑: 这是我正在尝试做的一个例子: http://jsfiddle.net/MFELx/

//I have no code, but it wouldn't let me post link. Hope this is allowed.

只有我试图在没有外部库或没有 JQUERY 的情况下这样做。我希望这更有意义。再次感谢您的帮助!

再次编辑:解决方案:

好的,所以 Mark E 的解决方案奏效了。我为那些没有 JQUERY 感兴趣的人编辑了它:

HTML: 复制多边形
原始背景

CSS:

body{ background-color: ivory; }
#canvas{border:1px solid red;}

JS:

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;

var polygonData=[];
polygonData.push({x:125,y:75});
polygonData.push({x:100,y:118});
polygonData.push({x:50,y:118});
polygonData.push({x:25,y:75});
polygonData.push({x:49,y:31});
polygonData.push({x:100,y:31});
polygonData.push({x:125,y:75});


var img=new Image();
img.onload=start;
img.src='https://dl.dropboxusercontent.com/u/139992952/multple/stars1.png';
function start(){

  // draw the original background
  ctx.drawImage(img,0,0);

  // copy the existing polygon to its new position


}


function copyPolygon(newStartingX,newStartingY){

  // calculate the copy's offset versus the original
  var dx=newStartingX-polygonData[0].x;
  var dy=newStartingY-polygonData[0].y;

  // define the path of the new polygon
  ctx.beginPath();
  ctx.moveTo(polygonData[0].x+dx,polygonData[0].y+dy);
  for(var i=1;i<polygonData.length;i++){
    ctx.lineTo(polygonData[i].x+dx,polygonData[i].y+dy);
  }
  ctx.closePath();

  // clip to the path of the new polygon
  ctx.save();
  ctx.clip();

  // use the clipping version of drawImage to
  // redraw the existing canvas over the new polygon position
  // Note: with clipping, new pixels will only be drawn in the new polygon
  ctx.drawImage(canvas, 0,0,cw,ch, dx,dy,cw,ch);

  // clean up -- un-clip by resetting the context state
  ctx.restore();

}
function myFunction() {
    copyPolygon(250,75);
}

【问题讨论】:

    标签: html canvas copy polygon pixel


    【解决方案1】:

    既然您有要复制的多边形数据点,那么复制多边形的方法比使用第二个屏幕外画布更简单。

    相反,您可以:

    • 使用多边形点定义新多边形副本的路径。

    • 使用context.clip() 将所有新图形限制在该多边形副本内。

    • 将画布用作自己的图像源,并使用与新多边形与前一个多边形的距离相等的偏移量进行绘制。

    这是示例注释代码和演示:

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");
    var cw=canvas.width;
    var ch=canvas.height;
    
    var polygonData=[];
    polygonData.push({x:125,y:75});
    polygonData.push({x:100,y:118});
    polygonData.push({x:50,y:118});
    polygonData.push({x:25,y:75});
    polygonData.push({x:49,y:31});
    polygonData.push({x:100,y:31});
    polygonData.push({x:125,y:75});
    
    
    var img=new Image();
    img.onload=start;
    img.src='https://dl.dropboxusercontent.com/u/139992952/multple/stars1.png';
    function start(){
    
      // draw the original background
      ctx.drawImage(img,0,0);
    
      // copy the existing polygon to its new position
      $('#copy').click(function(){
        copyPolygon(250,75);
        $('#status').text('With the polygon copied');
      });
    
    }
    
    
    function copyPolygon(newStartingX,newStartingY){
    
      // calculate the copy's offset versus the original
      var dx=newStartingX-polygonData[0].x;
      var dy=newStartingY-polygonData[0].y;
    
      // define the path of the new polygon
      ctx.beginPath();
      ctx.moveTo(polygonData[0].x+dx,polygonData[0].y+dy);
      for(var i=1;i<polygonData.length;i++){
        ctx.lineTo(polygonData[i].x+dx,polygonData[i].y+dy);
      }
      ctx.closePath();
    
      // clip to the path of the new polygon
      ctx.save();
      ctx.clip();
    
      // use the clipping version of drawImage to
      // redraw the existing canvas over the new polygon position
      // Note: with clipping, new pixels will only be drawn in the new polygon
      ctx.drawImage(canvas, 0,0,cw,ch, dx,dy,cw,ch);
    
      // clean up -- un-clip by resetting the context state
      ctx.restore();
    
    }
    body{ background-color: ivory; }
    #canvas{border:1px solid red;}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <button id=copy>Copy the polygon</button>
    <br>
    <h4 id='status'>Original background</h4>
    <canvas id="canvas" width=400 height=250></canvas>

    【讨论】:

    • 嗨,马克。感谢您的帮助。但似乎这只是再次复制多边形而不是多边形后面的内容。我正在努力做到这一点,以便您基本上在图像上绘制一个多边形,然后让 JS 从多边形内的图像中复制所有内容。这基本上只是制作一个多边形,然后将其复制到图像的另一部分。我正在寻找的有点像 GIMPS 魔杖。您在图像上绘图,然后可以将其复制并粘贴到图像中的其他位置或另一个应用程序中。 - 有点像。我希望这是有道理的。再次感谢!
    • 此技术将抓取并复制图像上的任何点集。我只是用一个金色的多边形来说明。您可以轻松地使用捕获的一组鼠标点并复制多边形内的背景并将这些复制的像素粘贴到背景的任何位置。这是一个没有金色多边形的不同示例:jsfiddle.net/m1erickson/fcuugwd4
    • 啊,是的!我现在看到了。我没有意识到多边形已经被绘制到背景上。我在上面编辑了我的请求。虽然我想要的是不需要 JQUERY,只是因为从长远来看,我会将它用作 Apache Cordova 应用程序,并且用户可能无法连接到 Internet 以引用 JQUERY 库。所以我正在寻找一种方法来做到这一点。实际上,现在我查看了代码,我可以在这里轻松地更改它。我会尽力让你知道。谢谢!
    • 不客气。正如你可能看到的,jQuery 只是用来处理 Demo 的按钮事件——不需要 jQuery 来完成效果 :-)
    • 是的!你说的对。我在没有 JQUERY 的情况下发布了您的解决方案。再次感谢!
    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 2011-01-09
    • 2013-03-14
    • 1970-01-01
    相关资源
    最近更新 更多