利用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"
  });
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值