gpt4 book ai didi

javascript - 如何使用 JavaScript 和 Raphael 旋转之前拖动的 SVG 对象?

转载 作者:行者123 更新时间:2023-11-29 10:22:56 27 4
gpt4 key购买 nike

我正在使用 Raphael 库来旋转和拖动图像。我正确地拖动了它,并且我有一个按钮,每次按下它时它都会正确旋转 90 度。但是,当我旋转图像、拖动它然后再次尝试旋转时,我遇到了问题。

HTML 中仅有的元素是用于放置 Raphael Canvas 的 div 和用于旋转图像的按钮。 JavaScript 在这里:

var gboard;
var piece;

window.onload = function ()
{
gboard = Raphael("gameboard", 800, 500);
// piece = gboard.rect(100, 100, 50, 50).attr({fill: "#0CF", "fill-opacity": 1, stroke: "none", cursor: "move"});
piece = gboard.image("piece.gif", 100, 100, 50, 50).attr({cursor: "move"});
piece.drag(dragMove, dragStart, dragStop);
var angle = 0;

document.getElementById('btn').onclick = function () {
angle += 90;
var cx = piece.attr('x') + 25;
var cy = piece.attr('y') + 25;
piece.animate({transform: "R" + angle + ", " + cx + ", " + cy + ""}, 500, "<>");
// piece.transform("R90," + cx + "," + cy);
};

}

// Set up the object for dragging
function dragStart()
{
this.ox = this.attr("x");
this.oy = this.attr("y");
}

// Clean up after dragging ends
function dragStop() {}

/**
* Handle the moving of objects when dragging
* Check the current angle to compensate for rotated coordinate system.
*/
function dragMove(dx, dy)
{
var angle = getAngle(this._['deg']);
if (angle == 90)
this.attr({x: this.ox + dy, y: this.oy - dx});
else if (angle == 180)
this.attr({x: this.ox - dx, y: this.oy - dy});
else if (angle == 270)
this.attr({x: this.ox - dy, y: this.oy + dx});
else // angle == 0
this.attr({x: this.ox + dx, y: this.oy + dy});
}

/**
* Get the simplified equivalent angle (0 <= angle <= 360) for the given angle.
*
* @param deg The angle in degrees
* @return The equivalent angle between 0 and 360
*/
function getAngle(deg)
{
if (deg % 360 == 0)
return 0;
else if (deg < 0)
{
while (deg < 0)
deg += 360;
}
else if (deg > 360)
{
while (deg > 360)
deg -= 360;
}

return deg;
}

最佳答案

我通过在值更改时重新应用所有转换来解决这个问题。

http://alias.io/raphael/free_transform/

来源:https://github.com/ElbertF/Raphael.FreeTransform

关于javascript - 如何使用 JavaScript 和 Raphael 旋转之前拖动的 SVG 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7749303/

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