你好,我想在mouseDragged或mousePressed上旋转网格。我的代码是工作的,但当我停止鼠标按下旋转到起点,而不是重新绘制网格。请指导我如何重新绘制网格后,我完成拖动或鼠标按到它的终点?
发布于 2019-11-08 08:16:44
造成此问题的原因是,变量angle声明为本地,并在每个帧中初始化为0。
将变量声明移动到全局范围。例如:
let angle = 0;
function draw() {
// [...]
if (mouseIsPressed) {
angle = atan2(mouseY - height / 2, mouseX - width / 2);
}
// [...]
}根据评论,分机。
如何从其末端位置开始再次拖动
使用mouseReleased()事件回调来管理角度。添加永久角和当前角:
let permanent_angle = 0;
function mouseReleased() {
let angle = atan2(mouseY - height / 2, mouseX - width / 2);
permanent_angle += angle;
}
function draw() {
// [...]
let current_angle = 0;
if (mouseIsPressed) {
current_angle = atan2(mouseY - height / 2, mouseX - width / 2);
}
translate(width / 2, height / 2);
rotate(permanent_angle + current_angle);
translate( -width / 2 + 20, -height / 2 +20 );
// [...]
}https://stackoverflow.com/questions/58762703
复制相似问题