jQuery旋转光标角度 [英] Jquery-rotate cursor angle

查看:147
本文介绍了jQuery旋转光标角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用Jquery-rotate旋转箭头,但是我不知道光标的角度:( 我尝试进行计算,但是脚本不起作用.

I need rotate arrow with Jquery-rotate, but i dont know cursor's angle :( I try calculate it, but script not work.

我想要这样的结果 http://www.lonhosford.com/content /html5/canvas/rotate_to_mouse.html

<script type="text/javascript">
//<![CDATA[
    $(document).ready(function(){
        function diff(x, y) {
            var a = (x * Math.PI / 180) - Math.PI;
            var b = (y * Math.PI / 180) - Math.PI;
            return Math.atan2(Math.sin(b - a), Math.cos(b - a)) * (180 / Math.PI);
        }
        $('body').mousemove(function(e){
            var x = e.pageX;
            var y = e.pageY;
            var myAngle = diff(x, y);
            $("#image").rotate(myAngle);
        });         
    });
//]]>
</script>

推荐答案

如果查看发布的示例的源代码,您会发现

If you look at the source code of the example you posted you will find

update: function()
    {
        // Distance from mouse x and center of canvas.
        this._dx = mouse.x - this.centerX; 
        // Distance from mouse y and center of canvas.
        this._dy = mouse.y - this.centerY; 
        // Radians for the canvas rotate method.
        this._radians = Math.atan2(this._dy,this._dx);
    }


要使此代码适合您自己的需求,请尝试


To adapt this code for your own needs try

function diff(x, y) {
    var centerItem = $('imageid'),
        centerLoc = centeItem.offset();
    var dx = x - centerLoc.left + (centerItem.width() / 2);
        dy = y - centerLoc.top + (centerItem.height() / 2);
    return Math.atan2(dy, dx) * (180 / Math.PI);
}

示例,位于 http://jsfiddle.net/gaby/xWtKc/1/

这篇关于jQuery旋转光标角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