<legend id='CBpCN'><style id='CBpCN'><dir id='CBpCN'><q id='CBpCN'></q></dir></style></legend>
      <i id='CBpCN'><tr id='CBpCN'><dt id='CBpCN'><q id='CBpCN'><span id='CBpCN'><b id='CBpCN'><form id='CBpCN'><ins id='CBpCN'></ins><ul id='CBpCN'></ul><sub id='CBpCN'></sub></form><legend id='CBpCN'></legend><bdo id='CBpCN'><pre id='CBpCN'><center id='CBpCN'></center></pre></bdo></b><th id='CBpCN'></th></span></q></dt></tr></i><div id='CBpCN'><tfoot id='CBpCN'></tfoot><dl id='CBpCN'><fieldset id='CBpCN'></fieldset></dl></div>

      1. <small id='CBpCN'></small><noframes id='CBpCN'>

        <tfoot id='CBpCN'></tfoot>
          <bdo id='CBpCN'></bdo><ul id='CBpCN'></ul>
      2. 如何使用 JavaScript/jQuery 选择图像的多边形区域?

        时间:2023-06-20
        <tfoot id='OjiNo'></tfoot>
          <tbody id='OjiNo'></tbody>
          <bdo id='OjiNo'></bdo><ul id='OjiNo'></ul>
        • <legend id='OjiNo'><style id='OjiNo'><dir id='OjiNo'><q id='OjiNo'></q></dir></style></legend>

                <small id='OjiNo'></small><noframes id='OjiNo'>

                <i id='OjiNo'><tr id='OjiNo'><dt id='OjiNo'><q id='OjiNo'><span id='OjiNo'><b id='OjiNo'><form id='OjiNo'><ins id='OjiNo'></ins><ul id='OjiNo'></ul><sub id='OjiNo'></sub></form><legend id='OjiNo'></legend><bdo id='OjiNo'><pre id='OjiNo'><center id='OjiNo'></center></pre></bdo></b><th id='OjiNo'></th></span></q></dt></tr></i><div id='OjiNo'><tfoot id='OjiNo'></tfoot><dl id='OjiNo'><fieldset id='OjiNo'></fieldset></dl></div>

                  本文介绍了如何使用 JavaScript/jQuery 选择图像的多边形区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我希望能够让我的用户选择他们上传的图像的特定多边形(点之间有曲线的 6-8 个顶点)区域 - 我如何使用 HTML5 和JS?我发现的唯一库允许纯矩形选择:http://odyniec.net/projects/imgareaselect/

                  I'd like to be able to let my users select a specific polygonal (6-8 vertices with curved lines between points) area of an image they upload - how do I go about doing this using HTML5 & JS? The only library I found allows purely rectangular selection: http://odyniec.net/projects/imgareaselect/

                  推荐答案

                  已经有一个库可以满足您的部分需求:polyclip.js, by Zoltan Dulac 你可以构建一个 UI 允许用户选择点,然后将数据提供给库,就完成了.

                  There's already a library that does part of what you need: polyclip.js, by Zoltan Dulac You can build a UI that allows the user to select points, then feed the data to the library and you're done.

                  这是一个 jsFiddle 演示.点击选择原始图像上的点,然后按生成按钮生成裁剪版本.

                  Here is a jsFiddle demonstration. Click to select points on the original image and press the Generate button to generate a cropped version.

                  HTML:

                  <div id="mainContent">
                      <div id="canvasDiv">
                          <br/>
                          <button id="generate" type="button">Generate
                          </button> 
                      </div>
                      <h1>Result:</h1>
                      <div class="clipParent" style="float:left;"> 
                      </div> 
                  </div>
                  

                  JS:

                  var canvasDiv = document.getElementById('canvasDiv'); 
                  canvas = document.createElement('canvas'); 
                  canvas.setAttribute('width', 500); 
                  canvas.setAttribute('height', 500); 
                  canvas.setAttribute('id', 'canvas'); 
                  $(canvasDiv).prepend(canvas); 
                  if(typeof G_vmlCanvasManager != 'undefined') { 
                      canvas = G_vmlCanvasManager.initElement(canvas); 
                  } 
                  
                  var context = canvas.getContext('2d'); 
                  var imageObj = new Image(); 
                  
                  imageObj.onload = function() {
                      $(canvas).attr({width : this.width, height: this.height});
                      context.drawImage(imageObj,0,0); 
                  }; 
                  imageObj.src = 'http://www.html5canvastutorials.com/demos/assets/darth-vader.jpg'; 
                  
                  var clickX = new Array(); 
                  var clickY = new Array(); 
                  var clickDrag = new Array(); 
                  var paint; 
                  
                  function addClick(x, y, dragging) 
                  { 
                      clickX.push(x); 
                      clickY.push(y); 
                      clickDrag.push(dragging); 
                  } 
                  
                  function redraw(){ 
                      canvas.width = canvas.width; // Clears the canvas 
                      context.drawImage(imageObj,0,0); 
                  
                      context.strokeStyle = "#df4b26"; 
                      context.lineJoin = "round"; 
                      context.lineWidth = 5; 
                  
                      for(var i=0; i < clickX.length; i++) 
                      { 
                      context.beginPath(); 
                      context.arc(clickX[i], clickY[i], 3, 0, 2 * Math.PI, false); 
                      context.fillStyle = '#ffffff'; 
                      context.fill(); 
                      context.lineWidth = 5; 
                      context.stroke(); 
                      } 
                  } 
                  
                  $('#canvas').click(function(e){ 
                      var mouseX = e.pageX - this.offsetLeft; 
                      var mouseY = e.pageY - this.offsetTop; 
                  
                      addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop); 
                      redraw(); 
                  }); 
                  
                  $('#generate').click(function(){ 
                      $(".clipParent").empty(); 
                      $(".clipParent").prepend('<img src="http://www.html5canvastutorials.com/demos/assets/darth-vader.jpg" id="genimg" />'); 
                      var arr = []; 
                      for(var i=0; i < clickX.length; i++){ 
                          arr.push(clickX[i]); 
                          arr.push(clickY[i]); 
                      } 
                      $("#genimg")[0].setAttribute("data-polyclip",arr.join(", ")); 
                      clickX=[]; 
                      clickY=[]; 
                      redraw(); 
                      polyClip.init(); 
                  });
                  

                  这篇关于如何使用 JavaScript/jQuery 选择图像的多边形区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 html5 画布中制作可点击点? 下一篇:是否可以创建没有 DOM 元素的 HTML 画布?

                  相关文章

                • <small id='k7xUU'></small><noframes id='k7xUU'>

                    <tfoot id='k7xUU'></tfoot>
                        <bdo id='k7xUU'></bdo><ul id='k7xUU'></ul>

                    1. <legend id='k7xUU'><style id='k7xUU'><dir id='k7xUU'><q id='k7xUU'></q></dir></style></legend>
                      <i id='k7xUU'><tr id='k7xUU'><dt id='k7xUU'><q id='k7xUU'><span id='k7xUU'><b id='k7xUU'><form id='k7xUU'><ins id='k7xUU'></ins><ul id='k7xUU'></ul><sub id='k7xUU'></sub></form><legend id='k7xUU'></legend><bdo id='k7xUU'><pre id='k7xUU'><center id='k7xUU'></center></pre></bdo></b><th id='k7xUU'></th></span></q></dt></tr></i><div id='k7xUU'><tfoot id='k7xUU'></tfoot><dl id='k7xUU'><fieldset id='k7xUU'></fieldset></dl></div>