Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
body { margin: 0; overflow: hidden; background: white; } .canvas-wrap { display: flex; align-items: center; justify-content: center; width: 100%; height: 100vh; } .canvas-inner { perspective: 75px; } canvas { width: 512px; height: 512px; }
JavaScript
class Ting { constructor() { this.canvas = document.getElementById("canvas"); this.setup = this.setup.bind(this); this.render = this.render.bind(this); this.onMouseMove = this.onMouseMove.bind(this); this.setup(); this.addEvents(); } addEvents() { window.addEventListener("mousemove", this.onMouseMove); // window.addEventListener('touchmove', this.onMouseMove) } setup() { this.mousePerspective = { _x: 0, _y: 0, x: 0, y: 0, origin: { x: 0, y: 0 }, updatePosition: function (e) { var e = event || window.event; this.x = e.clientX - this.origin.x; this.y = (e.clientY - this.origin.y) * -1; }, setOrigin: function (e) { this.origin.x = e.offsetLeft + Math.floor(e.offsetWidth / 2); this.origin.y = e.offsetTop + Math.floor(e.offsetHeight / 2); } }; this.mouseCanvas = { x: -500, y: -500, _x: -500, _y: -500 }; this.gl = this.canvas.getContext("webgl", { premultipliedAlpha: false }); this.rect = this.canvas.getBoundingClientRect(); // compile shaders, link program, lookup location this.programInfo = twgl.createProgramInfo(this.gl, ["vs", "fs"]); this.bufferInfo = twgl.primitives.createXYQuadBufferInfo(this.gl); this.mousePerspective.setOrigin(this.canvas); this.imageSize = { width: 1, height: 1 }; // replaced after loading this.texture = twgl.createTextures( this.gl, { darkTexture: { src: "http://www.jq22.com/tp/6370695285702246118614599.png" } }, (err, textures, sources) => { // Textures Loaded if (err) { console.error(err); } this.imageSize = sources.darkTexture; this.darkTexture = textures.darkTexture; this.render(); }); } render(time) { twgl.resizeCanvasToDisplaySize(this.gl.canvas); this.gl.viewport(0, 0, this.gl.canvas.width, this.gl.canvas.height); this.gl.clearColor(0, 0, 0, 0); this.gl.clear(this.gl.COLOR_BUFFER_BIT); this.gl.useProgram(this.programInfo.program); // calls gl.bindBuffer, gl.enableVertexAttribArray, gl.vertexAttribPointer twgl.setBuffersAndAttributes(this.gl, this.programInfo, this.bufferInfo); const canvasAspect = this.gl.canvas.clientWidth / this.gl.canvas.clientHeight; const imageAspect = this.imageSize.width / this.imageSize.height; const mat = this.scaling(imageAspect / canvasAspect, -1); // calls gl.activeTexture, gl.bindTexture, gl.uniformXXX twgl.setUniforms(this.programInfo, { u_matrix: mat, u_darkImage: this.darkTexture, u_mouse: [this.mouseCanvas.x, this.mouseCanvas.y], u_time: time * 0.001 }); this.mouseCanvas.x = this.lerp( this.mouseCanvas.x, this.mouseCanvas._x, 0.1); this.mouseCanvas.y = this.lerp( this.mouseCanvas.y, this.mouseCanvas._y, 0.1); this.mousePerspective.x = this.lerp( this.mousePerspective.x, this.mousePerspective._x, 0.1); this.mousePerspective.y = this.lerp( this.mousePerspective.y, this.mousePerspective._y, 0.1); this.canvas.style.transform = `rotateX(${ this.mousePerspective._x }deg) rotateY(${this.mousePerspective._y}deg)`; // calls gl.drawArrays or gl.drawElements twgl.drawBufferInfo(this.gl, this.bufferInfo); requestAnimationFrame(this.render); } onMouseMove(e) { this.mousePerspective.updatePosition(e); this.mouseCanvas._x = (e.clientX - this.rect.left) * this.canvas.width / this.rect.width; this.mouseCanvas._y = this.canvas.height - (e.clientY - this.rect.top) * this.canvas.height / this.rect.height; this.mousePerspective._x = ( this.mousePerspective.y / this.canvas.offsetHeight / 2). toFixed(2); this.mousePerspective._y = ( this.mousePerspective.x / this.canvas.offsetWidth / 2). toFixed(2); } /** * Creates a 2D scaling matrix * @param {number} sx amount to scale in x * @param {number} sy amount to scale in y * @return {Matrix3} a scale matrix that scales by sx and sy. */ scaling(sx, sy) { return [sx, 0, 0, 0, sy, 0, 0, 0, 1]; } lerp(start, end, amt) { return (1 - amt) * start + amt * end; } destroy() { // TODO: clean }} new Ting();
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>WebGL波浪图像悬停显示(TWGL)-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号