简体   繁体   English

如何使用 WebGL 渲染按钮背景?

[英]How to use WebGL to render a button background?

Let's say that I have a GL shader I'd want to use as an HTML button background (it's a pretty animated gradient).假设我有一个 GL 着色器,我想用作 HTML button背景(这是一个漂亮的动画渐变)。 How can I achieve that?我怎样才能做到这一点?

I can think of setting the background to a texture and rendering the shader to this texture.我可以考虑将背景设置为纹理并将着色器渲染到该纹理。 Is that fundamentally possible?这从根本上可能吗?

Using a canvas inside a button is the most logical way to go, note that this is just an example and not a shader, but we use the canvas to render WebGL so it can be slightly different for your case.在按钮内使用canvas是 go 最合乎逻辑的方式,请注意,这只是一个示例而不是着色器,但我们使用 canvas 来呈现 WebGL,因此它可能与您的情况略有不同。

 var c = document.getElementsByTagName("canvas")[0]; var ctx = c.getContext("2d"); // Create gradient var grd = ctx.createLinearGradient(0, 0, 200, 0); grd.addColorStop(0, "red"); grd.addColorStop(1, "green"); // Fill with gradient ctx.fillStyle = grd; ctx.fillRect(0, 0, 400, 200);
 button { position: relative; background-color: transparent; border: none; outline: none; padding: 8px; } button > div { position: relative; z-index: 1; } canvas { position: absolute; top: 0; right: 0; width: 100%; height: 100%; z-index: -1; pointer-events: none; }
 <button> <canvas></canvas> <div>Fancy Button</div> </button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM