Animasi Bola dengan PyScript
import js
import asyncio
canvas = js.document.getElementById("kanvas")
ctx = canvas.getContext("2d")
x = 50
y = 50
vx = 2
vy = 1.5
radius = 20
async def animate():
global x, y, vx, vy
while True:
# Hapus layar
ctx.clearRect(0, 0, canvas.width, canvas.height)
# Gambar bola
ctx.beginPath()
ctx.arc(x, y, radius, 0, 2 * js.Math.PI)
ctx.fillStyle = "red"
ctx.fill()
ctx.closePath()
# Perbarui posisi
x += vx
y += vy
# Pantul dari tepi
if x + radius > canvas.width or x - radius < 0:
vx = -vx
if y + radius > canvas.height or y - radius < 0:
vy = -vy
await asyncio.sleep(0.01)
await animate()