Published
Edited
May 28, 2018
1 fork
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
canvas.canvas
Insert cell
Insert cell
circle = function circle
(context, x, y, radius) {
var c = context;
c.beginPath();
c.arc(x, y, radius, 0, Math.PI*2, true);
c.closePath();
}
Insert cell
regularPolygon = function regularPolygon
(context, x, y, radius, angle, sides) {
var c = context;
var innerAngle = (2.0 * Math.PI) / sides;
c.save();
c.beginPath();
c.translate(x, y);
c.rotate(angle);
c.scale(radius, radius);
c.moveTo(1,0);
for (var i=0; i < sides; i++) {
c.rotate(innerAngle);
c.lineTo(1,0);
};
c.closePath();
c.restore();
}
Insert cell
regularQuadraticStar = function regularQuadraticStar
(context, x, y, radius, angle, sides) {
var c = context;
var innerAngle = (2.0 * Math.PI) / sides;
c.save();
c.beginPath();
c.translate(x, y);
c.rotate(angle);
c.scale(radius, radius);
c.moveTo(1,0);
for (var i=0; i < sides; i++) {
c.rotate(innerAngle);
c.quadraticCurveTo(0, 0, 1, 0);
};
c.closePath();
c.restore();
}
Insert cell
regularStar = function regularStar
(context, x, y, radius, angle, sides, pointiness) {
var c = context;
var halfInnerAngle = (Math.PI) / sides
c.save();
c.beginPath();
c.translate(x, y);
c.rotate(angle);
c.scale(radius, radius);
c.moveTo(1,0);
for (var i=0; i < sides; i++) {
c.rotate(halfInnerAngle);
c.lineTo(1 - pointiness, 0);
c.rotate(halfInnerAngle);
c.lineTo(1,0);
};
c.closePath();
c.restore();
}
Insert cell
uniformRandom = function uniformRandom
(low, high) {
if (typeof(high) === 'undefined') {
high = low;
low = 0;
}
return low + (high - low) * Math.random();
}
Insert cell
randomInt = function randomInt
(low, high) {
return Math.floor(uniformRandom(low, high));
}
Insert cell
randomChoice = function randomChoice
(array) {
var choice = Math.floor(Math.random() * array.length);
return array[choice];
}
Insert cell
random_color = function randomColor() {
var r = randomInt(30, 256);
var g = randomInt(30, 256);
var b = randomInt(30, 256);
var a = uniformRandom(0.2, 1.0);
return 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';
}
Insert cell
randomShape = function randomShape
(context, width, height, maxradius) {
var c = context;
c.save()

var minradius = 0.1 * maxradius;
var minstroke = 0.5;
var maxstroke = 5.0;

var x = uniformRandom(0, width);
var y = uniformRandom(0, height);
var radius = uniformRandom(minradius, maxradius);
var angle = uniformRandom(0.0, 2.0 * Math.PI);
var sides = randomInt(3,9);
var pointiness = uniformRandom(0.3, 0.9);

var shape = randomChoice(
[circle, regularPolygon, regularQuadraticStar, regularStar]);

c.fillStyle = random_color();
c.strokeStyle = random_color();
c.lineJoin = randomChoice(['round', 'bevel', 'miter']);
c.miterLimit = 10;
c.lineWidth = uniformRandom(minstroke, maxstroke);

// actually draw the shape
shape(c, x, y, radius, angle, sides, pointiness);
c.fill();
c.stroke();

c.restore()
}
Insert cell
fillCanvas = function fillCanvas (context, color) {
var c = context;
c.save();
c.fillStyle = color;
c.fillRect(0, 0, c.canvas.width, c.canvas.height);
c.restore();
}
Insert cell
canvas = {
var canvaswidth = width;
var canvasheight = 400;
var c = DOM.context2d(canvaswidth, canvasheight);

while (true) {
fillCanvas(c, black_background ? 'rgb(0, 0, 0)' : 'rgb(255, 255, 255)')
for (var i=0; i < nshapes; i++) {
randomShape(c, canvaswidth, canvasheight, 30);
yield c;
};
// After drawing n shapes, wait 3 seconds and start over.
yield Promises.delay(3000, c)
}
}
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