<section class="section section--1">
<canvas class="section__canv" data-src="https://images.unsplash.com/photo-1574158622682-e40e69881006?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=512&h=512&q=80"></canvas>
</section>
<section class="section section--2">
<canvas class="section__canv" data-src="https://images.unsplash.com/photo-1592194996308-7b43878e84a6?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=512&h=512&q=80"></canvas>
</section>
<section class="section section--3">
<canvas class="section__canv" data-src="https://images.unsplash.com/photo-1536590158209-e9d615d525e4?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=512&h=512&q=80"></canvas>
</section>
<section class="section section--4">
<canvas class="section__canv" data-src="https://images.unsplash.com/photo-1518791841217-8f162f1e1131?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=512&h=512&q=80"></canvas>
</section>
<section class="section section--5">
<canvas class="section__canv" data-src="https://images.unsplash.com/photo-1533738363-b7f9aef128ce?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=512&h=512&q=80"></canvas>
</section>
body {
margin: 0;
}
.section {
min-height: 75vh;
display: grid;
place-content: center;
background-color: #bbb;
}
.section--1 {
margin-top: 50vh;
}
.section--5 {
margin-bottom: 30vh;
}
.section:nth-child(even) {
background-color: #ddd;
}
.section__canv {
display: block;
}
import {
Renderer,
Triangle,
Program,
TextureLoader,
Transform,
Mesh,
Vec2,
Vec3
} from "https://cdn.skypack.dev/ogl@0.0.76";
import ScrollMagic from "https://cdn.skypack.dev/scrollmagic@2.0.8";
import "https://cdn.skypack.dev/scrollmagic@2.0.8/scrollmagic/uncompressed/plugins/debug.addIndicators";
const vertex = /* glsl */ `
attribute vec2 uv;
attribute vec2 position;
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = vec4(position, 0, 1);
}`;
const fragment = /* glsl */ `
precision highp float;
uniform float uProgress;
uniform sampler2D tMap;
uniform vec3 uResolution;
varying vec2 vUv;
// Simplex 2D noise
//
vec3 permute(vec3 x) { return mod(((x*34.0)+1.0)*x, 289.0); }
float snoise(vec2 v){
const vec4 C = vec4(0.211324865405187, 0.366025403784439,
-0.577350269189626, 0.024390243902439);
vec2 i = floor(v + dot(v, C.yy) );
vec2 x0 = v - i + dot(i, C.xx);
vec2 i1;
i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
vec4 x12 = x0.xyxy + C.xxzz;
x12.xy -= i1;
i = mod(i, 289.0);
vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 ))
+ i.x + vec3(0.0, i1.x, 1.0 ));
vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy),
dot(x12.zw,x12.zw)), 0.0);
m = m*m ;
m = m*m ;
vec3 x = 2.0 * fract(p * C.www) - 1.0;
vec3 h = abs(x) - 0.5;
vec3 ox = floor(x + 0.5);
vec3 a0 = x - ox;
m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
vec3 g;
g.x = a0.x * x0.x + h.x * x0.y;
g.yz = a0.yz * x12.xz + h.yz * x12.yw;
return 130.0 * dot(m, g);
}
vec2 cover(vec2 res, vec2 uv, vec2 pivot) {
vec2 ratio = vec2(max(res.x, res.y)) / res;
return (uv - pivot) / ratio + pivot;
}
void main() {
vec2 uv = cover(uResolution.xy, vUv, vec2(0.5));
float noize = snoise(uv * 6.0) * 0.5 + 0.5;
noize = smoothstep(uProgress - 0.05, uProgress + 0.05, mix(0.05, 0.95, noize));
vec4 tex = texture2D(tMap, uv);
gl_FragColor.rgb = tex.rgb;
gl_FragColor.a = 1.0 - noize;
}`;
function onSectionScroll(progress, renderer, scene, mesh) {
mesh.program.uniforms.uProgress.value = progress;
renderer.render({ scene });
}
const controller = new ScrollMagic.Controller({ addIndicators: true });
document.querySelectorAll(".section").forEach((section) => {
const canvas = section.querySelector(".section__canv");
const imageSrc = canvas.dataset.src;
if (!imageSrc) return;
const renderer = new Renderer({ canvas, antialias: true, alpha: true });
renderer.setSize(300, 200);
const gl = renderer.gl;
gl.clearColor(0, 0, 0, 0);
const scene = new Transform();
const geometry = new Triangle(gl);
const texture = TextureLoader.load(gl, { src: { jpg: imageSrc }});
const program = new Program(gl, {
vertex,
fragment,
uniforms: {
uProgress: { value: 0 },
uResolution: { value: new Vec3(300, 200, 1) },
tMap: { value: texture },
},
});
const mesh = new Mesh(gl, { geometry, program });
mesh.setParent(scene);
texture.loaded.then(() => renderer.render({ scene }));
new ScrollMagic.Scene({
triggerElement: section,
duration: () => section.offsetHeight * 1
})
.on("progress", ({ progress }) => onSectionScroll(progress, renderer, scene, mesh))
//.trigger("progress")
.addTo(controller);
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.