我使用popmotion制作了下面的旋转拨号。
const {
listen,
styler,
pointer,
value,
transform,
spring,
inertia,
calc
} = window.popmotion;
const { pipe } = transform;
const dial = document.querySelector(".dial");
const dialStyler = styler(dial);
const dialRotate = value(0, dialStyler.set('rotate'));
const dialRect = dial.getBoundingClientRect();
const dialY = dialRect.top + window.scrollY + (dialRect.height / 2);
const dialX = dialRect.left + window.scrollX + (dialRect.width / 2);
// console.log(dialX, dialY);
const pointA = {x: dialX, y: dialY};
// let pointB = {x: 0, y: 0};
// let angle = 0;
// let prevAngle = 90;
// Angle between origo and pointer
const pointerAngle = o => pointer( o ).pipe(v => {
const pointB = {x: v.x, y: v.y};
const angle = calc.angle(pointA, pointB) + 90;
// console.log('pointA: ', pointA);
// console.log('pointB: ', pointB);
// console.log('angle: ', angle);
// console.log('prevAngle: ', prevAngle);
// console.log('angle - prevAngle: ', angle - prevAngle);
return angle;
});
listen(dial, "mousedown touchstart").start(e => {
e.preventDefault();
// prevAngle = angle;
pointerAngle().start(dialRotate);
});
listen(document, "mouseup touchend").start(() => {
dialRotate.stop();
});
img {
width: 200px;
}
<script src="https://unpkg.com/popmotion/dist/popmotion.global.min.js"></script>
<img class="dial" src="https://greensock.com/wp-content/uploads/custom/draggable/img/knob.png">
每次放手并重新单击时,如何让表盘从同一位置开始?
它现在的工作方式是在单击/触摸时移动到光标/手指位置。我希望它从它当前所在的确切位置开始,并从那里计算角度。
这可能与三角学有关,但我无法弄清楚。