繁体   English   中英

使用箭头键发布旋转画布形状

[英]Issue Rotating Canvas Shape WIth Arrow Keys

我希望左右箭头键分别将画布形状顺时针和逆时针旋转。 当前,形状仅线性移动。

从长远来看,我试图使用此javascript代码复制ROS(机器人操作系统)TurtleSim的运动,并且左右键会以此方式旋转turtlesim。 (我对javascript很陌生。)

<script>
function Parent(){
    //diffColor = false;
    mainCanvas.load();
    tracker = new track(30, 50, "white", 30, 120); //create object that will move with keys;

    click();
    //touch();
    //animate();
    //mapCanvas.load();
}

function click(){
        window.addEventListener("click", getClickPosition, false);

    function getClickPosition(e){
        tracker.distanceX = e.clientX - (tracker.width / 2); //move tracker to near center of tracker; clientX gets horizontal coordinate of cursor
        tracker.distanceY = e.clientY - (tracker.height / 2);

        }
}


var mainCanvas = {
    canvas : document.createElement("canvas"),
    load: function(){

        this.canvas.width = (window.innerWidth)/2;
        this.canvas.height = window.innerHeight;
        this.ctx1 = this.canvas.getContext("2d");

        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
        this.interval = setInterval(moveTracker, 20);

        window.addEventListener ("keydown", function(e){
            console.log(e.keyCode);
            mainCanvas.key = e.keyCode; //execute movement when key pressed
        });
        window.addEventListener ("keyup", function(e){
                mainCanvas.key = false; //stop movement once key is released
                });

    },

    clear: function(){
        this.ctx1.clearRect(0, 0, this.canvas.width, this.canvas.height);
    }
}

    function track(width, height, color, distanceX, distanceY, theSquare){ 
    this.width = width;
    this.height = height;
    this.speedX = 0;
    this.speedY = 0;
    this.distanceX = distanceX;
    this.distanceY = distanceY;
    this.rotationSpeedRight = 0;
    this.rotationSpeedLeft= 0;
    this.rotationLeft = rotationLeft;
    this.rotationRight = rotationRight;
    console.log("inside track()");

    this.update = function(theSquare){
        ctx = mainCanvas.ctx1;
        ctx.fillStyle = color;
        ctx.fillRect(this.distanceX, this.distanceY, this.width, this.height, this.rotationLeft, this.rotationRight);
        ctx.rotate(45*Math.PI/180);
        ctx.save();
        ctx.restore();
    }
    this.newPosition = function(){
    this.rotation += this.rotationSpeed;
    this.distanceX += this.speed * Math.cos(this.rotation);
    this.distanceY += this.speed * Math.sin(this.rotation);
    }
}

function moveTracker(){ //recognize keys from keyboard
    mainCanvas.clear();
    tracker.speedX = 0;
    tracker.speedY = 0;
    tracker.rotationSpeedRight = 0;
    tracker.rotationSpeedLeft = 0;
    if (mainCanvas.key && mainCanvas.key == 37) //left key; should move anticlockwise
        tracker.rotationSpeedLeft = -1;
    if (mainCanvas.key && mainCanvas.key == 38) //down key
        tracker.speedY = -1;
    if (mainCanvas.key && mainCanvas.key == 39) //right key; should move clockwise;
        tracker.rotationSpeedRight = 1;
    if (mainCanvas.key && mainCanvas.key == 40) //up key
        tracker.speedY=1;

    tracker.newPosition();
    tracker.update();
}

没有“向左旋转”和“向右旋转”这样的东西,它们都指相同的东西。 您只需要一个旋转值,它是图形的当前角度。

我还假设您希望向上键朝着您所面对的方向而不是一直向上,因此您也可以将速度值也切换为一个值,即当前方向的速度。 基本上,这会将您的坐标系从笛卡尔 (x,y)更改为极坐标 (角度和距离)。
要了解基于旋转和速度的运动在XY平面上的最终变化,您必须对X使用speed * cos(angle)对Y使用speed * sin(angle) (基于三角函数)。

在绘制矩形之前,必须先调用rotate (基本上是说“我接下来要做的所有事情都需要旋转那个量”),然后围绕所有这些调用saverestore ,以在取消矩形后取消旋转完成绘制旋转形状。

另一个注意事项: rotate使画布绕原点(0,0)旋转。 要绕元素中心旋转(这可能是您想要做的),您需要首先translate移到该位置,然后不要忘记偏移绘制矩形的位置以考虑该初始平移。

您的代码底部的可能更新为:

function track(width, height, color, distanceX, distanceY, rotation){
    this.width = width;
    this.height = height;
    this.distanceX = distanceX || 0;
    this.distanceY = distanceY || 0;
    this.speed = 0;
    this.rotation = rotation || 0;
    this.rotationSpeed = 0;

    this.update = function(){
        ctx = mainCanvas.ctx1;
        ctx.fillStyle = color;
        ctx.save();
        ctx.translate(this.distanceX, this.distanceY);
        ctx.rotate(this.rotation);
        ctx.fillRect(-this.width / 2, -this.height / 2, this.width, this.height);
        ctx.restore();
    }
    this.newPosition = function(){
        this.rotation += this.rotationSpeed;
        this.distanceX += this.speed * Math.cos(this.rotation);
        this.distanceY += this.speed * Math.sin(this.rotation);
    }
}

function moveTracker(){ //recognize keys from keyboard
    mainCanvas.clear();
    tracker.speed = 0;
    tracker.rotationSpeed = 0;
    // Adjust the values as you need here
    if (mainCanvas.key == 37) //left key
        tracker.rotationSpeed = -0.5 / Math.PI;
    if (mainCanvas.key == 38) //up key
        tracker.speed = 3;
    if (mainCanvas.key == 39) //right key
        tracker.rotationSpeed = 0.5 / Math.PI;
    if (mainCanvas.key == 40) //down key
        tracker.speed = -3;

    tracker.newPosition();
    tracker.update();
}

JSFiddle (粗糙版本)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM