我一直在尝试创建绘图应用程序,并且作为其中一项功能,我希望用户绘制一条直线。我希望用户单击然后拖动鼠标,当他们放开时,将辅助点绘制为从它们开始和结束的位置绘制一条直线。
然而到目前为止,它只是从中心绘制一条对角线起点,设定长度可以帮助这是问题所在的代码
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
};
答案 0 :(得分:1)
您可以使用第二个临时画布让用户拖动您的线条。
如何做的概述:
创建第二个临时屏幕外画布,其尺寸与屏幕画布完全相同。
关于mousedown:
关于mousemove:
关于mouseup或mouseout:
以下是代码和演示: 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>