Creative Coding - Examples
Creative Coding - Examples
Creative Coding - Examples
let x = 25;
let y = 20
function setup() {
createCanvas(720, 400);
colorMode(HSB);
textSize(40);
// No animation to start
noLoop();
function draw() {
background(0);
text('sapna',20,50)
// Increase the x variable by 5
x += 5;
y +=5;
// Reset the circle position after it moves off the right side
x = -25;
y=-25;
function mousePressed() {
if (isLooping()) {
noLoop();
} else {
loop();
function keyPressed() {
redraw();
}
Moving circle in LOOP
function draw() {
createCanvas(1000, 1000);
background(0);
circle(x, y, 40);
fill(150);
fill(60,90,90);
fill(100,90,50);
Text on arc
function setup() {
createCanvas(100, 100);
background(200);
textSize(20);
text('SAPNA',15, 50);
// Use degrees.
angleMode(DEGREES);
fill(0, 0, 255);
describe('The bottom half of a circle drawn on a gray background. The bottom-right quarter is red.
The bottom-left quarter is blue.');
CIRCLE IN LOOP
function setup() {
createCanvas(100, 100);
describe(
'A red circle and a blue circle oscillate from left to right on a gray background. The circles drift
apart, then meet in the middle, over and over again.'
);
function draw() {
background(200);
translate(50, 50);
noStroke();
fill(255, 0, 0);
circle(x1, 0, 20);
fill(0, 0, 255);
circle(x2, 0, 20);
******************
Clock
let secondsRadius;
let minutesRadius;
let hoursRadius;
let clockDiameter;
function setup() {
createCanvas(710, 400);
stroke(255);
angleMode(DEGREES);
function draw() {
background(230);
noStroke();
stroke(255);
// Second hand
push();
rotate(secondAngle);
strokeWeight(1);
line(0, 0, 0, -secondsRadius);
pop();
// Minute hand
push();
strokeWeight(2);
rotate(minuteAngle);
line(0, 0, 0, -minutesRadius);
pop();
// Hour hand
push();
strokeWeight(4);
rotate(hourAngle);
line(0, 0, 0, -hoursRadius);
pop();
push();
strokeWeight(2);
point(0, -secondsRadius);
rotate(6);
pop();
Snowflakes
This example demonstrates the use of a particle system to simulate the motion of falling snowflakes.
This program defines a snowflake class and uses an array to hold the snowflake objects.
createCanvas(400, 600);
angleMode(DEGREES);
snowflakes.push(new Snowflake());
function draw() {
background(0);
flake.update(currentTime);
flake.display();
constructor() {
this.posX = 0;
update(time) {
this.posY += ySpeed;
this.posY = -50;
display() {
fill(this.color);
noStroke();
while
circle moving
function setup() {
createCanvas(100, 100);
frameRate(5);
describe(
"A gray square with several concentric circles at the center. The circles' sizes decrease at random
increments."
);
function draw() {
background(200);
let d = 100;
let minSize = 5;
d -= random(5, 15);
}
Moving TEXT
let frog1;
let frog2;
function setup() {
createCanvas(100, 100);
frameRate(1);
describe('Two frog faces on a gray background. The frogs hop around randomly.');
function draw() {
background('cornflowerblue');
frog1.show();
frog2.show();
frog2.hop();
frog1.checkEdges();
frog2.checkEdges();
class Frog {
constructor(x, y, size) {
this.x = x;
this.y = y;
this.size = size;
show() {
textAlign(CENTER, CENTER);
textSize(this.size);
hop() {
checkEdges() {
Moving Text_2
function setup() {
createCanvas(100, 100);
frogs.push(frog);
}
// Slow the frame rate.
frameRate(1);
describe(
'Five frog faces on a gray background. The frogs hop around randomly.'
);
function draw() {
background('cornflowerblue');
frog.show();
frog.hop();
frog.checkEdges();
class Frog {
constructor(x, y, size) {
this.x = x;
this.y = y;
this.size = size;
}
show() {
textAlign(CENTER, CENTER);
textSize(this.size);
hop() {
checkEdges() {
Number
Moving Balls
function setup() {
createCanvas(100, 100);
describe('A white circle travels from left to right on a gray background.');
function draw() {
background(200);
Object
animation
// Declare the variable data and assign it an object with three properties.
data.color = 'deeppink';
data1.color = 'rgb(255,203,20)';
function setup() {
createCanvas(100, 100);
function draw() {
background(200);
fill(data1.color);
Random Poetry
Using the floor() and random() functions, you can randomly select a series of items from an array and
draw them at different sizes and positions on the canvas.
'of', 'a', 'sketch', 'p5.js', 'has', 'a', 'full', 'set', 'of', 'drawing',
// which will be used to randomly start the word selection in the array.
let hue;
let position;
function setup() {
describe(
'A random series of words related to p5.js scattered onto the canvas.'
);
// Import the selected font style defined in the canvas's style.css file.
textFont('Space Mono');
createCanvas(720, 400);
// Set the text alignment to center and set the color mode to HSB.
textAlign(CENTER);
colorMode(HSB);
// array.
textSize(random(16, 48));