Published
Edited
Jan 15, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function CircleFactory(x = 100, y = 100, radius = 20) {
var randomDirection = function() { return Math.random() > 0.5 ? 1 : -1 }
var randomVariation = function(max) { return Math.random() * max }
var MAX_VELOCITY_X = 1;
var MAX_VELOCITY_Y = 1;
var color = randomColor(),
velocityY = MAX_VELOCITY_X - randomVariation(3),
velocityX = MAX_VELOCITY_Y - randomVariation(3),
directionX = randomDirection(),
directionY = randomDirection();
return {
getX: function() {
return x
},
getY: function() {
return y
},
radius,
color,
update: function() {
x += ( velocityX * directionX )
y += ( velocityY * directionY )
if(y > height || y < 0) {
directionY *= -1
}
if(x > width || x < 0) {
directionX *= -1
}
},
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more