Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
html, body { margin: 0; padding: 0; height: 100%; overflow: hidden; }
JavaScript
/*------------------------------ Map ------------------------------*/ const map = (value, x1, y1, x2, y2) => (value - x1) * (y2 - x2) / (y1 - x1) + x2; /*------------------------------ Mouse ------------------------------*/ const mouse = { x: 0, y: 0 }; window.addEventListener('mousemove', e => { mouse.x = e.clientX; mouse.y = e.clientY; }); window.addEventListener('touchstart', e => { mouse.x = e.touches[0].clientX; mouse.y = e.touches[0].clientY; }); /*------------------------------ GLSL TEMPLATE ------------------------------*/ class GLSLTemplate { constructor(opt) { Object.assign(this, opt); this.loadImages(); } /*------------------------------ Load Image ------------------------------*/ loadImages() { this.textures = []; let loadedImages = 0; // loop images for (let i = 0; i < this.images.length; i++) { const textureLoader = new THREE.TextureLoader(); textureLoader.crossOrigin = ''; textureLoader.load(this.images[i], img => { img.magFilter = THREE.NearestFilter; this.textures.push({ texture: img }); this.checkLoadedImages(); }); } } /*------------------------------ Check Load Images ------------------------------*/ checkLoadedImages() { if (this.textures.length === this.images.length) { this.setTexturesRatio(); this.setup(); } } /*------------------------------ Set Textures Ratio ------------------------------*/ setTexturesRatio() { this.winRatio = window.innerWidth / window.innerHeight; console.log(this.winRatio); for (let i = 0; i < this.textures.length; i++) { const t = this.textures[i]; const textureRatio = t.texture.image.naturalWidth / t.texture.image.naturalHeight; t.ratio = this.winRatio > textureRatio ? new THREE.Vector2(1.0, textureRatio / this.winRatio) : new THREE.Vector2(this.winRatio / textureRatio, 1.0); } } /*------------------------------ Setup ------------------------------*/ setup() { this.uniforms = { time: { type: "f", value: 1.0 }, resolution: { type: "v2", value: new THREE.Vector2() }, u_mouse: { type: "v2", value: new THREE.Vector2(0, 0) }, texture_0: { type: "t", value: this.textures[0].texture }, ratio_0: { type: "v2", value: this.textures[0].ratio }, texture_1: { type: "t", value: this.textures[1].texture }, ratio_1: { type: "v2", value: this.textures[1].ratio } }; this.vertexShader = ` void main() { gl_Position = vec4(position, 1.0); } `; this.fragmentShader = ` uniform vec2 resolution; uniform float time; uniform vec2 u_mouse; uniform sampler2D texture_0; uniform sampler2D texture_1; uniform vec2 ratio_0; uniform vec2 ratio_1; void main(){ vec2 uv = gl_FragCoord.xy / resolution.xy; // IMAGE 0 vec2 ratio_image_0 = uv * ratio_0; if (ratio_0.x < 1.) { ratio_image_0.x += ((1. - ratio_0.x) / 2.); } if (ratio_0.y < 1.) { ratio_image_0.y += ((1. - ratio_0.y) / 2.); } vec4 image_0 = texture2D(texture_0, ratio_image_0); // IMAGE 1 vec2 ratio_image_1 = uv * ratio_1; if (ratio_1.x < 1.) { ratio_image_1.x += ((1. - ratio_1.x) / 2.); } if (ratio_1.y < 1.) { ratio_image_1.y += ((1. - ratio_1.y) / 2.); } vec4 image_1 = texture2D(texture_1, ratio_image_1); // SLIDE PROGRESS float slideProgress = u_mouse.y; float colorProgress = uv.y + slideProgress; colorProgress = slideProgress * 4.8 - uv.y * 3. + uv.x * 0.8 - 0.8; colorProgress = clamp(colorProgress, 0., 1.); // gl_FragColor = vec4(vec3(colorProgress), 0.0); // gl_FragColor = image_1; gl_FragColor = mix(image_0,image_1,colorProgress); } `; this.startTime = Date.now(); this.camera = new THREE.Camera(); this.scene = new THREE.Scene(); this.init(); } /*------------------------------ Init ------------------------------*/ init() { this.camera.position.z = 1; this.geometry = new THREE.PlaneBufferGeometry(16, 9); this.material = new THREE.ShaderMaterial({ uniforms: this.uniforms, vertexShader: this.vertexShader, fragmentShader: this.fragmentShader }); this.mesh = new THREE.Mesh(this.geometry, this.material); this.scene.add(this.mesh); this.renderer = new THREE.WebGLRenderer(); this.container.appendChild(this.renderer.domElement); this.resize(); this.render(); this.events(); } /*------------------------------ Events ------------------------------*/ events() { window.addEventListener('resize', this.resize.bind(this), false); } /*------------------------------ Resize ------------------------------*/ resize() { this.setTexturesRatio(); this.uniforms.resolution.value.x = window.innerWidth; this.uniforms.resolution.value.y = window.innerHeight; this.uniforms.ratio_0.value = this.textures[0].ratio; this.uniforms.ratio_1.value = this.textures[1].ratio; this.renderer.setSize(window.innerWidth, window.innerHeight); } /*------------------------------ Render ------------------------------*/ render() { this.currentTime = Date.now(); this.elaspedSeconds = (this.currentTime - this.startTime) / 1000.0; this.uniforms.time.value = this.elaspedSeconds; this.uniforms.u_mouse.value.x = map(mouse.x, 0, window.innerWidth, 0, 1); this.uniforms.u_mouse.value.y = map(mouse.y, 0, window.innerHeight, 1, 0); this.renderer.render(this.scene, this.camera); this.RaF = requestAnimationFrame(this.render.bind(this)); }} /*------------------------------ Initialize ------------------------------*/ const shader = new GLSLTemplate({ container: document.getElementById('container'), images: [ 'https://images.unsplash.com/photo-1418386767268-77cdab4edcaa?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjI0MX0&auto=format&fit=crop&w=1920&q=90', 'https://images.unsplash.com/photo-1474433188271-d3f339f41911?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjI0MX0&auto=format&fit=crop&w=1920&q=90'] });
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>three.js图片过渡效果-jq22.com</title> <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script> <style>
</style> </head> <body>
<script>
</script>
</body> </html>
2012-2021 jQuery插件库版权所有
jquery插件
|
jq22工具库
|
网页技术
|
广告合作
|
在线反馈
|
版权声明
沪ICP备13043785号-1
浙公网安备 33041102000314号