javascript - 调整大小脚本会放大,而不是缩小。使用行列式会让事情变得紧张

标签 javascript resize geometry determinants pythagorean

我构建了以下调整大小脚本 - 正如您从 fiddle 中看到的那样,它适用于放大而不是缩小:

https://jsfiddle.net/calipoop/a37aeb08/

var item = document.getElementById('item'), 
btn = document.getElementById('resizeBtn'),
s = 1;

btn.addEventListener('mousedown', initResize);

function initResize(e) {
  var startS = s, startX = e.clientX, startY = e.clientY, dx, dy, d;
  window.addEventListener('mousemove', resize);
  window.addEventListener('mouseup', killResize);

  function resize(e) {
    dx = startX - e.clientX;
    dy = startY - e.clientY;
    d = Math.round(Math.sqrt(dx * dx + dy * dy));
    s = startS + (d * .001);
    item.style.transform = "scale(" + s + ")";
  }

  function killResize() {
    window.removeEventListener('mousemove', resize);
    window.removeEventListener('mouseup', killResize);
  }
}

我尝试通过使用行列式/叉积来解决正/负方向的问题:

https://jsfiddle.net/calipoop/zbx3us36/

det = (startX*e.clientY) - (e.clientX*startY);
dir = (det >= 0) ? 1 : -1; //get direction from sign of determinant
d = Math.round(Math.sqrt(dx * dx + dy * dy)) * dir;

正如你从 fiddle 中看到的,现在它“有时”会缩小,而且总是不稳定。

数学专业有想法吗?我是否错误地得到了行列式?预先感谢您的任何见解。

最佳答案

当我第一次看到它时,我发现无法使用负整数,所以我像这样改变了你的函数:

function initResize(e) {
  var startS = s,
    startX = e.clientX,
    startY = e.clientY,
    dx, dy, d;
  window.addEventListener('mousemove', resize);
  window.addEventListener('mouseup', killResize);

  function resize(e) {
    dx = startX - e.clientX;
    dy = startY - e.clientY;
    negative = false;
    if (dx < 0 || dy < 0) negative = true;
    d = Math.abs(Math.round(Math.sqrt(dx * dx + dy * dy))); //Always postive, need a way to determine if should be negative.
    if (!negative) d = (d * -1);
    s = startS + (d * .001);

    item.style.transform = "scale(" + s + ")";
  }

你可以在这里看到它: https://jsfiddle.net/gregborbonus/a37aeb08/5/

因此,在您发表评论后,我进行了进一步编辑,以解决初始位置问题。

您缩放的所有内容都是根据距起点的距离确定的,那么您将鼠标移动到距离调整点多远或靠近调整大小点?

这意味着,即使你停止向 Y 方向移动,但稍微移动了 X 一点,Y 仍然会大于你从 X 方向的移动,使其基于 Y 进行缩放,但你没有移动 Y,你只是移动了十.

因此,此函数根据您上次调整大小时移动鼠标的距离得出:

function initResize(e) {
  var startS = s,
    startX = e.clientX,
    startY = e.clientY,
    dx, dy, d;
  window.addEventListener('mousemove', resize);
  window.addEventListener('mouseup', killResize);

  function resize(e) {
    //The problem with this logic is that while resizing, you calculate based on distance FROM your start, this is not always the best approach, so what you need is the difference from your last recorded position.

    dx = startX - e.clientX;
    dy = startY - e.clientY;
    startX=e.clientX; //for the next run
    startY=e.clientY; //for the next run
    negative = false;
    if (dx < 0 || dy < 0) negative = true;
    //d = Math.abs(Math.round(Math.sqrt(dx * dx + dy * dy))); //Always postive, need a way to determine if should be negative.
    d=Math.max(Math.abs(dx),Math.abs(dy))*3; //Lets figure out which is the max and do the math off that.
    if (!negative) d = (d * -1);
    s = startS + (d * .001);
    startS=s; //Set the scale for the next run.

    item.style.transform = "scale(" + s + ")";
  }

  function killResize() {

    window.removeEventListener('mousemove', resize);
    window.removeEventListener('mouseup', killResize);
  }
}

https://jsfiddle.net/gregborbonus/a37aeb08/6/

关于javascript - 调整大小脚本会放大,而不是缩小。使用行列式会让事情变得紧张,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36390105/

相关文章:

optimization - 如何使用 ImageMagick 从具有不同大小和纵横比的图像制作蒙太奇?

javascript - 使用圆数学进行固体碰撞

c++ - 如何将平面网格转换为CGAL中的排列

javascript - 行为发生奇怪的变化 - Windows 8.1 中的 &lt;textarea&gt;

javascript - 新元素的CSS过渡

javascript - 在 javascript 中为所有浏览器设置 innerHTML

javascript - 随着时间的推移,链接 addEventListeners 将导致性能低下

html - 在页面调整大小时拆分 Bootstrap 行

html - 如何使用 CSS 调整电子邮件中动态图像的大小?

image-processing - 检测图像中的圆形的可能快速方法有哪些?