最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

three.js中g(shù)sap動(dòng)畫庫(kù)實(shí)現(xiàn)物體的動(dòng)畫

 更新時(shí)間:2023年07月27日 09:10:06   作者:jieyucx  
本文主要介紹了three.js中g(shù)sap動(dòng)畫庫(kù)實(shí)現(xiàn)物體的動(dòng)畫,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、什么是gsap

GSAP(GreenSock Animation Platform)是一個(gè)JavaScript動(dòng)畫庫(kù),由GreenSock公司開發(fā),用于在Web應(yīng)用程序中創(chuàng)建高性能動(dòng)畫。

使用GSAP可以通過一些簡(jiǎn)單的動(dòng)畫操作來實(shí)現(xiàn)復(fù)雜的動(dòng)畫效果,例如TweenLite、TweenMax、TimelineLite、TimelineMax等工具。

以下是一個(gè)簡(jiǎn)單動(dòng)畫的示例:

<!DOCTYPE html>
<html>
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.6.1/gsap.min.js"></script>
</head>
<body>
  <div id="box"></div>
  <script>
    // 選取元素
    var box = document.getElementById("box");
    // 設(shè)置初始樣式
    gsap.set(box, {x: -100});
    // 定義動(dòng)畫
    var tl = gsap.timeline({repeat: -1});
    tl.to(box, {x: 100, duration: 1, ease: "power1.out"})
      .to(box, {y: 100, duration: 1, ease: "power1.in"})
      .to(box, {x: -100, duration: 1, ease: "power1.out"})
      .to(box, {y: -100, duration: 1, ease: "power1.in"});
    // 暫停動(dòng)畫
    function toggleAnimation() {
      tl.paused(!tl.paused());
    }
    // 雙擊屏幕暫停動(dòng)畫
    document.addEventListener("dblclick", toggleAnimation);
  </script>
</body>
</html>

這是一個(gè)簡(jiǎn)單的矩形動(dòng)畫,矩形會(huì)沿著 X、Y 軸移動(dòng),在達(dá)到一個(gè)特定的位置時(shí)變形,然后在雙擊屏幕時(shí)暫停。

可以通過修改TweenMax動(dòng)畫中的參數(shù)來實(shí)現(xiàn)不同的效果,例如使用不同的緩動(dòng)函數(shù)(ease參數(shù))、改變動(dòng)畫的持續(xù)時(shí)間(duration參數(shù))等。

二、在three.js中應(yīng)用gsap

這是一個(gè)使物體來回運(yùn)動(dòng)的案例,并且實(shí)現(xiàn)雙擊屏幕暫停動(dòng)畫

要用這東西肯定得先下載拉

下載

npm i gsap

核心代碼

// 導(dǎo)入動(dòng)畫庫(kù)
import gsap from "gsap";
// 創(chuàng)建幾何體
const geometry = new THREE.BoxGeometry(1, 1, 1);
// 創(chuàng)建材質(zhì)
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
// 根據(jù)幾何體和材質(zhì)創(chuàng)建物體
const cube = new THREE.Mesh(geometry, material);
// 設(shè)置動(dòng)畫
let animate1 = gsap.to(cube.position, {
  x: 5,
  duration: 5,
  ease: "power1.inOut",
  //   設(shè)置重復(fù)的次數(shù),無限次循環(huán)-1
  repeat: -1,
  //   往返運(yùn)動(dòng)
  yoyo: true,
  //   delay,延遲2秒運(yùn)動(dòng)
  delay: 2,
  onComplete: () => {
    console.log("動(dòng)畫完成");
  },
  onStart: () => {
    console.log("動(dòng)畫開始");
  },
});
gsap.to(cube.rotation, { x: 2 * Math.PI, duration: 5, ease: "power1.inOut" });
window.addEventListener("dblclick", () => {
  //   console.log(animate1);
  if (animate1.isActive()) {
    //   暫停
    animate1.pause();
  } else {
    //   恢復(fù)
    animate1.resume();
  }
});

完整代碼

