gpt4 book ai didi

Javascript 两个对象相交

转载 作者:可可西里 更新时间:2023-11-01 12:55:49 24 4
gpt4 key购买 nike

我一直在使用 Konva 进行绘图,当箭头的尖端与其他组或形状相交并且用户松开鼠标时,我希望箭头“捕捉”到其他组或形状。如果箭头不与一个箭头相交,那么它应该自动删除它自己。

然后当移动组或形状时,我希望箭头的尖端随之移动。

我找到了一个类似的例子,但我不确定如何将它们组合起来以获得我想要的东西。

我将在下面发布我当前的代码。

示例链接

Click here

代码

var width = height = 170;

var stage = new Konva.Stage({
container: 'container',
width: width,
height: height
});

var layer = new Konva.Layer();
var isDrawArrow;
var Startpos;
var Endpos;

var arrow = new Konva.Arrow({
points: [],
pointerLength: 10,
pointerWidth: 10,
fill: 'black',
stroke: 'black',
strokeWidth: 4
});

var circle = new Konva.Circle({
x: stage.getWidth() / 2,
y: stage.getHeight() / 2,
radius: 20,
fill: 'green'
});

var circleA = new Konva.Circle({
x: stage.getWidth() / 5,
y: stage.getHeight() / 5,
radius: 30,
fill: 'red',
draggable: true
});

circle.on('mouseover', function() {
document.body.style.cursor = 'pointer';
layer.draw()
});

circle.on('mouseout', function() {
document.body.style.cursor = 'default';
layer.draw()
});

circle.on('mousedown touchstart', function() {
isDrawArrow = true;
circleA.on('dragmove', adjustPoint);
Startpos = stage.getPointerPosition();
});

stage.addEventListener('mouseup touchend', function() {
isDrawArrow = false;
});


stage.addEventListener('mousemove touchmove', function() {
if (!isDrawArrow) return;
Endpos = stage.getPointerPosition()

var p = [Startpos.x, Startpos.y, Endpos.x, Endpos.y];
arrow.setPoints(p);
layer.add(arrow);
layer.batchDraw();
});


circle.on('mouseup', function() {
this.setFill('green');
layer.batchDraw();
});


function adjustPoint(e) {
var p = [circle.getX(), circle.getY(), circleA.getX(), circleA.getY()];

arrow.setPoints(p);
layer.draw();
stage.draw();

}

function haveIntersection(r1, r2) {
return !(
r2.x > r1.x + r1.width ||
r2.x + r2.width < r1.x ||
r2.y > r1.y + r1.height ||
r2.y + r2.height < r1.y
);
}

layer.add(circle);
layer.add(circleA);

stage.add(layer);
adjustPoint();
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.3.0/konva.js"></script>
<div id="container"></div>

最佳答案

要进行捕捉,您需要一个函数来确定两点之间的距离。
通过毕达哥拉斯计算轻松完成,(如果您需要帮助,请阅读 here )。

  • 当您检测到箭头末端和您的点(在本例中为中心或红色圆圈)之间的距离小于您想要的距离时,鼠标移动,您可以“捕捉它”,即您对函数 adjustPoint 所做的一切都很好。

  • 在鼠标向上时你还需要检查距离,如果太远就隐藏箭头

下面的工作代码:

var width = height = 170;

var stage = new Konva.Stage({
container: 'container',
width: width,
height: height
});

var layer = new Konva.Layer();
var isDrawArrow, Startpos, Endpos;
var snapDistance = 20;

function distance(p, c) {
var dx = p.x - c.getX();
var dy = p.y - c.getY();
return Math.sqrt(dx * dx + dy * dy);
}

var arrow = new Konva.Arrow({
points: [],
pointerLength: 10,
pointerWidth: 10,
fill: 'black',
stroke: 'black',
strokeWidth: 4
});

var circle = new Konva.Circle({
x: stage.getWidth() - 25,
y: stage.getHeight() - 25,
radius: 20,
fill: 'green'
});

var circleA = new Konva.Circle({
x: stage.getWidth() / 5,
y: stage.getHeight() / 5,
radius: 25,
fill: 'red',
draggable: true
});

circle.on('mousedown touchstart', function() {
isDrawArrow = true;
circleA.on('dragmove', adjustPoint);
Startpos = stage.getPointerPosition();
});

stage.addEventListener('mouseup touchend', function() {
isDrawArrow = false;
if (distance(Endpos, circleA) > snapDistance) {
arrow.hide();
layer.batchDraw();
}
});

stage.addEventListener('mousemove touchmove', function() {
if (!isDrawArrow) return;
Endpos = stage.getPointerPosition()

var p = [Startpos.x, Startpos.y, Endpos.x, Endpos.y];
arrow.setPoints(p);
arrow.show();
layer.add(arrow);
layer.batchDraw();

if (distance(Endpos, circleA) <= snapDistance) {
adjustPoint();
isDrawArrow = false
}
});

function adjustPoint(e) {
var p = [circle.getX(), circle.getY(), circleA.getX(), circleA.getY()];
arrow.setPoints(p);
layer.draw();
stage.draw();
}

layer.add(circle);
layer.add(circleA);
stage.add(layer);
canvas {
border: 1px solid #eaeaea !IMPORTANT;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.3.0/konva.js"></script>
<div id="container"></div>

关于Javascript 两个对象相交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52350144/

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