我正在尝试通过单击并拖动鼠标来绘制画布。我的问题是,除了这条线的质量很差(我想要一个更明显的边框)之外,它只在鼠标位置为 0,0 时才尊重鼠标位置。当我将鼠标移动到下角时,线条与它的距离增加了,就像我在画布中间时一样,线条已经超出了它。我的代码位于:http: //jsfiddle.net/ajTkP/12/
我也会在这里发布:
var MDown = false;
var Color = 'blue';
var Canvas = document.getElementById('canvas');
var Context = Canvas.getContext('2d');
Canvas.onselectstart = function() { return false; };
Canvas.unselectable = "on";
Canvas.style.MozUserSelect = "none";
Canvas.onmousedown = function(e) {
MDown = true;
Context.strokeStyle = Color;
Context.lineWidth = 3;
Context.lineCap = 'round';
Context.beginPath();
Context.moveTo(e.pageX - Position(Canvas).left, e.pageY - 5);
}
Canvas.onmouseup = function() { MDown = false; };
Canvas.onmousemove = function(e) {
if (MDown) {
Context.lineTo(e.pageX - Position(Canvas).left, e.pageY - 5);
Context.stroke();
}
}
function Position(el) {
var position = {left: 0, top: 0};
if (el) {
if (!isNaN(el.offsetLeft) && !isNaN(el.offsetTop)) {
position.left += el.offsetLeft;
position.top += el.offsetTop;
}
}
return position;
}
谢谢你的帮助!