【问题标题】:Raphael SVG move object along straight horizontal lineRaphael SVG 沿水平直线移动对象
【发布时间】:2016-06-26 19:00:34
【问题描述】:

我想沿水平线路径移动绿色按钮,但绿色按钮没有跟随我的鼠标。拖动时如何使绿色按钮跟随我的鼠标? 代码:

<script>
var pdefs = {
      horizontalLine: {
          path: [
                    ['M',50,240],
                    ['l',640,0]
                ],
          transform: 'r0'
        }
     },
    useDef = 'wiggles';

function run()
{
   var paper = Raphael( $('.wrapper')[0], 600, 600 ),
       path = paper.path( Raphael.transformPath(pdefs['horizontalLine'].path, pdefs['horizontalLine'].transform) )
                   .attr( 'stroke-width', 10 )
                   .attr( 'stroke', 'rgb(80,80,80)' ),
       knob = paper.ellipse( 0, 0, 25, 15 )
                   .attr( 'fill', 'lime' )
                   .attr( 'stroke', 'rgba(80,80,80,0.5)' ),
       $shim = $('<div>')
                    .css( {position: 'absolute', width: 50, height: 50 } )
                    .appendTo( $('.wrapper') ),
       len = path.getTotalLength(),
       bb = path.getBBox(),
       mid = {x: bb.x+bb.width/2, y: bb.y+bb.height/2},
       pal = path.getPointAtLength(0);
   knob.translate(pal.x,pal.y).rotate(pal.alpha);
   $shim.css({ left: pal.x-5, top: pal.y-5 });
   $shim.draggable({
         drag: function ( e, ui ) {
            // Find lines and then angle to determine
            // percentage around an imaginary circle.
            var t = ( Raphael.angle( ui.position.left-25, ui.position.top-25, mid.x, mid.y ) ) /360;
            // Using t, find a point along the path
            pal = path.getPointAtLength( (t * len) % len );
            // Move the knob to the new point
            knob.transform( 't' + [pal.x, pal.y] + 'r' + pal.alpha );
         },
         stop: function ( e, ui ) {
            $shim.css({ left: pal.x-25, top: pal.y-25 });
         }
      });
}

run();

</script>

演示:https://jsfiddle.net/zac1987/zea53w7f/

【问题讨论】:

  • 我怀疑某处的 x 和 y 之间存在混淆...当您垂直移动鼠标时,对象沿线移动,而鼠标水平拖动,则没有任何反应。
  • @zac1987 我下面的回答有帮助吗?

标签: svg raphael


【解决方案1】:

您的drag 函数看起来像是一些不同小部件(可能是圆形旋钮?)的剩余部分。

代替:

drag: function ( e, ui ) {
    // Find lines and then angle to determine
    // percentage around an imaginary circle.
    var t = ( Raphael.angle( ui.position.left-25, ui.position.top-25, mid.x, mid.y ) ) /360;
    // Using t, find a point along the path
    pal = path.getPointAtLength( (t * len) % len );
    // Move the knob to the new point
    knob.transform( 't' + [pal.x, pal.y] + 'r' + pal.alpha );
 }

试试:

drag: function ( e, ui ) {
    var t = ui.position.left - 50;
    // Using t, find a point along the path
    pal = path.getPointAtLength( Math.max(t, 0) );
    // Move the knob to the new point
    knob.transform( 't' + [pal.x, pal.y] + 'r' + pal.alpha );
}

https://jsfiddle.net/1Lzqhm9o/2/

【讨论】:

  • 谢谢。我将背景色添加到您的 shim div $shim.css("background-color", "yellow"); 并意识到 div shim 在拖动时不沿着直线,如何让它沿着直线拖动?
  • 没关系,我自己想通了,加ui.position.top = pal.y; ui.position.left = pal.x;就行了。多谢兄弟。向你的伟大学习:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多