1
我無法弄清楚如何模擬這個。紅色箭頭處於靜止位置。背景,有一個彎曲的路徑。我只是移動背景位置,看起來箭頭正在沿着彎曲的路徑移動。在javascript中計算弧形路徑
Red Arrow 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);
希望有人能幫助我。
你想要數學解釋還是可以使用庫發佈工作解決方案嗎? – Prinzhorn
我可以請數學解釋。 –
考慮到你的弧可以看作是一個圓的一部分,你可以使用'sin'和'cos'來相應地轉換x/y。但首先你應該使用一個jQuery插件,它允許你動畫'背景位置',這使得你當前的代碼已經過時並且是一行代碼。然後用正弦和餘弦指定你自己的緩衝函數。 – Prinzhorn