I’ve converted your “Animated Random Boxes” sketch to PyScript!
Although I’ve done some tweaks to it before actual conversion:
“Animated_Random_Boxes.pyde”:
"""
Animated Random Boxes
by SalvaLnx (2024-Jan)
mod GoToLoop (2024-Jan-18)
Discourse.Processing.org/t/animated-random-boxes-in-p5-js-python/43612/10
PyScript.com/@gotoloop/animated-random-boxes
"""
BG = 0xffBFF2C5
ROWS, COLS = range(5), range(6)
def setup():
size(600, 500)
frameRate(5)
strokeWeight(.01 * width)
global w, h, ix, iy, wgap, hgap, off2, woff, hoff, weight
w = .10 * width
h = .10 * height
ix = .13 * width
iy = .17 * height
gap = .03 * width
wgap = w + gap
hgap = h + gap
off = .03 * width
off2 = .5 * off
woff = w - off
hoff = h - off
weight = .006 * width
def draw():
background(BG)
for r in ROWS:
y = hgap * r + iy
for c in COLS:
x = wgap * c + ix
y = hgap * r + iy
rect(x, y, w, h)
if random(25) > 10:
pushStyle()
strokeWeight(weight)
rect(x + off2, y + off2, woff, hoff)
popStyle()
So here’s the Python Mode “.pyde” above converted to PyScript + Pjs ".
You can check it out online from these links:
“pjs.py”:
"""
Animated Random Boxes
by SalvaLnx (2024-Jan)
mod GoToLoop (2024-Jan-18)
Discourse.Processing.org/t/animated-random-boxes-in-p5-js-python/43612/10
PyScript.com/@gotoloop/animated-random-boxes
"""
from js import Processing, document
from types import MethodType
BG = 0xffBFF2C5
ROWS, COLS = range(5), range(6)
def setup(p):
p.size(600, 500)
p.frameRate(5)
p.strokeWeight(.01 * p.width)
global w, h, ix, iy, wgap, hgap, off2, woff, hoff, weight
w = .10 * p.width
h = .10 * p.height
ix = .13 * p.width
iy = .17 * p.height
gap = .03 * p.width
wgap = w + gap
hgap = h + gap
off = .03 * p.width
off2 = .5 * off
woff = w - off
hoff = h - off
weight = .006 * p.width
def draw(p):
p.background(BG)
for r in ROWS:
y = hgap * r + iy
for c in COLS:
x = wgap * c + ix
p.rect(x, y, w, h)
if p.random(25) > 10:
p.pushStyle()
p.strokeWeight(weight)
p.rect(x + off2, y + off2, woff, hoff)
p.popStyle() # Not restoring previous strokeWeight!
def sketch(p):
p.setup = MethodType(setup, p)
p.draw = MethodType(draw, p)
def instantiatePjs():
div = document.getElementById('pjs')
canvas = document.createElement('canvas')
div.appendChild(canvas)
Processing.new(canvas, sketch)
__name__ == '__main__' and instantiatePjs()
And even a PyScript + p5*js too:
“p5js.py”:
"""
Animated Random Boxes
by SalvaLnx (2024-Jan)
mod GoToLoop (2024-Jan-18)
Discourse.Processing.org/t/animated-random-boxes-in-p5-js-python/43612/10
PyScript.com/@gotoloop/animated-random-boxes
"""
from js import p5
from types import MethodType
ROWS, COLS = range(5), range(6)
def setup(p):
p.createCanvas(600, 500)
p.frameRate(5)
p.strokeWeight(.01 * p.width)
global w, h, ix, iy, wgap, hgap, off2, woff, hoff, weight, bg
w = .10 * p.width
h = .10 * p.height
ix = .13 * p.width
iy = .17 * p.height
gap = .03 * p.width
wgap = w + gap
hgap = h + gap
off = .03 * p.width
off2 = .5 * off
woff = w - off
hoff = h - off
weight = .006 * p.width
bg = p.color('#BFF2C5')
def draw(p, _CHANCE = .7):
p.background(bg)
for r in ROWS:
y = hgap * r + iy
for c in COLS:
x = wgap * c + ix
p.rect(x, y, w, h)
if p.random() > _CHANCE:
p.push()
p.strokeWeight(weight)
p.rect(x + off2, y + off2, woff, hoff)
p.pop()
def sketch(p):
p.setup = MethodType(setup, p)
p.draw = MethodType(draw, p)
def instantiateP5js(): p5.new(sketch, 'p5js')
__name__ == '__main__' and instantiateP5js()
And the “index.html” which loads both of them:
“index.html”:
<!DOCTYPE html>
<meta charset=utf-8>
<title>Animated Random Boxes</title>
<link rel=icon href=//PyScript.net/assets/images/favicon.ico>
<script defer src=//Unpkg.com/processing-js></script>
<script defer src=//cdn.JsDelivr.net/npm/p5></script>
<script type=module src=//Unpkg.com/@pyscript/core></script>
<py-config src=pyscript.toml></py-config>
<py-script src=pjs.py></py-script>
<py-script src=pjs.py></py-script>
<py-script src=p5js.py></py-script>
<py-script src=p5js.py></py-script>
<h3>Animated Random Boxes [pjs]</h3>
<div id=pjs></div>
<h3>Animated Random Boxes [p5js]</h3>
<div id=p5js></div>
And although not required in this sketch project, the TOML config file:
“pyscript.toml”:
name = 'Animated Random Boxes'
version = '1.0.3'
author_name = 'SalvaLnx & GoToLoop'
license = 'Unlicense'
description = 'Discourse.Processing.org/t/animated-random-boxes-in-p5-js-python/43612/10'
url = 'https://PyScript.com/@gotoloop/animated-random-boxes'
I believe by studying a Python Mode sketch you’ve made and comparing it to its corresponding PyScript versions will help you understand what are the necessary steps for it.