20230610
p5.js
Generative Art
View Source Code
function setup() {
createCanvas(windowWidth, windowHeight);
angleMode(DEGREES);
}
function draw() {
background(255);
drawHorizontal()
drawVertical()
drawCos();
drawSin();
// drawCenterPoint();
// drawCosSinPoint();
// drawTriangle();
}
function drawHorizontal() {
push();
stroke(0);
line(0, height / 2, width, height / 2);
pop();
}
function drawVertical() {
push();
stroke(0);
line(width / 2, 0, width / 2, height);
pop();
}
function drawCos() {
push();
let degree = frameCount;
for (let x = 0; x < width; x++) {
let y = cos(x + degree) * 100;
stroke(255, 0, 0, 128);
fill(255, 0, 0, 128);
circle(x, height / 2 - y, 10);
}
pop();
}
function drawSin() {
push();
let degree = frameCount;
for (let x = 0; x < width; x++) {
let y = sin(x + degree) * 100;
stroke(0, 255, 0, 128);
fill(0, 255, 0, 128);
circle(x, height / 2 - y, 10);
}
pop();
}
function drawCenterPoint() {
push();
translate(width / 2, height / 2);
let x = 0;
let y = 0;
stroke(0, 0, 0, 128);
fill(0, 0, 0, 128);
circle(x, y, 10);
pop();
}
function drawCosSinPoint() {
push();
translate(width / 2, height / 2);
let degree = frameCount;
let x = cos(degree) * 100;
let y = -sin(degree) * 100;
stroke(0, 0, 255, 128);
fill(0, 0, 255, 128);
circle(x, y, 10);
pop();
}
function drawTriangle() {
push();
translate(width / 2, height / 2);
let degree = frameCount % 360;
// text
noStroke();
fill(0);
textAlign(CENTER);
text("degrees:" + degree, 0, height / 4);
// triangle
let x = cos(degree) * 100;
let y = -sin(degree) * 100;
stroke(255, 128, 128, 128);
fill(255, 128, 128, 128);
triangle(0, 0, x, y, x, 0);
pop();
}