首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在mouseDrag上旋转

在mouseDrag上旋转
EN

Stack Overflow用户
提问于 2019-11-08 08:05:44
回答 1查看 539关注 0票数 2

你好,我想在mouseDraggedmousePressed上旋转网格。我的代码是工作的,但当我停止鼠标按下旋转到起点,而不是重新绘制网格。请指导我如何重新绘制网格后,我完成拖动或鼠标按到它的终点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-08 08:16:44

造成此问题的原因是,变量angle声明为本地,并在每个帧中初始化为0。

将变量声明移动到全局范围。例如:

代码语言:javascript
复制
let angle = 0;

function draw() {

    // [...]

    if (mouseIsPressed) {
        angle =  atan2(mouseY - height / 2, mouseX - width / 2);
    }

    // [...]
}

根据评论,分机。

如何从其末端位置开始再次拖动

使用mouseReleased()事件回调来管理角度。添加永久角和当前角:

代码语言:javascript
复制
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 );

     // [...]
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58762703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档