Spaces:
Sleeping
Sleeping
import streamlit as st | |
def create_animation_app(): | |
st.title("Animated Pattern") | |
# JavaScript code for the animation | |
js_code = """ | |
<canvas id="animationCanvas"></canvas> | |
<script> | |
const canvas = document.getElementById('animationCanvas'); | |
const ctx = canvas.getContext('2d'); | |
// Set canvas size | |
canvas.width = 400; | |
canvas.height = 400; | |
let t = 0; | |
function mag(x, y) { | |
return Math.sqrt(x * x + y * y); | |
} | |
function a(x, y) { | |
const k = x/8 - 25; | |
const e = y/8 - 25; | |
const d = mag(k, e)**2 / 99; | |
const q = x/3 + k * 0.5 / Math.cos(y*5) * Math.sin(d*d - t); | |
const c = d/2 - t/8; | |
const xPos = q * Math.sin(c) + e * Math.sin(d + k - t) + 200; | |
const yPos = (q + y/8 + d*9) * Math.cos(c) + 200; | |
return [xPos, yPos]; | |
} | |
function draw() { | |
ctx.fillStyle = 'rgba(6, 6, 6, 0.3)'; | |
ctx.fillRect(0, 0, canvas.width, canvas.height); | |
ctx.strokeStyle = 'rgba(255, 255, 255, 0.376)'; | |
for(let y = 99; y < 300; y += 5) { | |
for(let x = 99; x < 300; x++) { | |
const [px, py] = a(x, y); | |
ctx.beginPath(); | |
ctx.moveTo(px, py); | |
ctx.lineTo(px, py); | |
ctx.stroke(); | |
} | |
} | |
t += Math.PI / 60; | |
requestAnimationFrame(draw); | |
} | |
// Start the animation | |
draw(); | |
</script> | |
""" | |
# CSS to center the canvas | |
css = """ | |
<style> | |
canvas { | |
display: block; | |
margin: auto; | |
background: #000; | |
} | |
</style> | |
""" | |
# Combine CSS and JavaScript | |
html_content = css + js_code | |
# Display using st.components.v1.html | |
st.components.v1.html(html_content, height=450) | |
if __name__ == "__main__": | |
create_animation_app() |