跳过主要内容

在图像序列中擦除<canvas>图像序列

创建一个图像URL数组,将其与对你的<canvas>对象的引用以及一个ScrollTrigger配置对象一起传递给此辅助函数,让它依次浏览这些图像,并将合适的图像绘制到画布上。

演示

加载中...

/*
Helper function that handles scrubbing through a sequence of images, drawing the appropriate one to the provided canvas.
Config object properties:
- urls [Array]: an Array of image URLs
- canvas [Canvas]: the <canvas> object to draw to
- scrollTrigger [Object]: an optional ScrollTrigger configuration object like {trigger: "#trigger", start: "top top", end: "+=1000", scrub: true, pin: true}
- onUpdate [Function]: optional callback for when the Tween updates (probably not used very often)

Returns a Tween instance
*/
function imageSequence(config) {
let playhead = {frame: 0},
ctx = gsap.utils.toArray(config.canvas)[0].getContext("2d"),
onUpdate = config.onUpdate,
images,
updateImage = function() {
ctx.drawImage(images[Math.round(playhead.frame)], 0, 0);
onUpdate && onUpdate.call(this);
};
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,
scrollTrigger: config.scrollTrigger
});
}

使用方式:

imageSequence({
urls, // Array of image URLs
canvas: "#image-sequence", // <canvas> object to draw images to
scrollTrigger: {
start: 0, // start at the very top
end: "max", // entire page
scrub: true // important!
}
});
无噪 Logo
无噪文档
中文文档 · 复刻官网
查看所有 ↗