• <tfoot id='n77YZ'></tfoot>
    <legend id='n77YZ'><style id='n77YZ'><dir id='n77YZ'><q id='n77YZ'></q></dir></style></legend>

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

    • <bdo id='n77YZ'></bdo><ul id='n77YZ'></ul>

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

        调整矩形 HTML5 画布的大小

        时间:2023-08-02
          • <i id='ABXjZ'><tr id='ABXjZ'><dt id='ABXjZ'><q id='ABXjZ'><span id='ABXjZ'><b id='ABXjZ'><form id='ABXjZ'><ins id='ABXjZ'></ins><ul id='ABXjZ'></ul><sub id='ABXjZ'></sub></form><legend id='ABXjZ'></legend><bdo id='ABXjZ'><pre id='ABXjZ'><center id='ABXjZ'></center></pre></bdo></b><th id='ABXjZ'></th></span></q></dt></tr></i><div id='ABXjZ'><tfoot id='ABXjZ'></tfoot><dl id='ABXjZ'><fieldset id='ABXjZ'></fieldset></dl></div>
              <tbody id='ABXjZ'></tbody>
            • <bdo id='ABXjZ'></bdo><ul id='ABXjZ'></ul>

              <legend id='ABXjZ'><style id='ABXjZ'><dir id='ABXjZ'><q id='ABXjZ'></q></dir></style></legend>

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

                  <tfoot id='ABXjZ'></tfoot>
                • 本文介绍了调整矩形 HTML5 画布的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一些函数可以在画布元素上绘制矩形.绘制元素时,我希望能够通过拖动它的角来调整它的大小.

                  I have some functions to draw rectangles on a canvas element. When the element is drawn, I want to be able to resize it by dragging its corners.

                  var canvas = document.getElementById('canvas'),
                    ctx = canvas.getContext('2d'),
                    rect = {},
                    drag = false;
                  
                  function init() {
                    canvas.addEventListener('mousedown', mouseDown, false);
                    canvas.addEventListener('mouseup', mouseUp, false);
                    canvas.addEventListener('mousemove', mouseMove, false);
                  }
                  
                  function mouseDown(e) {
                    rect.startX = e.pageX - this.offsetLeft;
                    rect.startY = e.pageY - this.offsetTop;
                    drag = true;
                  }
                  
                  function mouseUp() {
                    drag = false;
                  }
                  
                  function mouseMove(e) {
                    if (drag) {
                      rect.w = (e.pageX - this.offsetLeft) - rect.startX;
                      rect.h = (e.pageY - this.offsetTop) - rect.startY;
                      ctx.clearRect(0, 0, canvas.width, canvas.height);
                      draw();
                    }
                  }
                  
                  function draw() {
                    ctx.fillRect(rect.startX, rect.startY, rect.w, rect.h);
                  }
                  
                  init();

                  <canvas id="canvas" width="500" height="500"></canvas>

                  推荐答案

                  确保使用某种阈值来检查拐角处的拖动,使用 closeEnough 变量来保持此阈值,然后检查通过查看角点和鼠标点之间的差值的绝对值是否小于阈值来确定角点.除此之外,还有很多案例需要处理.这是一个jsFiddle

                  Make sure to use some kind of threshold value to check for dragging on corners, use a closeEnough variable to hold this threshold then check corners by seeing if the absolute value of the difference between corner point and mouse point is less than the threshold. Apart from that, it is just a lot of cases to go through. Here is a jsFiddle of it

                  var canvas = document.getElementById('canvas'),
                      ctx = canvas.getContext('2d'),
                      rect = {},
                      drag = false,
                      mouseX, 
                      mouseY,
                      closeEnough = 10,
                      dragTL=dragBL=dragTR=dragBR=false;
                  
                  function init() {
                    canvas.addEventListener('mousedown', mouseDown, false);
                    canvas.addEventListener('mouseup', mouseUp, false);
                    canvas.addEventListener('mousemove', mouseMove, false);
                  }
                  
                  function mouseDown(e) {
                    mouseX = e.pageX - this.offsetLeft;
                    mouseY = e.pageY - this.offsetTop;
                  
                    // if there isn't a rect yet
                    if(rect.w === undefined){
                      rect.startX = mouseY;
                      rect.startY = mouseX;
                      dragBR = true;
                    }
                  
                    // if there is, check which corner
                    //   (if any) was clicked
                    //
                    // 4 cases:
                    // 1. top left
                    else if( checkCloseEnough(mouseX, rect.startX) && checkCloseEnough(mouseY, rect.startY) ){
                      dragTL = true;
                    }
                    // 2. top right
                    else if( checkCloseEnough(mouseX, rect.startX+rect.w) && checkCloseEnough(mouseY, rect.startY) ){
                      dragTR = true;
                  
                    }
                    // 3. bottom left
                    else if( checkCloseEnough(mouseX, rect.startX) && checkCloseEnough(mouseY, rect.startY+rect.h) ){
                      dragBL = true;
                  
                    }
                    // 4. bottom right
                    else if( checkCloseEnough(mouseX, rect.startX+rect.w) && checkCloseEnough(mouseY, rect.startY+rect.h) ){
                      dragBR = true;
                  
                    }
                    // (5.) none of them
                    else {
                      // handle not resizing
                    }
                  
                    ctx.clearRect(0,0,canvas.width,canvas.height);
                    draw();
                  
                  }
                  
                  function checkCloseEnough(p1, p2){
                    return Math.abs(p1-p2)<closeEnough;
                  }
                  function mouseUp() {
                    dragTL = dragTR = dragBL = dragBR = false;
                  }
                  
                  function mouseMove(e) {
                    mouseX = e.pageX - this.offsetLeft;
                    mouseY = e.pageY - this.offsetTop;
                    if(dragTL){
                      rect.w += rect.startX-mouseX;
                      rect.h += rect.startY-mouseY;
                      rect.startX = mouseX;
                      rect.startY = mouseY;
                    } else if(dragTR) {
                      rect.w = Math.abs(rect.startX-mouseX);
                      rect.h += rect.startY-mouseY;
                      rect.startY = mouseY;
                    } else if(dragBL) {
                      rect.w += rect.startX-mouseX;
                      rect.h = Math.abs(rect.startY-mouseY);
                      rect.startX = mouseX;  
                    } else if(dragBR) {
                      rect.w = Math.abs(rect.startX-mouseX);
                      rect.h = Math.abs(rect.startY-mouseY);
                    }
                      ctx.clearRect(0,0,canvas.width,canvas.height);
                      draw();
                  }
                  
                  function draw() {
                    ctx.fillRect(rect.startX, rect.startY, rect.w, rect.h);
                  }
                  
                  init();
                  

                  这篇关于调整矩形 HTML5 画布的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 javascript 渲染瓦片地图 下一篇:如何从 Codepen 获取代码并在本地使用?

                  相关文章

                  • <bdo id='J4gbf'></bdo><ul id='J4gbf'></ul>

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

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