import * as THREE from "three";
// 導(dǎo)入軌道控制器
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
// 導(dǎo)入動(dòng)畫庫(kù)
import gsap from "gsap";
// console.log(THREE);
// 目標(biāo):掌握gsap設(shè)置各種動(dòng)畫效果
// 1、創(chuàng)建場(chǎng)景
const scene = new THREE.Scene();
// 2、創(chuàng)建相機(jī)
const camera = new THREE.PerspectiveCamera(
  75,
  window.innerWidth / window.innerHeight,
  0.1,
  1000
);
// 設(shè)置相機(jī)位置
camera.position.set(0, 0, 10);
scene.add(camera);
// 添加物體
// 創(chuàng)建幾何體
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const cubeMaterial = new THREE.MeshBasicMaterial({ color: 0xffff00 });
// 根據(jù)幾何體和材質(zhì)創(chuàng)建物體
const cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
// 修改物體的位置
// cube.position.set(5, 0, 0);
// cube.position.x = 3;
// 縮放
// cube.scale.set(3, 2, 1);
// cube.scale.x = 5;
// 旋轉(zhuǎn)
cube.rotation.set(Math.PI / 4, 0, 0, "XZY");
// 將幾何體添加到場(chǎng)景中
scene.add(cube);
console.log(cube);
// 初始化渲染器
const renderer = new THREE.WebGLRenderer();
// 設(shè)置渲染的尺寸大小
renderer.setSize(window.innerWidth, window.innerHeight);
// console.log(renderer);
// 將webgl渲染的canvas內(nèi)容添加到body
document.body.appendChild(renderer.domElement);
// // 使用渲染器,通過相機(jī)將場(chǎng)景渲染進(jìn)來
// renderer.render(scene, camera);
// 創(chuàng)建軌道控制器
const controls = new OrbitControls(camera, renderer.domElement);
// 添加坐標(biāo)軸輔助器
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);
// 設(shè)置時(shí)鐘
const clock = new THREE.Clock();
// 設(shè)置動(dòng)畫
var animate1 = gsap.to(cube.position, {
  x: 5,
  duration: 5,
  ease: "power1.inOut",
  //   設(shè)置重復(fù)的次數(shù),無限次循環(huán)-1
  repeat: -1,
  //   往返運(yùn)動(dòng)
  yoyo: true,
  //   delay,延遲2秒運(yùn)動(dòng)
  delay: 2,
  onComplete: () => {
    console.log("動(dòng)畫完成");
  },
  onStart: () => {
    console.log("動(dòng)畫開始");
  },
});
gsap.to(cube.rotation, { x: 2 * Math.PI, duration: 5, ease: "power1.inOut" });
window.addEventListener("dblclick", () => {
  //   console.log(animate1);
  if (animate1.isActive()) {
    //   暫停
    animate1.pause();
  } else {
    //   恢復(fù)
    animate1.resume();
  }
});
function render() {
  renderer.render(scene, camera);
  //   渲染下一幀的時(shí)候就會(huì)調(diào)用render函數(shù)
  requestAnimationFrame(render);
}
render();

哈哈哈 貼個(gè)圖敷衍一下,你知道這個(gè)物體是移動(dòng)的就行,腦補(bǔ)一下,賴得做gif動(dòng)圖了。。。。

到此這篇關(guān)于three.js中g(shù)sap動(dòng)畫庫(kù)實(shí)現(xiàn)物體的動(dòng)畫的文章就介紹到這了,更多相關(guān)three.js 物體動(dòng)畫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

监利县| 永胜县| 韶山市| 建德市| 金川县| 平凉市| 项城市| 旬阳县| 白城市| 阳谷县| 德州市| 建阳市| 句容市| 长春市| 桐柏县| 岑巩县| 宜川县| 斗六市| 时尚| 上栗县| 博罗县| 江门市| 洞头县| 临安市| 江华| 江源县| 浪卡子县| 永年县| 屏东县| 东阿县| 博野县| 互助| 时尚| 惠水县| 盘山县| 高清| 霍邱县| 扶余县| 赤壁市| 普陀区| 宁陕县|