Spaces:
Sleeping
Sleeping
import streamlit as st | |
def create_animation_app(): | |
st.title("Animated Spiral Creature") | |
# 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 getColor(x, y, t) { | |
// Create shifting colors based on position and time | |
const hue = (Math.sin(t/2) * 360 + x/3 + y/3) % 360; | |
const saturation = 70 + Math.sin(t) * 30; | |
const lightness = 50 + Math.cos(t/2) * 20; | |
return `hsla(${hue}, ${saturation}%, ${lightness}%, 0.5)`; | |
} | |
function draw() { | |
// Clear canvas with a fade effect | |
ctx.fillStyle = 'rgba(0, 0, 0, 0.1)'; | |
ctx.fillRect(0, 0, canvas.width, canvas.height); | |
ctx.lineWidth = 1.5; | |
for(let y = 99; y < 300; y += 4) { | |
for(let x = 99; x < 300; x += 2) { | |
const [px, py] = a(x, y); | |
const color = getColor(x, y, t); | |
ctx.strokeStyle = color; | |
ctx.beginPath(); | |
ctx.moveTo(px, py); | |
ctx.lineTo(px + 1, py + 1); // Make points slightly larger | |
ctx.stroke(); | |
} | |
} | |
t += Math.PI / 120; // Slowed down the animation slightly | |
requestAnimationFrame(draw); | |
} | |
// Ensure canvas is cleared on start | |
ctx.fillStyle = 'black'; | |
ctx.fillRect(0, 0, canvas.width, canvas.height); | |
// Start the animation | |
draw(); | |
</script> | |
""" | |
# CSS to center the canvas and ensure proper display | |
css = """ | |
<style> | |
canvas { | |
display: block; | |
margin: auto; | |
background: black; | |
} | |
.stApp { | |
background-color: black; | |
} | |
</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() |