Published
Edited
Oct 5, 2021
Insert cell
Insert cell
{
const width = 300
const height = 300

const canvas = html`<canvas width = ${width} height = ${height} style = "border:1px solid black"/>`
const ctx = canvas.getContext("2d")

const segSize = 10
const stepSize = 2
const framesPerTick = segSize/stepSize

let x = 150
let y = 150
let theta = d3.randomUniform(0,2*Math.PI)()
let tick = 0
let frame = 0

function loop(){
ctx.beginPath()
ctx.moveTo(x,y)
x = x + stepSize*Math.cos(theta)
y = y + stepSize*Math.sin(theta)
ctx.lineTo(x,y)
ctx.stroke()
ctx.closePath()

if(frame%framesPerTick == 0){
tick = tick + 1
theta = d3.randomUniform(0,2*Math.PI)()
}

if(tick < 100){
frame = frame + 1
window.requestAnimationFrame(loop)
}
}

window.requestAnimationFrame(loop)

return canvas
}
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