我一直在尝试创建一个绘图应用程序,作为功能之一,我希望用户绘制一条直线。我希望用户单击然后拖动鼠标,当他们松开次要点时,绘制一条直线,从他们开始和结束的地方。
然而,到目前为止,它只是画一条从中心起源的对角线,一个设定的长度,任何人都可以帮助这是问题所在的代码
var line = document.getElementById('line');
// Function for the line to draw
line.addEventListener('click', function () {
// Removed extra code
var putPoint2 = function (e) {
if (dragging2) {
var new_size = size/2;
context.moveTo(e.offsetX, e.offsetY);
}
};
engage2 = function (e) { // Made engage2() "global" to refer it in engage()
dragging2 = true;
putPoint2(e);
disengage2(e);
canvas.addEventListener('mousedown', putPoint2); // Repositioned event attachment
//canvas.addEventListener('mousemove', disengage2); // Repositioned event attachment
};
var disengage2 = function (e) {
dragging2 = false;
context.lineTo(e.offsetX+10, e.offsetY+10);
context.stroke();
context.beginPath();
canvas.removeEventListener('mousedown', putPoint2); // Added event detachment
canvas.removeEventListener('mouseup', disengage2); // Added event detachment
};
您可以使用第二个临时画布让用户拖动并绘制线条。
如何做到这一点的概述:
创建第二个临时屏幕外画布,其大小与屏幕画布完全相同。
鼠标按下时:
- 将临时画布精确地移动到常规画布的顶部
- 保存起始拖动 XY
- 设置一个标志,指示拖动已开始
鼠标移动时:
- 清除临时画布
- 在临时画布上:从起始拖动 XY 到当前鼠标 XY 绘制一条线
在鼠标向上或鼠标退出时:
- 拖动
- 结束,所以清除拖动标志
- 将临时画布移出屏幕
- 在主画布上:从起始拖动XY到结束鼠标XY绘制一条线
下面是代码和演示:http://jsfiddle.net/m1erickson/2FaFw/
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<style>
body{ background-color: ivory; }
#wrapper{
position:relative;
width:300px;
height:200px;
}
#canvas,#canvasTemp{
position:absolute; top:0px; left:0px;
border:1px solid blue;
width:100%;
height:100%;
}
#canvasTemp{ border:1.5px solid green; }
#canvas{ border:1px solid red; }
</style>
<script>
$(function(){
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var canvasTemp=document.getElementById("canvasTemp");
var ctxTemp=canvasTemp.getContext("2d");
var canvasOffset=$("#canvas").offset();
var offsetX=canvasOffset.left;
var offsetY=canvasOffset.top;
var startX;
var startY;
var isDown=false;
$("#canvasTemp").css({ left:-500, top:0 });
function drawLine(toX,toY,context){
context.beginPath();
context.moveTo(startX, startY);
context.lineTo(toX,toY);
context.stroke();
}
function handleMouseDown(e){
e.preventDefault();
mouseX=parseInt(e.clientX-offsetX);
mouseY=parseInt(e.clientY-offsetY);
// save drag-startXY,
// move temp canvas over main canvas,
// set dragging flag
startX=mouseX;
startY=mouseY;
ctxTemp.clearRect(0,0,canvasTemp.width,canvasTemp.height);
$("#canvasTemp").css({ left:0, top:0 });
isDown=true;
}
function handleMouseUp(e){
e.preventDefault();
if(!isDown){return;}
// clear dragging flag
// move temp canvas offscreen
// draw the user's line on the main canvas
isDown=false;
mouseX=parseInt(e.clientX-offsetX);
mouseY=parseInt(e.clientY-offsetY);
$("#canvasTemp").css({ left:-500, top:0 });
drawLine(mouseX,mouseY,ctx);
}
function handleMouseMove(e){
e.preventDefault();
if(!isDown){return;}
mouseX=parseInt(e.clientX-offsetX);
mouseY=parseInt(e.clientY-offsetY);
// clear the temp canvas
// on temp canvas draw a line from drag-start to mouseXY
ctxTemp.clearRect(0,0,canvasTemp.width,canvasTemp.height);
drawLine(mouseX,mouseY,ctxTemp);
}
$("#canvas").mousedown(function(e){handleMouseDown(e);});
$("#canvas").mousemove(function(e){handleMouseMove(e);});
$("#canvas").mouseup(function(e){handleMouseUp(e);});
$("#canvas").mouseout(function(e){handleMouseUp(e);});
}); // end $(function(){});
</script>
</head>
<body>
<div id="wrapper">
<canvas id="canvasTemp" width=300 height=200></canvas>
<canvas id="canvas" width=300 height=200></canvas>
</div>
</body>
</html>