Анимированная кинематографическая лента с фильмами на HTML5 Canvas
Оригинальное решение для кинопорталов. Анимированная лента с кадрами из фильмов, которую можно проматывать колёсиком мышки
Для начала посмотрите ДЕМО
Установка:
На странице, где должна быть такая лента, между тегами <body> и </body> вставьте:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
<script src="/js/three.js"></script> <script type="text/javascript"> const infinite = false; const waveLength = 1.1; const slideCount = 12; const precision = 25; const raduis = 0.7; const filmWidth = 0.3; const filmColor = '#876' const three = configureThree(); const customDepthMaterial = createDepthMaterial(); const uniforms = { rotation: { value: 0 }, }; const film = new THREE.Object3D(); three.scene.add(film); for (let i = 1; i < slideCount+1; i++) addSlide(i, `/img/${i}.jpg`) let rotation = 1.2; requestAnimationFrame(function render(t) { moveFilm(t); three.render(); requestAnimationFrame(render); }); // constols let touchStartY; let rotationAtStart; addEventListener('touchstart', (e) => { touchStartY = e.touches[0].clientY; rotationAtStart = rotation; }); addEventListener('touchmove', (e) => { rotation = rotationAtStart + (e.touches[0].clientY - touchStartY)/50; }); addEventListener('wheel', (e) => { rotation -= e.deltaY/400; }); function configureThree(){ const renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.shadowMap.enabled = true; renderer.shadowMap.type = THREE.PCFSoftShadowMap; renderer.localClippingEnabled = true; document.body.appendChild(renderer.domElement); const scene = new THREE.Scene(); scene.add(new THREE.AmbientLight('white', 0.3)); scene.add(createDirectionalLight()); const camera = new THREE.OrthographicCamera(); camera.position.set(0,0,1.5); camera.lookAt(scene.position); return { scene, render }; function render() { if (renderer.width !== innerWidth || renderer.height !== innerHeight ){ renderer.setSize(innerWidth, innerHeight); camera.left = -innerWidth/innerHeight camera.right = -camera.left camera.updateProjectionMatrix(); } renderer.render(scene, camera); } } function createDirectionalLight(){ const dirLight = new THREE.DirectionalLight('white', 0.7); dirLight.castShadow = true; dirLight.position.set(-0.1,0.1,1); dirLight.shadow.mapSize.width = 1024; dirLight.shadow.mapSize.height = 2048; dirLight.shadow.camera.left = -1; dirLight.shadow.camera.right = 1; dirLight.shadow.camera.top = 2; dirLight.shadow.camera.bottom = -2; dirLight.shadow.camera.near = 0.1; dirLight.shadow.camera.far = 2; return dirLight; } function addSlide(i, url) { const img = document.createElement('img'); img.crossOrigin = 'anonymous'; img.src = url; img.onload = () => { const geom = createGeometry(i); let slide = new THREE.Mesh(geom, createMaterial(img, i, -1)); slide.receiveShadow = true; film.add(slide); slide = new THREE.Mesh(geom, createMaterial(img, i, 1)); slide.castShadow = true; slide.customDepthMaterial = customDepthMaterial; slide.customDepthMaterial.map = slide.material.map; film.add(slide); }; } function createMaterial(img, index, side) { const mat = new THREE.MeshStandardMaterial({ map: cteateTexture(img), side: THREE.DoubleSide, clippingPlanes: [new THREE.Plane(new THREE.Vector3( 0, 0, side ), 0.0001 )], alphaTest: 0.5 }); mat.onBeforeCompile = shader => { shader.uniforms.rotation = uniforms.rotation; shader.uniforms.slideIndex = {value: index}; let main = 'void main(' let out = 'gl_FragColor = vec4( outgoingLight, diffuseColor.a );' shader.fragmentShader = shader.fragmentShader.split(main).join( ` uniform float rotation; uniform float slideIndex; ` + main).split(out).join(out + ` float value = abs(slideIndex + vUv.x - rotation - 2.0); value = clamp(value, 0., 1.); if (abs(vUv.y - 0.5) < 0.38 && abs(vUv.x - 0.5) < 0.46) { float grayscale = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); gl_FragColor.rgb = mix(gl_FragColor.rgb, vec3(grayscale), value); } `); } return mat; } function createDepthMaterial() { const mat = new THREE.MeshDepthMaterial( { depthPacking: THREE.RGBADepthPacking, alphaTest: 0.5 }); mat.onBeforeCompile = shader => { shader.fragmentShader = shader .fragmentShader.split('}') .join(`if (fragCoordZ>0.5) {discard;}}`) //console.log(shader.fragmentShader) } return mat; } function cteateTexture(img) { const pad = 10; const width = img.width + pad*2; const holesCount = 10; const w = width/holesCount/2; const h = w; const canvas = document.createElement('canvas'); canvas.height = img.height + pad*4+h*2; canvas.width = width; const ctx = canvas.getContext('2d'); ctx.fillStyle = filmColor; ctx.fillRect(0,0,1e5,1e5); ctx.drawImage(img, pad, pad*2+h); for (let i = 0; i<holesCount; i++) { const x = i*2*w + w/2; ctx.clearRect(x, pad, w, h) ctx.clearRect(x, canvas.height-pad-h, w, h) } const texture = new THREE.CanvasTexture(canvas); texture.anisotropy=4 return texture } function createGeometry(i) { const vertices = [] ; const uvs = []; for (let n = 0; n < precision; n++){ addVertices(vertices, i, n) addUvs(uvs, n); } for (let j = 0; j < vertices.length; j += 3) { vertices[j] = Math.cos(vertices[j])*raduis; vertices[j+2] = Math.sin(vertices[j+2])*raduis; } const geom = new THREE.BufferGeometry(); geom.setAttribute('position', new THREE.BufferAttribute(new Float32Array(vertices), 3)); geom.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(uvs), 2)); geom.computeVertexNormals(); return geom; } function addVertices(vertices, i, n) { const index = i*precision+n; const i1 = index/(slideCount * precision); const i2 = (index+1)/(slideCount * precision); const a1 = i1*Math.PI*2; const a2 = i2*Math.PI*2 const dy1 = i1*waveLength - filmWidth; const dy2 = i2*waveLength - filmWidth; vertices.push( a1, filmWidth+dy1, a1, a1, -filmWidth+dy1, a1, a2, -filmWidth+dy2, a2, a2, filmWidth+dy2, a2, a2, -filmWidth+dy2, a2, a1, filmWidth+dy1, a1, ); } function addUvs(uvs, n){ const x0 = n/precision; const x1 = (n+1)/precision; uvs.push( x0, 1, x0, 0, x1, 0, x1, 1, x1, 0, x0, 1, ); } function moveFilm(t){ film.rotation.y += (rotation - film.rotation.y)/20; film.rotation.y += Math.sin(t/2000)/500 film.position.y = -film.rotation.y/(Math.PI*2/waveLength); if (infinite) { film.position.y = film.position.y%waveLength - waveLength; } uniforms.rotation.value = slideCount * film.rotation.y / (Math.PI*2) } </script> |
Осталось лишь залить JS файл из прикреплённого архива в папку js и все картинки в папку img