-
Notifications
You must be signed in to change notification settings - Fork 0
/
snowfallRamayana.js
88 lines (77 loc) · 2.87 KB
/
snowfallRamayana.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// snowflakes is an array of snowflake objects
let snowflakes = [];
let img;
let xoff = 0;
let scaleFactor = 7/10; // Scale factor for the canvas size
// preload() is a special function in P5.js that runs once before setup()
function preload() {
// load the image (Ramayana.png) into the img variable
img = loadImage('./ramayana.png');
}
// setup() is a special function in P5.js that runs once after preload()
function setup() {
// create a canvas with the same size as the image, but scaled down by scaleFactor
createCanvas(img.width * scaleFactor, img.height * scaleFactor);
// disable drawing outlines around shapes
noStroke();
// set the background color to black
background("#000000");
// set the frame rate to 45 frames per second
frameRate(45);
}
// draw() is a special function in P5.js that runs continuously after setup()
function draw() {
// calculate the time since the last frame was drawn
let t = frameCount / 90;
// create a random number of snowflakes each frame
for (let i = 0; i < random(50); i++) {
// create a new snowflake object and add it to the snowflakes array
snowflakes.push(new snowflake());
}
// loop through snowflakes with a for..of loop
for (let flake of snowflakes) {
// update the position of the snowflake
flake.update(t);
// draw the snowflake
flake.display();
}
// increment xoff by 1.0 each frame
xoff += 1.0;
}
// snowflake is a constructor function that creates a new snowflake object
function snowflake() {
// set the initial position of the snowflake
this.posX = random(width);
this.posY = random(-50, 0);
// set the initial angle of the snowflake
this.initialangle = random(0, 2 * PI);
// set the size of the snowflake
this.size = random(1, 3) * scaleFactor;
// set the radius of the snowflake
this.radius = sqrt(random(pow(width / 2, 2)));
// update() is a method that updates the position of the snowflake
this.update = function(time) {
// angular speed
let w = 1.6;
// calculate the angle of the snowflake
let angle = w * time + this.initialangle;
// update the position of the snowflake
this.posX = width / 2 + this.radius * sin(angle);
this.posY += pow(this.size / scaleFactor, 0.5) * 2 * scaleFactor;
// if the snowflake has gone off the bottom of the screen, remove it from the array
if (this.posY > height) {
let index = snowflakes.indexOf(this);
snowflakes.splice(index, 1);
}
};
// display() is a method that draws the snowflake
this.display = function() {
// calculate the x and y coordinates of the snowflake on the original image
let imgX = this.posX / scaleFactor;
let imgY = this.posY / scaleFactor;
// get the color of the pixel at the calculated coordinates
fill(color(img.get(imgX, imgY)));
// draw an ellipse at the position of the snowflake
ellipse(this.posX, this.posY, this.size);
};
}