In [ ]:
from time import sleep, time
from random import randint, weibullvariate, choice

import numpy as np

from ipycanvas import Canvas, hold_canvas
In [ ]:
n_particles = 100_000

x = np.array(np.random.rayleigh(250, n_particles), dtype=np.int32)
y = np.array(np.random.rayleigh(250, n_particles), dtype=np.int32)
size = np.random.randint(1, 3, n_particles)
In [ ]:
canvas = Canvas(width=800, height=500)
canvas
In [ ]:
canvas.fill_rects(x, y, size)

Random color particles

In [ ]:
canvas = Canvas(width=800, height=500)
canvas
In [ ]:
with hold_canvas(canvas):
    canvas.fill_style = 'red'
    canvas.fill_rects(x[:30_000], y[:30_000], size)

    canvas.fill_style = 'orange'
    canvas.fill_rects(x[30_001:60_000], y[30_001:60_000], size)

    canvas.fill_style = 'yellow'
    canvas.fill_rects(x[60_001:], y[60_001:], size)

Stroked particles

In [ ]:
n_particles = 5_000

x = np.array(np.random.rayleigh(250, n_particles), dtype=np.int32)
y = np.array(np.random.rayleigh(250, n_particles), dtype=np.int32)
size = np.random.randint(5, 10, n_particles)

canvas = Canvas(width=800, height=500)

canvas.fill_style = '#c0dced'
canvas.stroke_style = '#3279a8'
canvas.line_width = 1.5

canvas.fill_rects(x, y, size)
canvas.stroke_rects(x, y, size)

canvas

Moving particles

In [ ]:
n = 2_000
In [ ]:
current_time = time()

def move_particles(x, speed_x, y, speed_y):
    global current_time

    elapsed_time = time() - current_time

    x += np.array(speed_x * elapsed_time, dtype=np.int32)
    y += np.array(speed_y * elapsed_time, dtype=np.int32)

    current_time = time()
In [ ]:
x = np.array(np.random.rayleigh(250, n_particles), dtype=np.int32)
y = np.array(np.random.rayleigh(250, n_particles), dtype=np.int32)
size = np.random.randint(1, 3, n_particles)
speed_x = np.random.randint(-40, 40, n_particles)
speed_y = np.random.randint(-40, 40, n_particles)
In [ ]:
canvas = Canvas(width=800, height=500)

display(canvas)

canvas.fill_style = 'green'

for _ in range(75):
    move_particles(x, speed_x, y, speed_y)

    with hold_canvas(canvas):
        canvas.clear()
        canvas.fill_rects(x, y, size)

    sleep(0.08)
In [ ]: