Skip to content

Instantly share code, notes, and snippets.

@etra0
Last active October 4, 2017 19:03
Show Gist options
  • Save etra0/4526ff6889ecac93c0890cd3682c6952 to your computer and use it in GitHub Desktop.
Save etra0/4526ff6889ecac93c0890cd3682c6952 to your computer and use it in GitHub Desktop.
Multiples spirographs made in p5.js
license: gpl-3.0
height: 700
scrolling: no
border: yes
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.14/p5.min.js"></script>
<script src="sketch.js"></script>
<button type="submit" onclick='generate()'>Generate other</button>
<button type="submit" onclick="pause()">Continue/Pause</button>
<style> body {padding: 0; margin: 0;} </style>
</head>
<body>
</body>
</html>
let width = 800,
height = 600;
var p = false;
function pause() {
p = !p;
}
function Drawer(R, r, p) {
var x = 0,
y = 0,
orig_x = 0,
orig_y = 0,
l = p/r,
k = r/R;
var alpha = 0;
var divisor = 10;
this.line = function (i) {
x = width/2;
y = height/2;
x += scale*R*((1-k)*Math.cos(i) + l*k*cos(i*((1-k)/k)));
y += scale*R*((1-k)*Math.sin(i) + l*k*sin(i*((1-k)/k)));
stroke(cos(i)*255/2 + 255/2,
sin(i)*255/2 + 255/2,
(sin(i)+cos(i))*255/2 + 128, alpha);
line(orig_x, orig_y, x, y);
noStroke();
alpha = 255;
orig_x = x;
orig_y = y;
return;
}
return this;
}
let scale = 2.5;
let i = 0;
var drawers = []
var x, y, z;
function generate() {
background(255);
drawers = [];
i = 0;
for(var _ = 0; _ < Math.floor(Math.random()*10)+1; _++) {
x = Math.floor(Math.random()*100);
y = Math.floor(Math.random()*100);
z = Math.floor(Math.random()*100);
console.log("drawer " + _, x, y, z);
drawers.push(new Drawer(x, y, z));
}
}
function setup() {
createCanvas(width, height);
cos = Math.cos;
sin = Math.sin;
stroke(255);
fill(255);
frameRate(30);
generate();
}
function draw() {
//background(cos(i)*255/2 + 255/2,
// sin(i)*255/2 + 255/2,
// (sin(i)+cos(i))*255/2 + 128);
if (!p) {
for (j = i; j < 50 + i; j += 0.1) {
drawers.forEach(function(dr) {
dr.line(j);
});
}
i = j;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment