gpt4 book ai didi

javascript - 如何使用鼠标坐标在拖动时画线

转载 作者:行者123 更新时间:2023-12-02 15:59:56 25 4
gpt4 key购买 nike

我能够通过 mousedownmouseup 事件获取鼠标坐标。现在我想使用这些坐标在鼠标移动(拖动)上画一条线。我尝试使用 move,start,up 但它不起作用,所以我暂时将其留空。

这是 jsbin 演示:https://jsbin.com/kuhisesede/edit?html,console,output

如果不使用元素,raphael 中就没有点击事件。我的意思是我无法仅通过单击事件获取拉斐尔中的屏幕坐标。单击始终必须与矩形或圆形等元素关联才能获取坐标。

现在我已经能够获取坐标了,如何在鼠标拖动/鼠标移动时画一条线

如果有机构已经实现了,有什么想法吗?

代码:

<!doctype html>
<html>
<head>
<title>Editor</title>
<meta http-equiv="x-ua-compatible" content="ie=9"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.4/raphael-min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
window.onload = function ()
{
var paper = new Raphael(Raphael("container", "100%", "100%"));
var line = paper.path();

//Get coordinates
$("svg").mousedown(function (e) {
console.log("Mouse down: " + e.offsetX + " " + e.offsetY);
var x = e.offsetX;
var y = e.offsetY;
line = paper.path("M" + x + "," + y);
});
$("svg").mouseup(function (e) {
console.log("Mouse up: " + e.offsetX + " " + e.offsetY);
var x = e.offsetX;
var y = e.offsetY;
line = paper.path("L" + x + "," + "L" + y);
});

paper.set(line);
line.drag(move,start,up);
var start = function (x, y, event)
{
this.ox = this.attr("x");
this.oy = this.attr("y");

},
move = function (dx, dy)
{
this.attr({
x1: this.x + dx,
y1: this.x + dy
});

},
up = function ()
{

};
};
</script>
</head>
<body>
<div id="container">
<div id="header" style="margin-bottom: 0;">
<h1 id="title">Editor</h1>
<div id="footer"></div>
</div>
</div>
</body>
</html>

最佳答案

我希望这会对您(或其他人)有所帮助。

<!doctype html>
<html>
<head>
<title>Editor</title>
<meta http-equiv="x-ua-compatible" content="ie=9"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.4/raphael-min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
var paper;
$(function(){
paper = new Raphael("container", "100%", "100%");
c = paper.circle(50, 50, 40).attr({fill: "#29ECC7", stroke: "none", opacity: .5});

startPoint = {}
endPoint = {}

startDragFunction = function(x, y, e) {
startPoint.x = x;
startPoint.y = y;
}
endDragFunction = function(x, y, e) {
paper.path("M"+ startPoint.x +","+ startPoint.y +"L"+ (startPoint.x+endPoint.x) +","+(startPoint.y+endPoint.y));
}
draggingFunction = function(x, y) {
endPoint.x = x;
endPoint.y = y;
}
paper.set(c).drag(draggingFunction, startDragFunction, endDragFunction);
});
</script>
</head>
<body>
<div id="container">
<div id="header" style="margin-bottom: 0;">
<h1 id="title">Editor</h1>
<div id="footer"></div>
</div>
</div>
</body>
</html>

需要注意的一些重要事项:

  1. 如果您希望能够拖动某个元素,则必须用某种颜色填充它(否则您将无法拖动它)
  2. 我没有处理拖动或元素,只处理线条的绘制。
  3. “drop”函数(endDragFunction)仅指示元素已被删除。它没有有关下降点的信息。为此,我们使用draggingFunction。
  4. 拖动功能的 x、y 值是与原始点的差异。

关于javascript - 如何使用鼠标坐标在拖动时画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31277079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com