Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

鼠标拖拽计算3d变换的rotate #26

Open
damoclesX opened this issue Feb 25, 2016 · 0 comments
Open

鼠标拖拽计算3d变换的rotate #26

damoclesX opened this issue Feb 25, 2016 · 0 comments

Comments

@damoclesX
Copy link
Owner

记不得的是多久之前,弄了个3d的骰子效果,不过无法通过鼠标拖拽来计算骰子的transform的rotateX,rotateY,于是一直搁置,最近在w3cfuns看到类似效果,查看源码,发现计算的算法,记录一下

var rY = 0, rX = 0, d1, d2;

document.onmousedown = function(e){
    var X = e.clientX;
    var Y = e.clientY;

    this.onmousemove = function(e){
        var x = e.clientX,
            y = e.clientY;
            d1 = x-X;
            d2 = y-Y;
        //下面的0.2和0.07怎么来得?
        rY += d1 * 0.2; 
        rX -= d2 * 0.07; 

        oCube.style.transform = 'translateZ(-150px) rotateX('+rX+'deg) rotateY('+rY+'deg)';

        X = x;
        Y = y;
        e.preventDefault();
        return false;
    };
    this.onmouseup = function(){
        this.onmousemove = null;
    };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant