8-Gsap动画库基本使用与原理

Gsap动画库基本使用与原理

  • npm run gsap
  • 官方文档: https://gsap.com/docs/v3/
js 复制代码
import * as THREE from "three";
// 导入轨道控制器
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
// 导入动画库
import gsap from "gsap";

// 目标了解threejs基本内容
// 1. 创建场景
const scene = new THREE.Scene();

// 2.创建一个(透视)相机对象 角度75° 屏幕的宽高比
const camera = new THREE.PerspectiveCamera(
  75,
  window.innerWidth / window.innerHeight,
  0.1, //近端
  1000 //远端的景深
);

// 3.设置相机位置
camera.position.set(0, 0, 10); // x y z坐标

// 4.添加到场景当中
scene.add(camera);

// 5.添加物体
// 5.1创建集合体对象
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
// 5.2设置材质(基础网格材质)
const cubeMaterial = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
// 5.3根据集合体和材质创建物体
const cube = new THREE.Mesh(cubeGeometry, cubeMaterial);

// 5.4设置物体的移动
// cube.position.set(5, 0, 0);
// 5.5设置某个轴的同上
// cube.position.x = 5;
// 5.6 设置缩放
// cube.scale.set(3, 2, 1); //这设置是整体进行设置,分别是x轴3被,y轴2倍,z轴一倍
// cube.scale.x = 2;

// 旋转
// cube.rotation.set(Math.PI / 4, 0, 0); //按照x轴旋转45°,y不旋转,z不旋转,还有第四个参数(非必填默认'xyz')

// 5.5将几何体添加到场景中(场景中没有物体也是不会显示的)
scene.add(cube);

// 6.初始化渲染器(进行渲染)
const renderer = new THREE.WebGLRenderer();
// 6.1 设置渲染的尺寸大小(这里根据屏幕的尺寸大小进行渲染)
renderer.setSize(window.innerWidth, window.innerHeight);

// 7.渲染的东西(渲染是往canvas上面去画的)添加到元素中(这里添加到body中)
document.body.appendChild(renderer.domElement); //这个是canvas

// 8.使用渲染器通过相机将场景渲染进来
// renderer.render(scene, camera); //这里渲染会定型后面就需要重新渲染才能看到每个面

// 9.显示3维立体的图形(轨道控制器)
// 9.1 创建轨道控制器
const controls = new OrbitControls(camera, renderer.domElement); //相机 和 监控的对象

// 10. 添加坐标轴辅助器
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);

// 11. 设置动画
gsap.to(cube.position, { x: 5, duration: 5, ease: 'power1.inOut' }); //花费5s到5这个位置
gsap.to(cube.rotation, { x: Math.PI * 2, duration: 5, ease: 'power1.inOut' }); //旋转

// 设置一个渲染函数
function render() {
  renderer.render(scene, camera);
  requestAnimationFrame(render); //动画帧->渲染下一帧
}

render();
  • 动画的ease:
    • 找对应的方式

Gsap控制动画属性与方法

  • 全部代码
js 复制代码
import * as THREE from "three";
// 导入轨道控制器
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
// 导入动画库
import gsap from "gsap";

// 目标了解threejs基本内容
// 1. 创建场景
const scene = new THREE.Scene();

// 2.创建一个(透视)相机对象 角度75° 屏幕的宽高比
const camera = new THREE.PerspectiveCamera(
  75,
  window.innerWidth / window.innerHeight,
  0.1, //近端
  1000 //远端的景深
);

// 3.设置相机位置
camera.position.set(0, 0, 10); // x y z坐标

// 4.添加到场景当中
scene.add(camera);

// 5.添加物体
// 5.1创建集合体对象
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
// 5.2设置材质(基础网格材质)
const cubeMaterial = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
// 5.3根据集合体和材质创建物体
const cube = new THREE.Mesh(cubeGeometry, cubeMaterial);

// 5.4设置物体的移动
// cube.position.set(5, 0, 0);
// 5.5设置某个轴的同上
// cube.position.x = 5;
// 5.6 设置缩放
// cube.scale.set(3, 2, 1); //这设置是整体进行设置,分别是x轴3被,y轴2倍,z轴一倍
// cube.scale.x = 2;

// 旋转
// cube.rotation.set(Math.PI / 4, 0, 0); //按照x轴旋转45°,y不旋转,z不旋转,还有第四个参数(非必填默认'xyz')

