gpt4 book ai didi

draw - Kinetic JS - 使用鼠标在一行的开头和结尾绘制箭头

转载 作者:行者123 更新时间:2023-12-02 05:09:59 24 4
gpt4 key购买 nike

我正在尝试将箭头添加到我使用鼠标绘制的线条的开头和结尾,这是我绘制线条的脚本:

    document.getElementById('dLine').onclick = function() {
layer.on("mousedown", function () {
if (moving) {
moving = false;
layer.draw();
} else {
var mousePos = stage.getMousePosition();
x1 = mousePos.x;
y1 = mousePos.y;
line = new Kinetic.Line({
points: [0, 0, 50, 50],
stroke: "red"
});
layer.add(line);
line.getPoints()[0].x = mousePos.x;
line.getPoints()[0].y = mousePos.y;
line.getPoints()[1].x = mousePos.x;
line.getPoints()[1].y = mousePos.y;
moving = true;
layer.drawScene();
}
});

layer.on("mousemove", function () {
if (moving) {
var mousePos = stage.getMousePosition();
var x = mousePos.x;
var y = mousePos.y;
line.getPoints()[1].x = mousePos.x;
line.getPoints()[1].y = mousePos.y;
moving = true;
layer.drawScene();
}
});

layer.on("mouseup", function () {
moving = false;
var mousePos = stage.getMousePosition();
x2 = mousePos.x;
y2 = mousePos.y;
$("#distance").val(calculateDistance(x1, y1, x2, y2));

});
};

非常感谢您的建议。

最佳答案

您可以使用此函数绘制箭头:

function canvas_arrow(fromx, fromy, tox, toy){
var headlen = 20; // how long you want the head of the arrow to be, you could calculate this as a fraction of the distance between the points as well.
var angle = Math.atan2(toy-fromy,tox-fromx);

line = new Kinetic.Line({
points: [fromx, fromy, tox, toy, tox-headlen*Math.cos(angle-Math.PI/6),toy-headlen*Math.sin(angle-Math.PI/6),tox, toy, tox-headlen*Math.cos(angle+Math.PI/6),toy-headlen*Math.sin(angle+Math.PI/6)],
stroke: "red"
});
}

编辑:jsfiddle.net/cmcculloh/M56w4 - 在评论中提供

关于draw - Kinetic JS - 使用鼠标在一行的开头和结尾绘制箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15628838/

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