Published
Edited
Dec 2, 2019
Insert cell
Insert cell
Insert cell
Insert cell
viewof scale = slider({min: 5, max: 10, value: 5, step: 1, title: "ball size"});
Insert cell
viewof speed = slider({min: 1, max: 10, value: 1, step: 1, title: "ball speed"});
Insert cell
viewof canvas = {
const c = DOM.context2d(w, h);
c.canvas.value = c;
return c.canvas;
}
Insert cell
mutable ball = ({x: 2, y: 2, r: scale, xSpeed: speed, ySpeed: speed})
Insert cell
ball
Insert cell
viewof b = button({value: "reverse", description: "reverse the ball values"})
Insert cell
b
Insert cell
reverse = {
b;
return !this;
}
Insert cell
gameloop = {

let { x, y, r, xSpeed, ySpeed } = mutable ball;
while (true) {

if (!reverse) {
x = x + xSpeed;
y = y + ySpeed;
// r = r + 1
} else {
x = Math.max(0, x - xSpeed);
y = Math.max(0, y - ySpeed);
// r = Math.max(0, r - 1);
}
mutable ball = {x, y, r} //, xSpeed, ySpeed}
// return mutable ball
yield false
}


}
Insert cell
gameloop
Insert cell
drawing = {
gameloop
const {x, y, r} = mutable ball;
// let {x, y, r} = ball;
canvas.fillStyle = "purple";
canvas.fillRect(0, 0, w, h);
canvas.beginPath();
canvas.fillStyle = "red";
canvas.arc(x, y, r, 0, 2 * Math.PI);
canvas.fill();
}
Insert cell
import { slider, videoyt, button} from "@embracelife/tutorial-utilities"
Insert cell
FileAttachment("visualizer.png").image()
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