我一直在尝试创建一个绘图应用程序,作为其中一项功能,我希望用户画一条直线。我希望用户单击然后拖动鼠标,当他们放开第二个点时,将从他们开始和结束的地方绘制一条直线。
但是到目前为止,它只是从中心绘制一条对 Angular 线,设置长度可以帮助任何人,这是问题所在的代码
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
};
最佳答案
您可以使用第二个临时 Canvas 让用户拖拽您的线条。
操作大纲:
创建第二个临时屏幕外 Canvas ,其大小与屏幕上的 Canvas 完全相同。
鼠标按下时:
- 将临时 Canvas 恰好移动到常规 Canvas 之上
- 保存起始拖动XY
- 设置一个标志,表示拖动已经开始
鼠标移动时:
- 清除临时 Canvas
- 在临时 Canvas 上:从开始拖动 XY 到当前鼠标 XY 绘制一条线
在 mouseup 或 mouseout 上:
- 拖动结束清除拖动标志
- 将临时 Canvas 移出屏幕
- 在主 Canvas 上:从起始拖动 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>
关于javascript - Canvas 点对点用户线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21658103/