简体   繁体   English

相对于HTML5画布缩放文档上的鼠标坐标

[英]scale mouse coordinates on document relative to HTML5 canvas

I have a HTML5 canvas of a certain size on the page 我在页面上有一定尺寸的HTML5画布

<canvas id="canvas" width="200" height="100" style="border:1px solid #000000;">

Right now, the canvas is painted when the mouse is dragged over it (ie you click first(not necessarily inside the canvas) then start dragging the mouse over it without releasing) Script below; 现在,当鼠标被拖到画布上时(例如,您首先单击(不一定在画布内)然后在不释放的情况下开始将鼠标拖到画布上)就绘制了画布。

  $(function () {
  var c = document.getElementById("canvas");
  var context = c.getContext("2d");
  var clickX = new Array();
  var clickY = new Array();
  var clickDrag = new Array();
  var paint;
  var $canvas=$("#canvas");

  $(this).mousedown(function (e) {         

      paint = true;
      addClick(e.pageX - $canvas[0].offsetLeft, e.pageY - $canvas[0].offsetTop);
      redraw();
  });


  $(this).mousemove(function (e) {
      if (paint) {
          addClick(e.pageX - $canvas[0].offsetLeft, e.pageY - $canvas[0].offsetTop, true);
          redraw();
      }
  });

  $(this).mouseup(function (e) {
      paint = false;
  });

  $(this).mouseleave(function (e) {
      paint = false;
  });

  function addClick(x, y, dragging) {
      clickX.push(x);
      clickY.push(y);
      clickDrag.push(dragging);
  }

  function redraw() {
      context.clearRect(0, 0, context.canvas.width, context.canvas.height); // Clears the canvas

      context.strokeStyle = "#000000";
      context.lineJoin = "round";
      context.lineWidth = 2;

      for (var i = 0; i < clickX.length; i++) {
          context.beginPath();
          if (clickDrag[i] && i) {
              context.moveTo(clickX[i - 1], clickY[i - 1]);
          } else {
              context.moveTo(clickX[i] - 1, clickY[i]);
          }
          context.lineTo(clickX[i], clickY[i]);
          context.closePath();
          context.stroke();
      }
  }

});

What I need now is a way to scale all the mouse coordinates on document during the document's mouse events to the relative coordinates inside the canvas. 我现在需要的是一种在文档的鼠标事件期间将文档上所有鼠标坐标缩放为画布内的相对坐标的方法。

so that no matter wherever you drag the mouse on the document it is drawn inside the canvas (in relatively small size of course). 这样,无论您在文档上的什么位置拖动鼠标,它都将在画布内绘制(当然,尺寸较小)。 Any Idea how to achieve this? 任何想法如何实现这一目标?

http://jsfiddle.net/umwc5/3/ http://jsfiddle.net/umwc5/3/

Why I need this? 为什么我需要这个?

It is for a signature application, When a user scribbles the signature using a tablet on a page(without seeing the page!) the entire signature is to be registered in a small canvas. 它用于签名应用程序,当用户使用平板电脑在页面上涂鸦签名(看不见页面!)时,整个签名将被注册在一个小画布中。

Update 更新

The final working fiddle 最后的工作提琴

The most important thing you were missing here was to multiply by the canvas/screen ratio. 您在这里错过的最重要的事情是乘以画布/屏幕比例。

First calculate the ratio: 首先计算比例:

  var docToCanv = Math.min($canvas[0].width / $('body').width(), $canvas[0].height/$('body').height());

Then use it like this: 然后像这样使用它:

addClick(e.pageX*docToCanv, e.pageY*docToCanv);

Depending on the additional behavior you want, you may need to adjust the location a bit, but this should get you past the current issue you are having. 根据您想要的其他行为,您可能需要稍微调整位置,但这可以使您摆脱当前遇到的问题。

Demo 演示

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

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