Physics2DPlugin3+gsap实现canvas画布点击扩散动画效果代码
代码语言:html
所属分类:动画
代码描述:Physics2DPlugin3+gsap实现canvas画布点击扩散动画效果代码
代码标签: Physics2DPlugin gsap canvas 画布 点击 扩散 动画
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> body { background-color: #000; display: flex; align-items: center; justify-content: center; overflow: hidden; } canvas { min-height: 100vh; } </style> </head> <body translate="no"> <canvas id="canvas"></canvas> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/gsap.3.12.2.js"></script> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/Physics2DPlugin3.min.js"></script> <script > gsap.config({trialWarn: false}); console.clear() let select = s => document.querySelector(s), canvas = select('#canvas'), ctx = canvas.getContext('2d'), particles = [], sizeObj = {width: window.innerWidth, height: window.innerHeight }, particleArray = [], bigCircleArray = [], smallCircleArray = [], colorArray = ["fec10e","fe744d","cc9edc","ff4b10","019bd9","a1e8af","979797","83c9f4","5c374c","302f4d"], mousePos = { x: sizeObj.width/2, y: sizeObj.height/2 }, particleArea = Math.min(sizeObj.width, sizeObj.height) * 0.2, numParticles = 66, step = 360/numParticles, clickCount = -1, oldColor = null, currentColor = null colorArray = colorArray.map(x => Array.from(x)[0] == '#' ? x : `#${x}`); class Particle { constructor(x, y, radius, color, scale, rotation, origin, opacity, duration) { this.x = x; this.y = y; this.radius = radius; this.color = color; this.scale = scale; this.rotation = rotation; this.origin = origin; this.opacity = opacity; this.duration = duration; } } function bigCircleDuration () { let mapDuration, duration = 4; /* if(mousePos.x < sizeObj.width * 0.3) { duration = gsap.utils.mapRange(0, sizeObj.width * 0.3, 1.5, 4, mousePos.x) } else if(mousePos.x > sizeObj.width * 0.6) { duration = gsap.utils.mapRange(sizeObj.width * 0.6, sizeObj.width, 140, 1.5, mousePos.x) } */ return duration; } function removeParticle(particle, subParticleArray) { //remove the particle from sub array var particleIndex = subParticleArray.indexOf(particle); if (particleIndex !== -1) { subParticleArray.splice(particleIndex, 1); } //remove the empty sub array from main particleArray if(subParticleArray.length == 0) { var subIndex = particleArray.indexOf(subParticleArray); if (subIndex !== -1) { particleArray.splice(subIndex, 1); } } } function createAnimation() { clickCount++; oldColor = currentColor ? currentColor : gsap.utils.wrap(colorArray, clickCount+1); currentColor = (gsap.utils.wrap(colorArray, clickCount)) let subParticleArray = [] smallCircleArray = []; for(let i = 0; i < numParticles; i++) { let angle = i * step; let radius = gsap.utils.random(3, 12); let particleOrigin = [0,0]; let particleDuration = gsap.utils.random(0.4, 1); let point = { x: (Math.cos(angle * Math.PI / 180) * gsap.utils.random(particleArea*0.082, particleArea)) + mousePos.x, y: (Math.sin(angle * Math.PI / 180) * gsap.utils.random(particleArea*0.082, particleArea)) + mousePos.y } let p = new Particle(point.x, point.y, radius , oldColor, 1, 0, particl.........完整代码请登录后点击上方下载按钮下载查看
网友评论0