p5.js is a version of Processing built natively in JavaScript. It’s really, really awesome.

Sean Kross wrote R bindings for p5.js in his p5 package so it can be written and R and published as an htmlwidget. This is a little exploration of how it works.

library(p5)

setup_ <- setup() %>% 
    createCanvas(500, 500) %>% 
    noStroke()

draw_ <- draw() %>% 
    background('#888') %>% 
    fill('rgba(255, 255, 255, 0.25)') %>% 
    ellipse(~mouseX, ~height / 2, ~mouseY, ~mouseY) %>% 
    fill('rgba(0, 0, 0, 0.5)') %>% 
    ellipse(~width - mouseX, ~height / 2, ~width - mouseY, ~width - mouseY)

sketch(setup = setup_, draw = draw_)