gpt4 book ai didi

javascript - onmouseover onmouseout 图像变化液体效果

转载 作者:搜寻专家 更新时间:2023-11-01 04:34:40 24 4
gpt4 key购买 nike

我需要像 here 中那样用液体效果实现图像变化

我有一个带有图像的简单 block 我需要在 onmouseover 中使用此效果更改此图像(到其他图像)并返回到 onmouseout 中的初始位置也使用此效果

const avatarQuantumBreak = document.querySelector(".avatar_quantum_break");
const avatar = document.querySelector(".avatar");

avatarQuantumBreak.style.opacity = "0";

let hover = () => avatarQuantumBreak.style.opacity = "1";
let normal = () => avatarQuantumBreak.style.opacity = "0";

avatar.onmouseover = () => hover();
avatar.onmouseout = () => normal();
html , body {
height:100%;
}

.avatar {
position: relative;
border-radius: 50%;
display: flex;
justify-content: center;
height: 195px;
}
.avatar_simple,
.avatar_quantum_break {
position: absolute;
display: block;
text-align:center;
transition: opacity 1s ease-out;
}
.avatar .avatar_simple img,
.avatar .avatar_quantum_break img {
border-radius: 50%;
display: inline-block;
width: 86%;
height: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.0.2/TweenMax.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/97/three.min.js"></script>


<div class=avatar>
<span class=avatar_simple>
<img src="https://pixel.nymag.com/imgs/fashion/daily/2014/05/27/27-amber-heard.w330.h330.jpg">
</span>
<span class=avatar_quantum_break>
<img src="https://pixel.nymag.com/imgs/daily/vulture/2016/05/31/31-amber-heard.w330.h330.jpg">
</span>
</div>

下面是触发液体动画的图像transition函数

transitionNext() {
TweenMax.to(this.mat.uniforms.dispPower, 2.5, {
value: 1,
ease: Expo.easeInOut,
onUpdate: this.render,
onComplete: () => {
this.mat.uniforms.dispPower.value = 0.0
this.changeTexture()
this.render.bind(this)
this.state.animating = false
}
})

我尝试使用此功能,但这对我没有帮助。

我也尝试更改 Array15 中的图像,但这也没有帮助。

this.images = [ //1
'https://s3-us-west-2.amazonaws.com/s.cdpn.io/58281/bg1.jpg',
'https://s3-us-west-2.amazonaws.com/s.cdpn.io/58281/bg2.jpg',
'https://s3-us-west-2.amazonaws.com/s.cdpn.io/58281/bg3.jpg'
]

这个函数启动了动画

listeners() {
window.addEventListener('wheel', this.nextSlide, { passive: true })
}

下一张幻灯片功能

nextSlide() {
if (this.state.animating) return

this.state.animating = true

this.transitionNext()

this.data.current = this.data.current === this.data.total ? 0 : this.data.current + 1
this.data.next = this.data.current === this.data.total ? 0 : this.data.current + 1
}

请帮忙..

最佳答案

不错的一个 - 实时 vfx 与 web 开发相结合:)

这个效果的所有魔法都是用 GLSL 着色器完成的(你可以在示例 html 的底部看到它)我在这里添加了一些评论

  // next lines are input data that gpu gets form javascript
varying vec2 vUv; // uv coordinate of current pixel
uniform sampler2D texture1; // picture 1
uniform sampler2D texture2; // picture 2
uniform sampler2D disp; // noise texture
uniform float dispPower; // effect progress
uniform float intensity; // effect scale

void main() {
vec2 uv = vUv;

vec4 disp = texture2D(disp, uv); // read noise texture
vec2 dispVec = vec2(disp.x, disp.y); // get red and green values

// calculate uv displacement
vec2 distPos1 = uv + (dispVec * intensity * dispPower);
vec2 distPos2 = uv + (dispVec * -(intensity * (1.0 - dispPower)));

// sample images with displaced uv
vec4 _texture1 = texture2D(texture1, distPos1);
vec4 _texture2 = texture2D(texture2, distPos2);

// mix both pictures using effect dispPower value and output pixel color
gl_FragColor = mix(_texture1, _texture2, dispPower);
}

它需要 3 个纹理作为输入:picture1 picture2 和用于扭曲 uv 的噪声纹理并在 GPU 上动态生成一帧过渡效果的一个像素的颜色值此着色器应用于表面的所有像素

此处使用的技术称为“纹理失真”或“UV 位移”这个想法是使用存储在噪声纹理中的数据调整 UV 坐标。

开始学习 GLSL 的好地方是 https://thebookofshaders.com/

GLSL 引用 http://www.shaderific.com/glsl/

另外,我建议访问 https://www.shadertoy.com/

欢迎来到实时视觉特效的神奇世界

关于javascript - onmouseover onmouseout 图像变化液体效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54118016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com