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();
相关推荐
行者963 小时前
Flutter跨平台开发适配OpenHarmony:进度条组件的深度实践
开发语言·前端·flutter·harmonyos·鸿蒙
云和数据.ChenGuang3 小时前
Uvicorn 是 **Python 生态中用于运行异步 Web 应用的 ASGI 服务器**
服务器·前端·人工智能·python·机器学习
IT_陈寒3 小时前
SpringBoot 3.0实战:这5个新特性让你的开发效率提升50%
前端·人工智能·后端
遗憾随她而去.3 小时前
Webpack 面试题
前端·webpack·node.js
我要敲一万行3 小时前
前端文件上传
前端·javascript
恋猫de小郭3 小时前
Tailwind 因为 AI 的裁员“闹剧”结束,而 AI 对开源项目的影响才刚刚开始
前端·flutter·ai编程
要加油哦~3 小时前
算法 | 整理数据结构 | 算法题中,JS 容器的选择
前端·javascript·算法
一只小bit3 小时前
Qt 重要控件:多元素控件、容器类控件及布局管理器
前端·c++·qt
一 乐11 小时前
婚纱摄影网站|基于ssm + vue婚纱摄影网站系统(源码+数据库+文档)
前端·javascript·数据库·vue.js·spring boot·后端
C_心欲无痕12 小时前
ts - tsconfig.json配置讲解
linux·前端·ubuntu·typescript·json