javascript - Canvas绘图工具问题

标签 javascript jquery canvas

我正在通过开发一个绘图工具来练习 JavaScript,但我有一些关于它的问题。首先,这是我现在所拥有的: https://jsfiddle.net/w6kLbg2q/

(function($) {
  var canvas = document.getElementById('canvas');
  var context = canvas.getContext('2d');

  //Detect a mouse down. Set the xy coordinates
  var mouseDown = false;

  $(canvas).mousedown(function(e) {
    mouseDown = true;

    context.beginPath();
    context.moveTo(e.pageX, e.pageY);
  });

  //Detect that the mouse is moving and draw the line while the mouse is still down
  $(canvas).mousemove(function(e) {
    if (mouseDown) {
      var x = e.offsetX * 2;
      var y = e.offsetY * 2;


      context.lineTo(x, y);
      context.strokeStyle = '#000';
      context.stroke();

    }
  });

  //On mouse up, reset the coordinates
  $(canvas).mouseup(function() {
    mouseDown = false;
    context.closePath();
  });

})(jQuery);
#canvas {
  width: 400px;
  height: 400px;
  border: 1px solid;
  margin: 0 auto;
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas id="canvas">
        This text is displayed if your browser does not support HTML5 Canvas.
    </canvas>

  1. 为什么这条线没有精确地绘制在光标所在的位置?我做错了什么?
  2. 我正在使用这个 e.offsetX * 2; 因为我在某处看到了这个,但当我执行 e.pageX 时它不起作用。这是为什么?为什么需要* 2?

提前致谢!

最佳答案

Canvas 分辨率 V 显示尺寸

问题与 Canvas 分辨率和 Canvas 显示尺寸不匹配有关。

Canvas 分辨率是通过 Canvas 宽度和高度属性设置的。它们可以设置如下

<canvas id="canvasId" width="400" height="400"></canvas>

或通过脚本

canvasId.width = 400;
canvasId.height = 400;

如果您不设置这些值, Canvas 将默认为 300 x 150。

显示尺寸是页面上显示的 Canvas 的实际尺寸,通过样式属性 width 和 height 设置

<canvas id="canvasId" style="width:400px;height:400px;"></canvas>

或通过脚本

 canvasId.style.width = "400px";
 canvasId.style.height = "400px";

或在 CSS 中

 #canvasId { 
     width : 400px;
     height : 400px;
 }

解决您的问题

您的问题有两种解决方案。

首先是让显示尺寸与 Canvas 分辨率相匹配。

或者您可以使用显示尺寸和 Canvas 分辨率之间的差异来计算鼠标的比例。

var bounds = canvasId.getBoundingClientRect()
mouseScaleX = canvasId.width / bounds.width; 
mouseScaleY = canvasId.height / bounds.height; 
// then multiply the mouse coords with scales

代码示例

我已经修改了您的代码片段以缩放鼠标坐标以匹配 Canvas 分辨率。

(function($) {
  var canvas = document.getElementById('canvas');
  var context = canvas.getContext('2d');

  //Detect a mouse down. Set the xy coordinates
  var mouseDown = false;

  $(canvas).mousedown(function(e) {
    mouseDown = true;
    var bounds = e.target.getBoundingClientRect()
    mouseScaleX = e.target.width / bounds.width; 
    mouseScaleY = e.target.height / bounds.height; 

    context.beginPath();
    context.moveTo(e.offsetX * mouseScaleX, e.offsetY * mouseScaleY);
  });

  //Detect that the mouse is moving and draw the line while the mouse is still down
  $(canvas).mousemove(function(e) {
    if (mouseDown) {
      var bounds = e.target.getBoundingClientRect()
      mouseScaleX = e.target.width / bounds.width; 
      mouseScaleY = e.target.height / bounds.height; 

      var x = e.offsetX * mouseScaleX;
      var y = e.offsetY * mouseScaleY;


      context.lineTo(x, y);
      context.strokeStyle = '#000';
      context.stroke();

    }
  });

  //On mouse up, reset the coordinates
  $(canvas).mouseup(function() {
    mouseDown = false;
    context.closePath();
  });

})(jQuery);
#canvas {
  width: 400px;
  height: 400px;
  border: 1px solid;
  margin: 0 auto;
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas id="canvas">
        This text is displayed if your browser does not support HTML5 Canvas.
    </canvas>

关于javascript - Canvas绘图工具问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42443021/

相关文章:

jquery - CSS:当显示设置为无时,没有 DIV 宽度

javascript - 如果 <p> 中的文本是 X 然后 addClass

javascript - 将 C++ 代码连接到 Electron 应用程序中的 HTML Canvas ?

node.js - 在 ubuntu 14 上使用 npm 安装 Canvas

java - 如何让图片在 Canvas 上充当游戏角色?

javascript - 如何将两个箭头指向数字?

javascript - 多模态叠加

javascript - 从 JSON 文件获取元素属性并在 jQuery 函数中创建它们

javascript - Nodejs 函数在内部函数完成之前返回

javascript - 我有一个 HTML5 Canvas ,我想制作该 Canvas 的动画 GIF