Upload streamlit_app.py
Browse files- streamlit_app.py +103 -0
streamlit_app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import streamlit.components.v1 as components
|
| 3 |
+
import time
|
| 4 |
+
|
| 5 |
+
st.title("Shader + Mouse Demo")
|
| 6 |
+
|
| 7 |
+
# Display mouse coordinates from Streamlit sliders
|
| 8 |
+
st.sidebar.subheader("Mouse coordinates (simulate / control)")
|
| 9 |
+
mouse_x = st.sidebar.slider("Mouse X (0–1)", 0.0, 1.0, 0.5)
|
| 10 |
+
mouse_y = st.sidebar.slider("Mouse Y (0–1)", 0.0, 1.0, 0.5)
|
| 11 |
+
|
| 12 |
+
# WebGL shader HTML
|
| 13 |
+
html_code = f"""
|
| 14 |
+
<canvas id="glcanvas" width="500" height="400"></canvas>
|
| 15 |
+
<script type="text/javascript">
|
| 16 |
+
const canvas = document.getElementById('glcanvas');
|
| 17 |
+
const gl = canvas.getContext('webgl');
|
| 18 |
+
|
| 19 |
+
if (!gl) {{
|
| 20 |
+
alert('WebGL not supported');
|
| 21 |
+
}}
|
| 22 |
+
|
| 23 |
+
// Vertex shader
|
| 24 |
+
const vertCode = `
|
| 25 |
+
attribute vec4 position;
|
| 26 |
+
void main() {{
|
| 27 |
+
gl_Position = position;
|
| 28 |
+
}}
|
| 29 |
+
`;
|
| 30 |
+
|
| 31 |
+
// Fragment shader
|
| 32 |
+
const fragCode = `
|
| 33 |
+
precision mediump float;
|
| 34 |
+
uniform float iTime;
|
| 35 |
+
uniform vec2 iMouse;
|
| 36 |
+
uniform vec2 iResolution;
|
| 37 |
+
|
| 38 |
+
void main() {{
|
| 39 |
+
vec2 uv = gl_FragCoord.xy / iResolution.xy;
|
| 40 |
+
vec3 color = vec3(uv.x + iMouse.x*0.5 + sin(iTime)*0.2,
|
| 41 |
+
uv.y + iMouse.y*0.5 + cos(iTime)*0.2,
|
| 42 |
+
0.5 + 0.5*sin(iTime));
|
| 43 |
+
gl_FragColor = vec4(color, 1.0);
|
| 44 |
+
}}
|
| 45 |
+
`;
|
| 46 |
+
|
| 47 |
+
// Compile helper
|
| 48 |
+
function compileShader(gl, source, type) {{
|
| 49 |
+
const shader = gl.createShader(type);
|
| 50 |
+
gl.shaderSource(shader, source);
|
| 51 |
+
gl.compileShader(shader);
|
| 52 |
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {{
|
| 53 |
+
console.error(gl.getShaderInfoLog(shader));
|
| 54 |
+
return null;
|
| 55 |
+
}}
|
| 56 |
+
return shader;
|
| 57 |
+
}}
|
| 58 |
+
|
| 59 |
+
// Compile shaders
|
| 60 |
+
const vertShader = compileShader(gl, vertCode, gl.VERTEX_SHADER);
|
| 61 |
+
const fragShader = compileShader(gl, fragCode, gl.FRAGMENT_SHADER);
|
| 62 |
+
|
| 63 |
+
// Create program
|
| 64 |
+
const program = gl.createProgram();
|
| 65 |
+
gl.attachShader(program, vertShader);
|
| 66 |
+
gl.attachShader(program, fragShader);
|
| 67 |
+
gl.linkProgram(program);
|
| 68 |
+
gl.useProgram(program);
|
| 69 |
+
|
| 70 |
+
// Fullscreen quad
|
| 71 |
+
const vertices = new Float32Array([
|
| 72 |
+
-1,-1, 1,-1, -1,1,
|
| 73 |
+
-1,1, 1,-1, 1,1
|
| 74 |
+
]);
|
| 75 |
+
const buffer = gl.createBuffer();
|
| 76 |
+
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
|
| 77 |
+
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
|
| 78 |
+
const position = gl.getAttribLocation(program, "position");
|
| 79 |
+
gl.enableVertexAttribArray(position);
|
| 80 |
+
gl.vertexAttribPointer(position, 2, gl.FLOAT, false, 0, 0);
|
| 81 |
+
|
| 82 |
+
// Uniform locations
|
| 83 |
+
const iTime = gl.getUniformLocation(program, "iTime");
|
| 84 |
+
const iResolution = gl.getUniformLocation(program, "iResolution");
|
| 85 |
+
const iMouse = gl.getUniformLocation(program, "iMouse");
|
| 86 |
+
|
| 87 |
+
gl.uniform2f(iResolution, canvas.width, canvas.height);
|
| 88 |
+
|
| 89 |
+
// Animation loop
|
| 90 |
+
let startTime = Date.now();
|
| 91 |
+
function render() {{
|
| 92 |
+
let t = (Date.now() - startTime) / 1000.0;
|
| 93 |
+
gl.uniform1f(iTime, t);
|
| 94 |
+
gl.uniform2f(iMouse, {mouse_x}, {mouse_y}); // send Streamlit slider values
|
| 95 |
+
gl.drawArrays(gl.TRIANGLES, 0, 6);
|
| 96 |
+
requestAnimationFrame(render);
|
| 97 |
+
}}
|
| 98 |
+
render();
|
| 99 |
+
</script>
|
| 100 |
+
"""
|
| 101 |
+
|
| 102 |
+
components.html(html_code, height=420)
|
| 103 |
+
st.write(f"Mouse in Streamlit sliders: X={mouse_x:.2f}, Y={mouse_y:.2f}")
|