亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Paper.js - 如何設置繪制矢量或線段的持續時間?

Paper.js - 如何設置繪制矢量或線段的持續時間?

繁花不似錦 2023-08-18 17:39:14
我需要設置與某些路線相關的向量(或線段),在一定時間內從A點開始到B點結束。比如說一次旅行。不幸的是,我找不到如何設置傳遞值的繪圖時間:<script src="https://cdnjs.cloudflare.com/ajax/libs/paper.js/0.12.7/paper-core.js"></script>var point1 = new Point(0, 0);var point2 = new Point(110, 200);var x = point2.x - point1.x;// = 110 - 50 = 60var y = point2.y - point1.y;// = 200 - 50 = 150;var vector = point2 - point1;// Create a Paper.js Path to draw a line into it:var path = new Path();// Give the stroke a colorpath.strokeColor = 'red';var start = vector;function onFrame(event) {    if (event.count < 101) {        path.add(start);        start += new Point(1, 1);    }}
查看完整描述

1 回答

?
眼眸繁星

TA貢獻1873條經驗 獲得超9個贊

如果我很好地理解你的情況,這里有一個演示可能實現的草圖。

這個想法是保留一個參考路徑,我們根據該參考路徑計算每個幀的臨時路徑,以生成動畫。

該解決方案的優點是您可以將其應用于任何類型的路徑。


// Create a path to animate.

const path = new Path.Circle({

? ? center: view.center,

? ? radius: 50,

? ? selected: true,

? ? closed: false

});


// Initialize the time variable that will control the animation.

let time = 0;


// On each frame...

function onFrame() {

? ? // ...if the animation is not finished yet...

? ? if (time <= 1) {

? ? ? ? // ...animate.

? ? ? ? time += 0.01;

? ? ? ? drawTmpPath(time);

? ? }

}


// Initialize the temporary path that will display our animation.

let tmpPath;


function drawTmpPath(t) {

? ? // Make sure that t is never over 1.

? ? t = Math.min(t, 1);

? ? // Remove the previously drawn temporary path.

? ? if (tmpPath) {

? ? ? ? tmpPath.remove();

? ? }

? ? // Draw the new temporary path from the reference one.

? ? tmpPath = path.clone().set({

? ? ? ? selected: false,

? ? ? ? strokeColor: 'orange',

? ? ? ? strokeWidth: 5

? ? });

? ? // Split it at the appropriate location.

? ? const remainingPath = tmpPath.splitAt(tmpPath.length * t);

? ? // Remove the eventual remaining part.

? ? if (remainingPath) {

? ? ? ? remainingPath.remove();

? ? }

}


// Scale things up.

project.activeLayer.fitBounds(view.bounds.scale(0.8));

編輯

為了回答您的評論,為了控制動畫時間,您可以存儲動畫開始時間,并在每一幀上計算更新函數從當前時間開始所需的相對時間。
請注意,您還可以依賴GreenSock等外部動畫庫來更輕松地處理計時。

// Total animation time in milliseconds.

const totalTime = 10000;


// Create a path to animate.

const path = new Path.Circle({

? ? center: view.center,

? ? radius: 50,

? ? selected: true,

? ? closed: false

});


// Initialize the time variable that will control the animation.

const startTime = Date.now();

let animationDone = false;


// On each frame...

function onFrame() {

? ? // ...if the animation is not finished yet...

? ? if (!animationDone) {

? ? ? ? // ...calculate the relative time needed to draw the tmp path.

? ? ? ? const relativeTime = (Date.now() - startTime) / totalTime;

? ? ? ? // ...animate.

? ? ? ? if (relativeTime < 1) {

? ? ? ? ? ? drawTmpPath(relativeTime);

? ? ? ? } else {

? ? ? ? ? ? drawTmpPath(1);

? ? ? ? ? ? animationDone = true;

? ? ? ? }

? ? }

}


// Initialize the temporary path that will display our animation.

let tmpPath;


function drawTmpPath(t) {

? ? // Make sure that t is never over 1.

? ? t = Math.min(t, 1);

? ? // Remove the previously drawn temporary path.

? ? if (tmpPath) {

? ? ? ? tmpPath.remove();

? ? }

? ? // Draw the new temporary path from the reference one.

? ? tmpPath = path.clone().set({

? ? ? ? selected: false,

? ? ? ? strokeColor: 'orange',

? ? ? ? strokeWidth: 5

? ? });

? ? // Split it at the appropriate location.

? ? const remainingPath = tmpPath.splitAt(tmpPath.length * t);

? ? // Remove the eventual remaining part.

? ? if (remainingPath) {

? ? ? ? remainingPath.remove();

? ? }

}


// Scale things up.

project.activeLayer.fitBounds(view.bounds.scale(0.8));


查看完整回答
反對 回復 2023-08-18
  • 1 回答
  • 0 關注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號