我創建了一個影片來說明我想用代碼實作的目標:抽搐示例
它保持靜止并偶爾快速旋轉,然后回傳其原始狀態。
例如,如果能夠在懸停時將其調高,使其劇烈搖晃 1 秒,那就太好了。但這并不重要。
uj5u.com熱心網友回復:
基于 Marquizzo 的回答:
只需使用
Math.random()更新每一幀的旋轉。
單獨使用Math.random(),不會達到這個效果。如果你想讓它停留在一個地方,就像你的 GIF 中顯示的那樣,你可以這樣做:
function getRandomDecimal(min, max) { //Generates the rotation value
var rand = Math.random()*(max-min) min;
var power = Math.pow(10, 2);
return Math.floor(rand*power) / power;
}
function render(){
renderer.render(scene, camera);
requestAnimationFrame(render);
objectToTwitch.rotation.x = getRandomDecimal(-1, 1); //Set the x rotation to a random decimal from `-1` to `1`.
};
render(); //Calling the function manually the first time
讓我解釋一下這段代碼。我們從一個名為 的自定義函式開始getRandomDecimal(),它接受 和 的min引數max。然后我們將物件的旋轉設定為-1, min, 和1,之間的亂數max。這每秒改變 60 次,或用戶瀏覽器的幀速率。如果這種“抽搐”效果發生得太快,您可以在程式的其他地方觸發該行代碼。
應該可以的~
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/485444.html
下一篇:僅使用CSS的文本向上滑動影片
