利用gsap和canvas实现序列帧

function imageSequence(config) {
  if (config.full) {
    setCanvas($(config.canvas), config.top);
  }

  let playhead = { frame: 0 },
    canvas =
      gsap.utils.toArray(config.canvas)[0] ||
      console.warn("canvas not defined"),
    ctx = canvas.getContext("2d"),
    curFrame = -1,
    onUpdate = config.onUpdate,
    images,
    updateImage = function () {
      let frame = Math.round(playhead.frame);
      if (frame !== curFrame) {
        // only draw if necessary
        config.clear && ctx.clearRect(0, 0, canvas.width, canvas.height);
        const img = images[Math.round(playhead.frame)];
        let dx, dy, dw, dh;
        let imgRatio = img.width / img.height;
        let canvasRatio = canvas.width / canvas.height;

        if (config.objectFit === "contain") {
          if (imgRatio <= canvasRatio) {
            dw = imgRatio * canvas.height;
            dh = canvas.height;
            dx = (canvas.width - dw) / 2;
            dy = 0;
          } else {
            dw = canvas.width;
            dh = dw / imgRatio;
            dx = 0;
            dy = (canvas.height - dh) / 2;
          }
          ctx.drawImage(img, 0, 0, img.width, img.height, dx, dy, dw, dh);
        } else {
          if (imgRatio <= canvasRatio) {
            dw = img.width;
            dh = img.width / canvasRatio;
            dx = 0;
            dy = (img.height - dh) / 2;
          } else {
            dw = img.height * canvasRatio;
            dh = img.height;
            dx = (img.width - dw) / 2;
            dy = 0;
          }
          ctx.drawImage(img, dx, dy, dw, dh, 0, 0, canvas.width, canvas.height);
        }

        curFrame = frame;
        onUpdate && onUpdate.call(this, frame, images[frame]);
      }
    };

  window.addEventListener(
    "resize",
    debounce(() => {
      setCanvas($(config.canvas), config.top);
    }, 300),
    false
  );

  images = config.urls.map((url, i) => {
    let img = new Image();
    img.src = url;
    i || (img.onload = updateImage);
    return img;
  });
  return gsap.to(playhead, {
    frame: images.length - 1,
    ease: "none",
    onUpdate: updateImage,
    duration: images.length / (config.fps || 30),
    paused: !!config.paused,
    scrollTrigger: config.scrollTrigger,
    onComplete: config.onComplete,
  });
}

function setCanvas(canvas, top) {
  const width = window.innerWidth;

  const fullHeight = window.innerWidth < 750 ? $($.CN(`.section-third .page-stickybox`)).height() : window.innerHeight;
  
  const height = top
    ? fullHeight - (window.innerWidth < 1600 ? 50 : 60)
    : fullHeight;

  const dpr = window.devicePixelRatio;

  canvas.attr("width", Math.round(width * dpr));
  canvas.attr("height", Math.round(height * dpr));
  canvas.css("width", `${width}px`);
  canvas.css("height", `${height}px`);
}

