Introduction:
New Year's Eve is to get rid of the annoying brain and welcome the new hope! Here I first wish you all a happy New Year's Eve, years and years often laugh, everything is as you wish!
Main article:
Creating a Canvas
setup
cap (a poem)draw
beThe two main functions in the
createCanvas
for creating the size of the canvas.background
to set the background color of the canvas
function setup() { createCanvas(1303 / 2, 734 / 2) } function draw() { background(50); }
Drawing firework particles
Consider that there will be a lot of them, through a functionParticle
to generate, the code is as follows
var firework; function Particle(x, y) { = createVector(x, y) = createVector(0, 0) = createVector(0, 0) = function () { () () (0) } = function () { point(, ) } } # Call () and () to display the fireworks particles out of the way function setup() { createCanvas(1303 / 2, 734 / 2) stroke(255) strokeWeight(4) firework = new Particle(200, 150) } function draw() { background(50); () () }
The results are as follows:
Make firework particles appear randomly at the bottom
modificationssetup
hit the nail on the headfirework
The first thing you need to do is to make it appear anywhere at the bottom of the page.
firework = new Particle(random(width), height)
Here.width
cap (a poem)height
The width and height of the canvas
The results are as follows
Make the firework particles move upward
Simply modify theParticle
hit the nail on the headcan immediately (do sth)
= createVector(0, -4)
createVector
in which the first parameter represents the rate of the x-axis, with positive being the rate to the right and negative being the rate to the left; the second parameter represents the rate of the y-axis, with negative being the rate up and positive being the rate down
The effect is as follows
Let the particles use the gravity effect, which allows downward motion
First declare a variable globallygravity
, setup gravity in the setup function
gravity = createVector(0, 0.2) (gravity) = function (force) { (force) }
The effect is as follows
Need a lot of firework particles
It is necessary to create aFirework
function (math.)
function Firework() { = new Particle(random(width), height) = function () { (gravity) () } = function () { (); } } # And then in the draw, a for loop to show a lot of firework particles function draw() { background(50) (new Firework()) for (var i = 0; i < ; i++) { fireworks[i].update() fireworks[i].show() } }
The results are as follows
Make the firework particles disappear when they reach their own apex.
function Firework() { = new Particle(random(width), height) = function () { if () { (gravity) () if ( >= 0) { = null } } } = function () { if () { (); } } }
The effect is as follows
The moment it disappeared, it made the surrounding blasting
There will be more modifications here, the main ones areFirework
οΌ
function Firework() { = new Particle(random(width), height, true) = false = [] = function () { if (!) { (gravity) () if ( >= 0) { = true () } } for (let i = 0; i < ; i++) { [i].applyForce(gravity) [i].update() } } = function () { for (let i = 0; i < 100; i++) { var p = new Particle(, ) (p) } } = function () { if (!) { (); } for (let i = 0; i < ; i++) { [i].show() } } }
The results are as follows
Stochastic multiplier bursts
Can be modifiedParticle
to refine the following effect above, the modified code is
function Particle(x, y, firework) { = createVector(x, y) = firework if () { = createVector(0, random(-12, -8)) } else { = .random2D() (random(1, 6)) } = createVector(0, 0) = function (force) { (force) } = function () { () () (0) } = function () { point(, ) } }
The effect is as follows:
Show less fireworks
This is accomplished by adjusting the odds to allow for fewer display fireworks
we willdraw
function in the
if(random(1)<0.1){ (new Firework()) }
Modified to:
if(random(1)<0.02){ (new Firework()) }
That's less.
And then we realized that the fireworks were too scattered, modify the fireworks to be too scattered
until (a time)Particle
In theupdate
method, add the
if(!){ (0.85) }
It can be understood thatmult
The greater the value of the force, the greater the dispersion of the explosion.
Fade-out effect implementation
After dispersing, it needs to slowly fade out and disappear.
In fact, the main introduction of a variablelifespan
Let's get it out of the255
Starts decreasing through thestroke(255,)
to realize the fade-out
following code
function Particle(x, y, firework) { = createVector(x, y) = firework = 255 if () { = createVector(0, random(-12, -8)) } else { = .random2D() (random(1, 6)) } = createVector(0, 0) = function (force) { (force) } = function () { if(!){ (0.85) -= 4 } () () (0) } = function () { if (!) { strokeWeight(2) stroke(255,) } else { strokeWeight(4) stroke(255) } point(, ) } }
The effect is as follows
Modify the background color
existsetup
hit the nail on the headbackground
function to change the background color to black
background(0)
at the same timedraw
increase
colorMode(RGB) background(0, 0, 0, 25)
colorMode
Used to set the color model, in addition to theRGB
And above that.HSB
οΌbackground
(used form a nominal expression)4
parameter is the parameter that corresponds to thergba
The effect is as follows
Add firework colors
Mainly add color to fireworks, you can add random color by random number, mainly in theFirework
Add it.
= random(255)
Ending:
Finally, good luck.
Happy New Year's Eve~ππππππ!
To this article on the most dazzling Python fireworks code full analysis of the article is introduced to this, more related Python brilliant fireworks content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!