xxxxxxxxxx
120
let useText = false;
let useTextOnly = false;
// let frames = 0;
let x, y;
let px = 0;
let py = 0;
let UNIT = -1;
let index = 1;
const dim = 50;
// 2500 frames
let step = 0;
let stepsWoutRotation = 0;
let rotation = 0;
function setup() {
createCanvas(367*2, 367*2);
fullscreen()
background(0);
x = width/2; y = height/2; // Initialize at center
UNIT = (width/dim) * 1;
textAlign(CENTER, CENTER);
rectMode(CENTER);
strokeWeight(UNIT/8)
textSize(UNIT/2);
px = x;
py = y;
const options = {
units: "frames",
delay: 0
}
// saveGif("noText.gif", 2500, options)
// frameRate(2)
}
function draw() {
// Draw
if (isPrime(index)) {
if (useText) {
stroke(0,255,0)
fill(0,255,0)
rect(x, y, UNIT, UNIT)
} else {
fill(255)
ellipse(x, y, UNIT/2, UNIT/2)
}
}
// frames++;
stroke(255)
if (useText) {
if (!useTextOnly)
line(px, py, x, y)
noStroke()
fill(255)
text(index, x, y);
} else {
line(px, py, x, y)
}
// Update
px = x;
py = y;
index++;
if (rotation == 0)
x += UNIT;
else if (rotation == 1)
y -= UNIT;
else if (rotation == 2)
x -= UNIT;
else if (rotation == 3)
y += UNIT
if (rotation > 3) {
rotation = 0;
x += UNIT;
}
// Rotate
if (stepsWoutRotation >= floor(step)) {
stepsWoutRotation = 0;
rotation++;
step += 0.5;
if (index > dim*dim) {
noLoop()
}
} else {
stepsWoutRotation++;
}
// console.log(frames)
}
function isPrime(num) {
// Make it faster (because no even number except 2 is prime)
if (num != 2 && num % 2 == 0) return false
for (let i = 2; i <= sqrt(num); i++) {
if (num % i == 0 && i != num) {
return false;
}
}
return true;
}