gpt4 book ai didi

svg - Raphael SVG沿水平直线移动对象

转载 作者:行者123 更新时间:2023-12-04 15:41:57 29 4
gpt4 key购买 nike

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

<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/

最佳答案

您的 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/

关于svg - Raphael SVG沿水平直线移动对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35941809/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com