【问题标题】:Move object around circle javascript围绕圆圈javascript移动对象
【发布时间】:2014-05-25 17:47:13
【问题描述】:

以上只是获取小提琴链接的图片,它的作用是没有问题的。

我正在尝试沿圆形移动一个对象(实际上是一个边框半径=100% 的矩形画布,因此表现为圆形)。但只能移动它,不能限制圆圈的移动)

这是我的JS Fiddle Link注意:请在js代码中向下滚动查看唯一写在以下cmets下的代码

//---- Code of interest begins-----// 到 //---感兴趣的代码结束---//

我的想要的输出会像这个轮子farbtastic。虽然它是开源的,但我一直无法在我的色轮中使用它的鼠标移动事件代码

在尝试搜索时,我找到了Canvas move object in circle 但我不能使用它,因为我不知道d,也不知道across 是什么。所以无法通过arccos(1-(d/r)^2/2)获得theta

【问题讨论】:

    标签: javascript math drag-and-drop logic farbtastic


    【解决方案1】:

    我认为最简单的方法是计算角度并将 css3 旋转变换应用于元素。

    我在这里给你举个例子..

    读取 cmets,特别是在 rotate() 函数上

    http://jsfiddle.net/Z37FF/3/

    HTML

        <body>
            <div id="circle">
            <div id="circle-in"></div>
            <div id="picker">
            <div id="picker-circle"></div>
            </div>
            </div>  
        </body>
    

    CSS

        #circle{
            position: relative;
            width: 300px;
            height: 300px;
            border-radius: 50%;
            background: #000;
        }
    
        #circle-in{
            position: absolute;
            top: 35px;
            left: 35px;
            width: 230px;
            height: 230px;
            border-radius: 50%;
            background: #fff;
        }
    
        #picker{
            position: absolute;
            top: 50%;
            left: 50%;
            height: 30px;
            margin-top: -15px;
            width: 50%;
    
            /* important: sets the transform origin to the center of the circle */
            transform-origin: center left;
        }
    
        #picker-circle{
            width: 30px;
            height: 30px;
            border-radius: 50%;
            background: #fff;
            margin: 0 3px 0 auto;
            cursor: move;
        }
    

    JS

    document.addEventListener('DOMContentLoaded', function(){
        var circle = document.getElementById('circle'),
            picker = document.getElementById('picker'),
            pickerCircle = picker.firstElementChild,
            rect = circle.getBoundingClientRect(),
    
            center = {
                x: rect.left + rect.width / 2,
                y: rect.top + rect.height / 2
            },
    
            rotate = function(x, y){
                var deltaX = x - center.x,
                    deltaY = y - center.y,
    
                // The atan2 method returns a numeric value between -pi and pi representing the angle theta of an (x,y) point.
                // This is the counterclockwise angle, measured in radians, between the positive X axis, and the point (x,y).
                // Note that the arguments to this function pass the y-coordinate first and the x-coordinate second.
                // atan2 is passed separate x and y arguments, and atan is passed the ratio of those two arguments.
                // * from Mozilla's MDN
    
                // Basically you give it an [y, x] difference of two points and it give you back an angle
                // The 0 point of the angle is right (the initial position of the picker is also right)
    
                    angle = Math.atan2(deltaY, deltaX) * 180 / Math.PI
    
                // Math.atan2(deltaY, deltaX) => [-PI +PI]
                // We must convert it to deg so...
                // / Math.PI => [-1 +1]
                // * 180 => [-180 +180]
    
                return angle
            },
    
            // DRAGSTART
            mousedown = function(event){
                event.preventDefault()
                document.body.style.cursor = 'move'
                mousemove(event)
                document.addEventListener('mousemove', mousemove)
                document.addEventListener('mouseup', mouseup)
            },
    
            // DRAG
            mousemove = function(event){
                picker.style.transform = 'rotate(' + rotate(event.x, event.y) + 'deg)'
            },
    
            // DRAGEND
            mouseup = function(){
                document.body.style.cursor = null;
                document.removeEventListener('mouseup', mouseup)
                document.removeEventListener('mousemove', mousemove)
            }
    
    
    
        // DRAG START
        pickerCircle.addEventListener('mousedown', mousedown)
    
        // ENABLE STARTING THE DRAG IN THE BLACK CIRCLE
        circle.addEventListener('mousedown', function(event){
            if(event.target == this) mousedown(event)
        })
    })
    

    【讨论】:

    • 谢谢.. 很高兴看到有人会提供帮助。但是请确保小提琴链接正常工作..它可能无法拖动对象太多
    • 我确定这是因为小提琴只设置了变换而不是 webkitTransform(不支持旧版 chrome 变换)试试这个http://jsfiddle.net/Z37FF/1/
    • 在 chrome 中工作但不完全跟随圆圈
    • 它在我的电脑上运行良好.. Linux 上的 Chrome 36。到底发生了什么?
    • 现在我真的完成了。由于 event.x event.y,它在 ff 中不起作用,将其更改为 event.pageX event.pageY。几乎所有浏览器都支持转换...(不是 IE8 及更低版本)请参阅 http://caniuse.com/transforms2d http://jsfiddle.net/Z37FF/3/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    相关资源
    最近更新 更多