简体   繁体   English

在JavaScript中沿旋转方向移动旋转的元素

[英]Moving a rotated element in the direction of the rotation in JavaScript

This is a problem where firefox works just fine, while chrome has the problem. 这是一个问题,Firefox无法正常工作,而chrome有问题。 Opera has additional problems I need to adress and IExplore I haven't bothered testing yet (don't care right now to be quite honest). Opera还有其他问题,我需要解决,而IExplore我还没有打扰测试(说实话,现在不在乎)。

I have an experiment going, where I rotate a picture of a car seen from above using JqueryRotate (sets the rotation of an element) and the left right arrow keys. 我正在进行一个实验,在该实验中,我使用JqueryRotate(设置元素的旋转)和左右方向键旋转了从上方看的汽车的图片。 The car is set to the center using javascript/jquery(works - no problem). 使用javascript / jquery将汽车设置到中心(有效-没问题)。 If the up arrow is used, the car moves up in the direction it points - here is where the problem occurs. 如果使用向上箭头,则汽车将沿其指向的方向向上移动-这就是发生问题的地方。 Don't mind the fact that you can't turn and drive at the same time, this will be fixed, no prob. 不要介意您不能同时转弯和开车的事实,这将是固定的,没有问题。 But the picture moves a bit after I have turned it and then move forward? 但是,在我转动图片后再向前移动一点吗? Why? 为什么?

the javascript: javascript:

    var angle = 0;
var degreeInRadians = 2*Math.PI/360;
var realX = 0; 
var realY = 0;

$(document).ready(function(){
    placeInCenter($("#carImage"));
});

$(document).keydown(function(e){
    //left
    if (e.keyCode == 37) { 
        rotateLeft($("#carImage"));
        return false;
    }
    //up
    if (e.keyCode == 38) { 
        moveForward($("#carImage"),1);
        return false;
    }   
    //right
    if (e.keyCode == 39) { 
        rotateRight($("#carImage"));
        return false;
    }
    //down
    if (e.keyCode == 40) { 
        //TODO move the car forward
        return false;
    }    
});



function placeInCenter(element) {
    // get viewport height and width and divide it by 2
    centerY = window.innerHeight/2;
    centerX = window.innerWidth/2;

    //set absoulte positioning on element
    $(element).css("position", "absolute");
    // place the element
    $(element).offset({ top: centerY, left: centerX });
    //update realX and realY
    realX = centerX;
    realY = centerY;
}

function rotateRight(element) {
    if(angle <360) {
        angle++;
    }
    else {
        angle=0;
    }
    element.rotate(angle);
}

function rotateLeft(element) {
    if(angle > 0) {
        angle--;
    }
    else {
        angle=359;
    }
    element.rotate(angle);
}

function moveForward(element, speed) {
    //move the element to polar cordinate (speed,angle)
    realX = realX + Math.cos(degreeInRadians * angle);
    realY = realY + Math.sin(degreeInRadians * angle);

    $(element).offset({ top: realY, left: realX });
}

the demo: http://www.perandersen.no/sandbox/fromAbove/ 演示: http : //www.perandersen.no/sandbox/fromAbove/

The problem is that WebKit seems to set offset from the bounding box of the rotated element, while Firefox offsets it from the original bounding box, and then applies the rotation. 问题在于,WebKit似乎设置了从旋转元素的边界框开始的偏移量,而Firefox将其从原始边界框偏移了,然后应用旋转度。

Place your finger at the corner of the car, rotate it, and then move it forward. 将手指放在汽车的拐角处,旋转它,然后向前移动。 The upper left corner of the car's bounding box (its furthest left and top points after rotation) will line up with your finger. 汽车边界框的左上角(旋转后最左和最远的点)将与您的手指对齐。

It seems to works fine in Firefox and WebKit if you use .css({ top: ... left: }) instead of .offset() , and set the following css style: 如果您使用.css({ top: ... left: })而不是.offset()并设置以下CSS样式,则在Firefox和WebKit中似乎可以正常工作:

#carImage {
    position: relative;
    margin-left: -100px;
    margin-top: -51px;    
}

Demo: http://jsfiddle.net/jtbowden/2aeyP/2/ 演示: http//jsfiddle.net/jtbowden/2aeyP/2/

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

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