Published
Edited
Oct 26, 2021
1 star
Insert cell
Insert cell
{
const width = 600
const height = 600
const canvas = html`<canvas width=${width} height=${height} style = "border:1px black solid">`
const ctx = canvas.getContext("2d")

const props = {
ctx:ctx,
width:width,
height:height,
numOwls:100
}

const system = new System(props)
system.run()
return canvas
}
Insert cell
class Owl{
constructor(props){
this.ctx = props.ctx
this.x = props.x
this.y = props.y
this.rotate = props.rotate,
this.scale = props.scale
this.visible = props.visible
}

drawRightBrow(){
this.ctx.save()
this.ctx.rotate(-Math.PI/5)
this.ctx.strokeStyle = "white"
this.ctx.beginPath()
this.ctx.moveTo(0,0)
this.ctx.lineTo(100,0)
this.ctx.stroke()
this.ctx.closePath()
this.ctx.restore()
}

drawRightEye(){
this.ctx.save()
this.ctx.rotate(-Math.PI/5)
this.ctx.translate(40,0)
this.ctx.fillStyle = "gold"
this.ctx.beginPath()
this.ctx.arc(0,0,20,0,Math.PI)
this.ctx.stroke()
this.ctx.fill()
this.ctx.closePath()

this.ctx.fillStyle = "red"
this.ctx.beginPath()
this.ctx.arc(0,0,10,0,Math.PI)
this.ctx.stroke()
this.ctx.fill()
this.ctx.closePath()
this.ctx.restore()
}

drawLeftBrow(){
this.ctx.save()
this.ctx.rotate(Math.PI/5)
this.ctx.strokeStyle = "white"
this.ctx.beginPath()
this.ctx.moveTo(0,0)
this.ctx.lineTo(-100,0)
this.ctx.stroke()
this.ctx.closePath()
this.ctx.restore()
}

drawLeftEye(){
this.ctx.save()
this.ctx.rotate(Math.PI/5)
this.ctx.translate(-40,0)
this.ctx.fillStyle = "gold"
this.ctx.beginPath()
this.ctx.arc(0,0,20,0,Math.PI)
this.ctx.stroke()
this.ctx.fill()
this.ctx.closePath()

this.ctx.fillStyle = "red"
this.ctx.beginPath()
this.ctx.arc(0,0,10,0,Math.PI)
this.ctx.stroke()
this.ctx.fill()
this.ctx.closePath()
this.ctx.restore()
}
draw(){
if(this.visible){
this.ctx.save()
this.ctx.translate(this.x,this.y)
this.ctx.rotate(this.rotate)
this.ctx.scale(this.scale,this.scale)
this.drawRightBrow()
this.drawLeftBrow()
this.drawRightEye()
this.drawLeftEye()
this.ctx.restore()
}
}

switchVisibility(){
if(this.visible){
this.visible = false
}else{
this.visible = true
}
}
}
Insert cell
class System{
constructor(props){
this.ctx = props.ctx
this.width = props.width
this.height = props.height
this.numOwls = props.numOwls

this.parliament = this.makeParliament(this.numOwls)
}

tick(){
this.ctx.fillRect(0,0,this.width,this.height)
this.parliament.map(owl => {
owl.draw()
const rvar = d3.randomUniform(0,1)()
if(owl.visible && rvar < .01){
owl.switchVisibility()
}
else if(!owl.visible && rvar < .001){
owl.switchVisibility()
}
})
window.requestAnimationFrame(this.tick.bind(this))
}

run(){
window.requestAnimationFrame(this.tick.bind(this))
}
makeParliament(n){
const parliament = []
for(let i = 0; i < n; i = i + 1){
const props = {
ctx:this.ctx,
x:d3.randomUniform(0,this.width)(),
y:d3.randomUniform(0,this.height)(),
scale:d3.randomUniform(0,.7)(),
rotate:d3.randomUniform(-Math.PI/5,Math.PI/5)(),
visible:Math.floor(d3.randomUniform(0,1.1)())
}
parliament.push(new Owl(props))
}
return parliament
}
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more