我有以下代码,使我可以在画布上绘制图片,类似于MS Paint。因此,一旦用户按下鼠标左键,他/她就可以在画布上移动鼠标来绘制图片。一旦用户将手指从鼠标按钮上移开,绘图就会停止。
var colors = document.querySelectorAll('#colors div');
var link = document.getElementById('download');
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var drawing = false;
var prev;
document.getElementById('current').style.backgroundColor = context.strokeStyle;
for (var i = 0; i < colors.length; i++) {
colors[i].addEventListener('click', function(e) {
var src = e.srcElement;
var color = src.style.backgroundColor;
context.strokeStyle = color;
document.getElementById('current').style.backgroundColor = color;
});
colors[i].style.height = 100 / colors.length + '%';
colors[i].style.backgroundColor = colors[i].id;
}
function clearCanvas() {
context.clearRect(0, 0, canvas.width, canvas.height);
}
link.addEventListener('click', function() {
link.href = canvas.toDataURL();
link.download = 'image.png';
});
canvas.addEventListener('mousedown', function(e) {
drawing = true;
});
canvas.addEventListener('mousemove', function(e) {
if (drawing) {
var coord = {
'x': e.clientX - this.offsetLeft,
'y': e.clientY - this.offsetTop
};
if (prev !== undefined) {
context.beginPath();
context.moveTo(prev.x, prev.y);
context.lineTo(coord.x, coord.y);
context.stroke();
}
prev = coord;
}
});
canvas.addEventListener('mouseup', function() {
drawing = false;
prev = undefined;
});
#draw {
border: 2px outset black;
border-radius: 5px;
display: inline-block;
margin: 5px;
padding: 10px;
}
#container {
border: 2px solid black;
width: 300px;
height: 300px;
position: relative;
}
#canvas {
background-color: lightgrey;
}
#colors {
float: right;
position: relative;
width: 20%;
height: 100%;
}
#colors div {
width: 100%
}
#buttons {
width: 100%;
height: auto;
}
#buttons * {
width: 33%;
background-color: lightgrey;
margin-top: 5px;
margin-left: 5px;
display: inline;
}
#download {
text-align: center;
border-radius: 2px;
padding-left: 5px;
padding-right: 5px;
border: 2px outset lightgrey;
font-family: 'Arial';
background-color: lightgrey;
font-size: 10pt;
cursor: context-menu;
}
#download:link {
text-decoration: none;
color: black;
}
#download:active {
border: 2px inset #00acff;
border-radius: 2px;
}
#current {
height: 100%;
width: 10%;
border: 1px solid black;
display: inline;
position: relative;
margin-left: 25px;
}
<div id='draw'>
<div id='container'>
<canvas id='canvas' width='240' height='300'></canvas>
<div id='colors'>
<div id='red'></div>
<div id='blue'></div>
<div id='green'></div>
<div id='black'></div>
<div id='yellow'></div>
<div id='cyan'></div>
<div id='magenta'></div>
</div>
<div id='currentColor'></div>
</div>
<div id='buttons'>
<input type='button' id='clear' value='Clear Canvas' onclick='clearCanvas()' />
<a id='download'>Download Image</a>
<input type='button' id='current' />
</div>
</div>
http://jsfiddle.net/SirToadstool/eywraw8t/341261/
现在,尽管此代码可在JSFiddle中运行,但在外部进行测试时,用户绘制的线条与鼠标指针的坐标不同。
我感觉代码中包含的CSS可能会弄乱Javascript中如何计算坐标,但是我不确定要进行哪些更改以进行补偿,因此尝试使用clientX/Y
和screenX/Y
代表坐标,而不仅仅是event.x/event.y
。
关于在绘制时如何正确计算坐标的任何建议?
答案 0 :(得分:1)
this.offsetLeft
等于0。这就是为什么从中减去无效的原因,因为在函数范围内,“ this”是画布本身。您想使用鼠标事件offsetX \ Y来代替clientX \ Y,这很简单:
var coord = { 'x': e.offsetX, 'y': e.offsetY };
答案 1 :(得分:0)
canvas.width是绘图画布的宽度,不一定是canvas元素的宽度。这意味着画布上下文可以具有与屏幕不同的分辨率。因此,您需要相应地调整坐标。即
var coord = {
'x': (e.clientX - this.offsetLeft)*this.width/this.clientWidth,
'y': (e.clientY - this.offsetTop)*this.height/this.clientHeight
};