function section1Animation() {
  let frameCount1 = 62,
    urls1 = new Array(frameCount1)
      .fill()
      .map((o, i) => `${BASE_URL}files/${PREFIXER}C01_v5_${i + 1}.png`);

  const images = imageSequence({
    urls: urls1,
    canvas: "#image-sequence1",
    clear: true,
    full: true,
    objectFit: "cover"
  });
}

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用GSAP(GreenSock Animation Platform)实现烟花效果需要一些基本的HTML、CSS和JS知识,以及对GSAP的了解。下面是一个基本的烟花效果实现步骤: 1. 创建HTML结构 首先,创建一个HTML文件并添加一个canvas元素,用于在其上绘制烟花效果。在此之前,可以添加一些样式来设置canvas的大小和位置。例如: ```html <!doctype html> <html> <head> <meta charset="utf-8"> <title>Fireworks with GSAP</title> <style> body { margin: 0; padding: 0; background-color: #000; } canvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } </style> </head> <body> <canvas id="canvas"></canvas> <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.5.1/gsap.min.js"></script> <script src="script.js"></script> </body> </html> ``` 2. 编写JS代码 在JS文件中,使用GSAP创建一个TweenMax对象,用于创建动画效果。下面的代码演示了如何创建一个基本的烟花效果: ```javascript var canvas = document.getElementById("canvas"), ctx = canvas.getContext("2d"), particles = [], particleCount = 30, colors = ["#FFFFFF", "#FF4136", "#0074D9", "#2ECC40", "#FFDC00", "#FF851B", "#B10DC9", "#111111"], WIDTH = window.innerWidth, HEIGHT = window.innerHeight; canvas.width = WIDTH; canvas.height = HEIGHT; function Particle() { this.radius = Math.random() * 20 + 10; this.x = Math.random() * (WIDTH - this.radius * 2) + this.radius; this.y = Math.random() * (HEIGHT - this.radius * 2) + this.radius; this.color = colors[Math.floor(Math.random() * colors.length)]; this.vx = Math.random() * 4 - 2; this.vy = Math.random() * 4 - 2; this.gravity = 0.2; this.opacity = 1; } Particle.prototype.draw = function() { ctx.globalAlpha = this.opacity; ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false); ctx.fillStyle = this.color; ctx.fill(); }; Particle.prototype.update = function() { this.x += this.vx; this.y += this.vy; this.opacity -= 0.01; this.radius -= 0.1; }; function createParticles() { for (var i = 0; i < particleCount; i++) { particles.push(new Particle()); } } function drawParticles() { for (var i = 0; i < particles.length; i++) { particles[i].draw(); } } function updateParticles() { for (var i = 0; i < particles.length; i++) { particles[i].update(); if (particles[i].opacity <= 0) { particles.splice(i, 1); } } } function animate() { requestAnimationFrame(animate); ctx.clearRect(0, 0, WIDTH, HEIGHT); drawParticles(); updateParticles(); } createParticles(); animate(); ``` 代码解释: - 创建canvas元素和一些变量,包括particles数组、particleCount(粒子数量)、颜色数组、canvas的宽度和高度。 - 创建Particle对象,包括粒子的半径、位置、颜色和速度等属性,以及draw(绘制)和update(更新)方法。 - 创建createParticles(创建粒子)、drawParticles(绘制粒子)和updateParticles(更新粒子)函数。 - 创建animate(动画)函数,使用requestAnimationFrame方法实现动画效果。 - 调用createParticles函数和animate函数,实现烟花效果的基本动画。 3. 添加交互效果 要使烟花效果更加有趣,可以添加一些交互效果。例如,点击鼠标时,可以在鼠标位置创建一个新的烟花效果。下面是如何实现这个交互效果的代码: ```javascript function addFirework(x, y) { for (var i = 0; i < particleCount; i++) { var p = new Particle(); p.x = x; p.y = y; particles.push(p); } } canvas.addEventListener("mousedown", function(e) { var x = e.clientX, y = e.clientY; addFirework(x, y); }); ``` 代码解释: - 创建addFirework函数,用于在指定位置创建烟花效果。 - 在canvas元素上添加mousedown事件,当鼠标按下时,获取鼠标位置,并调用addFirework函数创建烟花效果。 4. 添加动画效果 为了让烟花效果更加生动,可以添加一些动画效果,例如粒子的旋转和缩放效果。使用TweenMax对象可以轻松地实现这些效果。下面是如何添加粒子旋转和缩放效果的代码: ```javascript Particle.prototype.update = function() { this.x += this.vx; this.y += this.vy; this.opacity -= 0.01; this.radius -= 0.1; this.rotation += Math.random() * 20 - 10; this.scale -= 0.005; }; function drawParticles() { for (var i = 0; i < particles.length; i++) { var p = particles[i], tl = new TimelineMax(); tl.to(p, 0.5, {scale: 1.2, ease: Power1.easeInOut}) .to(p, 0.5, {scale: 1, ease: Power1.easeInOut}) .to(p, 0.5, {rotation: "+=360", ease: Power1.easeInOut}); p.draw(); } } ``` 代码解释: - 在Particle对象的update方法中,添加rotation(旋转)和scale(缩放)属性,并在更新方法中增加旋转和缩放效果。 - 在drawParticles函数中,使用TweenMax对象创建一个时间轴,包括粒子的缩放和旋转效果。 - 调用p.draw方法绘制粒子。 5. 完整代码 下面是完整的烟花效果代码: ```html <!doctype html> <html> <head> <meta charset="utf-8"> <title>Fireworks with GSAP</title> <style> body { margin: 0; padding: 0; background-color: #000; } canvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } </style> </head> <body> <canvas id="canvas"></canvas> <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.5.1/gsap.min.js"></script> <script> var canvas = document.getElementById("canvas"), ctx = canvas.getContext("2d"), particles = [], particleCount = 30, colors = ["#FFFFFF", "#FF4136", "#0074D9", "#2ECC40", "#FFDC00", "#FF851B", "#B10DC9", "#111111"], WIDTH = window.innerWidth, HEIGHT = window.innerHeight; canvas.width = WIDTH; canvas.height = HEIGHT; function Particle() { this.radius = Math.random() * 20 + 10; this.x = Math.random() * (WIDTH - this.radius * 2) + this.radius; this.y = Math.random() * (HEIGHT - this.radius * 2) + this.radius; this.color = colors[Math.floor(Math.random() * colors.length)]; this.vx = Math.random() * 4 - 2; this.vy = Math.random() * 4 - 2; this.gravity = 0.2; this.opacity = 1; this.rotation = 0; this.scale = 1; } Particle.prototype.draw = function() { ctx.save(); ctx.translate(this.x, this.y); ctx.rotate(this.rotation * Math.PI / 180); ctx.scale(this.scale, this.scale); ctx.globalAlpha = this.opacity; ctx.beginPath(); ctx.arc(0, 0, this.radius, 0, Math.PI * 2, false); ctx.fillStyle = this.color; ctx.fill(); ctx.restore(); }; Particle.prototype.update = function() { this.x += this.vx; this.y += this.vy; this.opacity -= 0.01; this.radius -= 0.1; this.rotation += Math.random() * 20 - 10; this.scale -= 0.005; }; function createParticles() { for (var i = 0; i < particleCount; i++) { particles.push(new Particle()); } } function drawParticles() { for (var i = 0; i < particles.length; i++) { var p = particles[i], tl = new TimelineMax(); tl.to(p, 0.5, {scale: 1.2, ease: Power1.easeInOut}) .to(p, 0.5, {scale: 1, ease: Power1.easeInOut}) .to(p, 0.5, {rotation: "+=360", ease: Power1.easeInOut}); p.draw(); } } function updateParticles() { for (var i = 0; i < particles.length; i++) { particles[i].update(); if (particles[i].opacity <= 0) { particles.splice(i, 1); } } } function addFirework(x, y) { for (var i = 0; i < particleCount; i++) { var p = new Particle(); p.x = x; p.y = y; particles.push(p); } } canvas.addEventListener("mousedown", function(e) { var x = e.clientX, y = e.clientY; addFirework(x, y); }); function animate() { requestAnimationFrame(animate); ctx.clearRect(0, 0, WIDTH, HEIGHT); drawParticles(); updateParticles(); } createParticles(); animate(); </script> </body> </html> ``` 运行代码后,点击鼠标可以在指定位置创建烟花效果,粒子会旋转和缩放,直到消失。可以尝试修改代码中的参数,例如粒子数量、颜色数组、速度等,以获得不同的烟花效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值