在javascript中计算弧路径

Calculating an arc path in javascript

本文关键字:路径 计算 javascript      更新时间:2023-09-26

我不知道如何模拟这个。红色箭头处于静态位置。背景,有一个弯曲的路径。我只是移动了背景位置,使箭头看起来是沿着弯曲的路径移动的。

红箭http://img829.imageshack.us/img829/6636/redarrowe.png

我现在看到的是箭头以45度角向目标移动。但我想要实现的是箭头沿着虚线移动。

var interval = 100;
var currX = -82;    // Current x position
var currY = -342;   // Current y position
var destX = -750;   // Destination x position
var destY = -850;   // Destination y position
var currentDuration = 0;
var duration = 1800 * 1000;  // Duration
var $bg = $('#flight-wrapper');
var intbg = window.setInterval(function(){
    currentDuration += interval;
    var leftX = destX - currX;  // X pixels left
    var leftY = destY - currY;  // Y pixels left
    var leftDuration = duration - currentDuration;  // Duration left
    var tickX = leftX / leftDuration * interval;    // Pixel to offset
    var tickY = leftY / leftDuration * interval;    // Pixel to offset
    var newX = currX + tickX;
    var newY = currY + tickY;
    // Prevent it going further than the destination X & Y pixel
    currX = newX <= destX ? destX : newX;
    currY = newY <= destY ? destY : newY;
    if ( currX <= destX && currY <= destY ) {
        window.clearTimeout(intbg);
    }
    $bg.css({
        backgroundPosition: Math.floor(currX) + 'px ' + Math.floor(currY) + 'px'
    });
}, interval);

希望有人能帮我一下。

给定点p = (x_t, y_t),即箭头在任何给定时间t的位置,您需要找出曲线在点p处的切线,这将告诉您箭头应该指向何处。