首页 > 其他分享 >three-第三方动画库-让物体动起来更简单(gsap)

three-第三方动画库-让物体动起来更简单(gsap)

时间:2024-02-23 16:55:20浏览次数:28  
标签:动画 const THREE three window gsap renderer new

gsap

介绍:

GreenSock Animation Platform (GSAP) 是一个业界知名的动画库,它被1100多万个网站使用,有超过50%的获奖的网站都是用了它。不管是在原生环境中,还是任意的框架中,你可以使用GSAP去让非常多的东西动起来。不管你是想要去让UI界面产生交互动画,还是SVG图形产生动画,甚至是Threejs还是React组件,GSAP都可以轻松搞定!

相关地址:

https://gsap.framer.wiki/stated   -   中文翻译说明

https://gsap.com/docs/v3/  -   官方文档说明

安装

npm:

npm install gsap

 cdn:

<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.5/gsap.min.js"></script>

yarn:

yarn add gsap

参考地址:https://gsap.com/docs/v3/Installation?tab=yarn&module=esm&method=private+registry&tier=free&club=false&require=false&trial=true

gsap和three.js结合

import * as THREE from "three";
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls';
import gsap from "gsap";
import {func} from "three/examples/jsm/nodes/code/FunctionNode";
// 函数中添加时间
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

const geometry = new THREE.BoxGeometry(2, 2, 2);
const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
const cube = new THREE.Mesh( geometry, material );
cube.position.set(1,0,0);
cube.scale.set(0.5,0.5,0.5);
cube.rotation.set(Math.PI / 4, 0, 0, 'XYZ')
scene.add(cube);

camera.position.z = 5;
const controls = new OrbitControls( camera, renderer.domElement );
// 添加阻尼
controls.enableDamping = true;
const axesHelper = new THREE.AxesHelper( 5 );
const animal1 = gsap.to(cube.position, { x: 4, duration: 5, ease: "power2.inOut", repeat: -1, yoyo: true, })
const animal2 = gsap.to(cube.rotation, { x: 2 * Math.PI, duration: 5, ease: "power2.inOut", repeat: -1, yoyo: true, })
document.body.addEventListener('click', function () {
    if (animal1.isActive()) {
        animal1.pause();
    } else {
        animal1.resume()
    }
})
scene.add( axesHelper );
function render() {
    renderer.render(scene, camera);
    requestAnimationFrame(render);
}
render();
window.addEventListener('resize', function () {
    // 更新摄像头
    camera.aspect = window.innerWidth / window.innerHeight;
    // 更新摄像头的投影矩阵
    camera.updateProjectionMatrix();
    // 更新渲染器
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setPixelRatio(window.devicePixelRatio);
});
// 全屏
window.addEventListener("dblclick", function () {
    const fullScreen = document.fullscreenElement;
    if (!fullScreen) {
        renderer.domElement.requestFullscreen();
    } else {
        document.exitFullscreen();
    }
})

展示效果:

 

标签:动画,const,THREE,three,window,gsap,renderer,new
From: https://www.cnblogs.com/shangguancn/p/18029921

相关文章

  • three.js-坐标轴辅助器
    坐标轴辅助器(AxesHelper)用于简单模拟3个坐标轴的对象.红色代表X轴.绿色代表Y轴.蓝色代表Z轴.constaxesHelper=newTHREE.AxesHelper(5);scene.add(axesHelper);文档地址:https://threejs.org/docs/index.html#api/zh/helpers/AxesHelper构造函数(Constructor)A......
  • three.js-轨道控制器(OrbitControls)
    轨道控制器(OrbitControls)Orbitcontrols(轨道控制器)可以使得相机围绕目标进行轨道运动。要使用这一功能,就像在/examples(示例)目录中的所有文件一样,您必须在HTML中包含这个文件。进口OrbitControls是一个附加组件,必须显式导入。See Installation/Addons.import{OrbitCont......
  • 迪文屏TA指令开发_开机动画
    迪文屏TA指令开发_开机动画1.新建项目新建一个空白文件夹,点击新建工程之后选择新建文件夹地址即可创建完成之后,文件夹结构如下:2.导入背景图片素材说是设置开机动画,实际上是通过多个背景图片的连续播放实现的动画效果点击加号键,可以直接选中所有的背景图片素材进行一键导......
  • 搭建我们自己的第一个three项目
    搭建项目我们要学习three,那么我们势必需要在本地搭建一个自己的项目。首先我们需要在我们自己的电脑中安装node,创建一个node环境。这个自行从网上百度,这里就不再介绍了。(简单的一批,自行百度吧,如果不会,那么也不要学习three了)Parcel这里我们使用Parcel来打包我们的项目(你也可以......
  • 认识three.js和本地环境搭建
    要学习three.js首先我们要了解几个概念什么是3d,什么是webgl,那么three.js又是什么。下面我们来一一介绍。首先什么是3d?3D,全称为Three-Dimensional,即三维,是指在空间中具有三个维度(长度、宽度和高度)的概念。在现实世界中,我们所处的就是一个三维空间,物体都有立体形态,可以从不同的......
  • npm 报错 npm ERR! request to https://registry.npm.taobao.org/three failed, reaso
    1.问题描述npm使用淘宝镜像安装报错npmERR!codeCERT_HAS_EXPIREDnpmERR!errnoCERT_HAS_EXPIREDnpmERR!requesttohttps://registry.npm.taobao.org/xxxfailed,reason:certificatehasexpired2.错误原因错误提示已经告诉原因是淘宝镜像过期了!其实,早在2021......
  • three.js 添加 echarts图表
    functioninitChart(){////基于准备好的dom,初始化echarts实例varmyChart=echarts.init($("<canvaswidth='512'height='512'></canvas>")[0]);//绘制图表myChart.setOption({title:{text:'ECharts入门示例'......
  • threejs 如何设置自己想要的初始角度和大小
    threejs在渲染完模型后,一般初始的大小和角度并不合我们的心意,这就需要我们手动调整了,但是手动调整又不好控制精度。  开启相机控件threejs渲染的模型大小和角度是由相机决定的,所以我们只需要调整相机就可以了,然后获取相机位置信息写入到初始化的数据中就可以了。要调整相......
  • three.js的物体点击事件
    直接粘贴无脑用 !!!vue文件执行要现在初始化方法里面调用一下onclickS(){   letraycaster=newTHREE.Raycaster();   letmouse=newTHREE.Vector2();     functiononmodelclick(event){    mouse.x=(event.clientX/window.innerWidt......
  • gsap 事件
    1ScrollTrigger.create({2trigger:'.box',3start:'toptop',4end:'+=2000',5markers:true,6scrub:true,7pin:true,8animation:9gsap.ti......