// 5.5将几何体添加到场景中(场景中没有物体也是不会显示的)
scene.add(cube);

// 6.初始化渲染器(进行渲染)
const renderer = new THREE.WebGLRenderer();
// 6.1 设置渲染的尺寸大小(这里根据屏幕的尺寸大小进行渲染)
renderer.setSize(window.innerWidth, window.innerHeight);

// 7.渲染的东西(渲染是往canvas上面去画的)添加到元素中(这里添加到body中)
document.body.appendChild(renderer.domElement); //这个是canvas

// 8.使用渲染器通过相机将场景渲染进来
// renderer.render(scene, camera); //这里渲染会定型后面就需要重新渲染才能看到每个面

// 9.显示3维立体的图形(轨道控制器)
// 9.1 创建轨道控制器
const controls = new OrbitControls(camera, renderer.domElement); //相机 和 监控的对象

// 10. 添加坐标轴辅助器
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);

// 11. 设置动画
let animate1 = gsap.to(cube.position, {
  x: 5,
  duration: 5,
  ease: "power1.inOut",
  repeat: 2, //重复动画的次数,无限次循环给 -1
  yoyo: true, //往返运动 默认值 false
  delay: 2, // 延迟时间 2s 默认值 0
  onStart: () => {
    console.log("动画开始");
  },
  onComplete: () => {
    console.log("动画完成");
  },
}); //花费5s到5这个位置
gsap.to(cube.rotation, { x: Math.PI * 2, duration: 5, ease: "power1.inOut" }); //旋转

// 当鼠标点击后停止 -> 这样就可以主动去控制动画停止了
document.addEventListener("click", () => {
  if (animate1.isActive()) {
    //isActive是方法
    //如果是在动画的状态
    animate1.pause(); //暂停
  } else {
    animate1.resume(); //恢复动画
  }
});

// 设置一个渲染函数
function render() {
  renderer.render(scene, camera);
  requestAnimationFrame(render); //动画帧->渲染下一帧
}

render();
  • 动画部分的使用
js 复制代码
// 11. 设置动画
let animate1 = gsap.to(cube.position, {
  x: 5,
  duration: 5,
  ease: "power1.inOut",
  repeat: 2, //重复动画的次数,无限次循环给 -1
  yoyo: true, //往返运动 默认值 false
  delay: 2, // 延迟时间 2s 默认值 0
  onStart: () => {
    console.log("动画开始");
  },
  onComplete: () => {
    console.log("动画完成");
  },
}); //花费5s到5这个位置
gsap.to(cube.rotation, { x: Math.PI * 2, duration: 5, ease: "power1.inOut" }); //旋转

// 当鼠标点击后停止 -> 这样就可以主动去控制动画停止了
document.addEventListener("click", () => {
  if (animate1.isActive()) {
    //isActive是方法
    //如果是在动画的状态
    animate1.pause(); //暂停
  } else {
    animate1.resume(); //恢复动画
  }
});

// 设置一个渲染函数
function render() {
  renderer.render(scene, camera);
  requestAnimationFrame(render); //动画帧->渲染下一帧
}

render();
相关推荐
喜欢打篮球的普通人30 分钟前
LLVM 前端与优化层:Clang 如何把 C 代码变成 IR
c语言·前端·状态模式
大文说跨境43 分钟前
亚马逊多店铺用过多款指纹浏览器,说点长期用的实际感受
前端·网络
中屹指纹浏览器svip1 小时前
中屹指纹浏览器和普通 Chrome 有什么区别?
前端·chrome·中屹指纹浏览器
feixing_fx1 小时前
CSS Grid 入门:二维布局时代的降维打击
前端·css·前端框架·css3
Lank_M1 小时前
JPG的8×8块里只要有条边,白底就保不住255
前端
coding漫漫长路1 小时前
主图纯白检测,阈值放宽到250会放过252的底
前端
计算机魔术师1 小时前
藏了八个月没被当漏洞的洞,AI红队一晚上翻出来了
前端
用户83134859306981 小时前
Vue3 v-for:常用用法与开发高频坑点
前端·javascript·vue.js
用户621007587021 小时前
Web 3D 开发实践:BIM+GIS 融合需要解决哪些技术问题?
前端·three.js
志尊宝1 小时前
AI原生前端开发(017):端侧AI与Vue 3结合实战
前端·人工智能·prompt·1024·ai-native