Upload sa2.py
Browse files- src/sa2.py +115 -0
src/sa2.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import streamlit.components.v1 as components
|
| 3 |
+
|
| 4 |
+
st.title("Mobile-Friendly Shader Demo")
|
| 5 |
+
|
| 6 |
+
# HTML + JS shader component
|
| 7 |
+
html_code = """
|
| 8 |
+
<canvas id="glcanvas"></canvas>
|
| 9 |
+
<script type="text/javascript">
|
| 10 |
+
// Setup canvas
|
| 11 |
+
const canvas = document.getElementById('glcanvas');
|
| 12 |
+
const gl = canvas.getContext('webgl');
|
| 13 |
+
if (!gl) { alert('WebGL not supported'); }
|
| 14 |
+
|
| 15 |
+
// Resize canvas to fit screen
|
| 16 |
+
function resizeCanvas() {
|
| 17 |
+
canvas.width = window.innerWidth * window.devicePixelRatio;
|
| 18 |
+
canvas.height = window.innerHeight * 0.6 * window.devicePixelRatio; // ~60% height
|
| 19 |
+
}
|
| 20 |
+
window.addEventListener('resize', resizeCanvas);
|
| 21 |
+
resizeCanvas();
|
| 22 |
+
|
| 23 |
+
// Vertex shader
|
| 24 |
+
const vertCode = `
|
| 25 |
+
attribute vec4 position;
|
| 26 |
+
void main() { gl_Position = position; }
|
| 27 |
+
`;
|
| 28 |
+
|
| 29 |
+
// Fragment shader
|
| 30 |
+
const fragCode = `
|
| 31 |
+
precision mediump float;
|
| 32 |
+
uniform float iTime;
|
| 33 |
+
uniform vec2 iMouse;
|
| 34 |
+
uniform vec2 iResolution;
|
| 35 |
+
void main() {
|
| 36 |
+
vec2 uv = gl_FragCoord.xy / iResolution.xy;
|
| 37 |
+
vec3 color = vec3(
|
| 38 |
+
uv.x + iMouse.x*0.5 + 0.5*sin(iTime),
|
| 39 |
+
uv.y + iMouse.y*0.5 + 0.5*cos(iTime),
|
| 40 |
+
0.5 + 0.5*sin(iTime)
|
| 41 |
+
);
|
| 42 |
+
gl_FragColor = vec4(color, 1.0);
|
| 43 |
+
}
|
| 44 |
+
`;
|
| 45 |
+
|
| 46 |
+
// Shader compile helper
|
| 47 |
+
function compileShader(gl, source, type) {
|
| 48 |
+
const shader = gl.createShader(type);
|
| 49 |
+
gl.shaderSource(shader, source);
|
| 50 |
+
gl.compileShader(shader);
|
| 51 |
+
if(!gl.getShaderParameter(shader, gl.COMPILE_STATUS)){
|
| 52 |
+
console.error(gl.getShaderInfoLog(shader));
|
| 53 |
+
return null;
|
| 54 |
+
}
|
| 55 |
+
return shader;
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
// Compile shaders
|
| 59 |
+
const vertShader = compileShader(gl, vertCode, gl.VERTEX_SHADER);
|
| 60 |
+
const fragShader = compileShader(gl, fragCode, gl.FRAGMENT_SHADER);
|
| 61 |
+
|
| 62 |
+
// Link program
|
| 63 |
+
const program = gl.createProgram();
|
| 64 |
+
gl.attachShader(program, vertShader);
|
| 65 |
+
gl.attachShader(program, fragShader);
|
| 66 |
+
gl.linkProgram(program);
|
| 67 |
+
gl.useProgram(program);
|
| 68 |
+
|
| 69 |
+
// Fullscreen quad
|
| 70 |
+
const vertices = new Float32Array([-1,-1, 1,-1, -1,1, -1,1, 1,-1, 1,1]);
|
| 71 |
+
const buffer = gl.createBuffer();
|
| 72 |
+
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
|
| 73 |
+
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
|
| 74 |
+
const position = gl.getAttribLocation(program, "position");
|
| 75 |
+
gl.enableVertexAttribArray(position);
|
| 76 |
+
gl.vertexAttribPointer(position, 2, gl.FLOAT, false, 0, 0);
|
| 77 |
+
|
| 78 |
+
// Uniform locations
|
| 79 |
+
const iTime = gl.getUniformLocation(program, "iTime");
|
| 80 |
+
const iResolution = gl.getUniformLocation(program, "iResolution");
|
| 81 |
+
const iMouse = gl.getUniformLocation(program, "iMouse");
|
| 82 |
+
|
| 83 |
+
// Initial mouse
|
| 84 |
+
let mouseX = 0.5;
|
| 85 |
+
let mouseY = 0.5;
|
| 86 |
+
|
| 87 |
+
// Mouse / touch events
|
| 88 |
+
canvas.addEventListener('mousemove', e => {
|
| 89 |
+
const rect = canvas.getBoundingClientRect();
|
| 90 |
+
mouseX = (e.clientX - rect.left) / rect.width;
|
| 91 |
+
mouseY = 1.0 - (e.clientY - rect.top) / rect.height;
|
| 92 |
+
});
|
| 93 |
+
canvas.addEventListener('touchmove', e => {
|
| 94 |
+
e.preventDefault();
|
| 95 |
+
const rect = canvas.getBoundingClientRect();
|
| 96 |
+
const touch = e.touches[0];
|
| 97 |
+
mouseX = (touch.clientX - rect.left) / rect.width;
|
| 98 |
+
mouseY = 1.0 - (touch.clientY - rect.top) / rect.height;
|
| 99 |
+
}, {passive:false});
|
| 100 |
+
|
| 101 |
+
// Animation loop
|
| 102 |
+
function render() {
|
| 103 |
+
gl.viewport(0, 0, canvas.width, canvas.height);
|
| 104 |
+
gl.uniform2f(iResolution, canvas.width, canvas.height);
|
| 105 |
+
gl.uniform2f(iMouse, mouseX, mouseY);
|
| 106 |
+
gl.uniform1f(iTime, performance.now() / 1000.0);
|
| 107 |
+
gl.drawArrays(gl.TRIANGLES, 0, 6);
|
| 108 |
+
requestAnimationFrame(render);
|
| 109 |
+
}
|
| 110 |
+
render();
|
| 111 |
+
</script>
|
| 112 |
+
"""
|
| 113 |
+
|
| 114 |
+
components.html(html_code, height=400)
|
| 115 |
+
st.markdown("**Instructions:** Move your mouse (desktop) or touch (mobile) the canvas to change colors in real time.")
|