A tool for rendering different fireworks. Created special for celebrating new year 2021 on pikabu.ru.
This code creates one random explosion (without rocket and launching) every second:
<fw-canvas id="fw" width="800" height="600" bg-color="#000" bg-image="" sounds-url="./sounds"></fw-canvas>
<script src="fireworks.js"></script>
<script>
const fw = document.querySelector('#fw');
setInterval(() => {
if (document.hidden) {
// ignore if page is not visible
return;
}
const firework = fw.getRandomFirework({x: 400, y: 300});
// remove rocket, we need only explosion
firework.rocketType = '';
fw.addFirework(firework);
}, 1000);
</script>
Custom element <fw-canvas>
supports the following attributes:
fullscreen
- resizes canvas to whole viewport width and height;width
&height
- specifies fixed width and height for canvas;bg-color
- sets background color. By defaultrgba(11, 17, 34, 0.9)
;bg-image
- sets url to the background image. This image will be rendered at the bottom of canvas and with horizontal repeating;sounds-url
- sets url where sound files are.
<fw-canvas></fw-canvas>
<fw-canvas fullscreen></fw-canvas>
<fw-canvas bg-color="#500" bg-image="//cdn.mysite.com/img1.png"></fw-canvas>
<fw-canvas sounds-url="//cdn.mysite.com/sounds"></fw-canvas>
Custom element <fw-canvas>
supports the following methods and getters:
canvas
- getter. Returns instance of HTMLCanvasElement;addFirework(firework)
- adds a firework to render;removeFirework(firework)
- removes firework from rendering;removeAllFireworks()
- removes all fireworks;launchFirework(firework)
- launch firework with a rocket;render()
- force render;getRandomFirework(position, autoLaunch)
- generates and returnsfirework
random object;
This example creates firework with 3 colors of explosion comb1
and rocket type1
with 1 color.
Rocket automatically launched. No sound.
const fw = document.querySelector('fw-canvas');
fw.addFirework({
position: {x: 400, y: 560},
colors: ['#fa5252', '#6db4ff', '#8cff41'],
rocketColors: ['#ac63c6'],
angleFactor: 0.5,
sizeFactor: 0.8,
elementsFactor: 0.9,
rocketType: 'type1',
explosionType: 'comb1',
canBeLaunched: true,
autoLaunch: true,
rocketFading: false,
noSound: true,
});
The firework object contains the following properties:
position: {x: number, y: number}
- Required. Start position of the rocket or explosion;colors: Array<string>
- Optional. CSS colors for an explosion. If not specified the random colors will be used;rocketColors: Array<string>
- Optional. CSS colors for a rocket. If not specified the random colors will be used;angleFactor: number
- Optional. Value from 0 to 1. Specifies start angle of the rocket;sizeFactor: number
- Optional. Value from 0 to 1. Specifies size of the rocket and explosion;elementsFactor: number
- Optional. Value from 0 to 1. Specifies count of lights in the explosion;rocketType: string
- Optional. Possible values: '', 'type1', 'type1';explosionType: string
- Optional. Possible values: '', 'type0', 'type1-circle', 'type1-sphere', 'type1-disk', 'type2', 'comb1', 'comb2', 'comb3', 'comb4', 'comb5', 'comb6';canBeLaunched: boolean
- Optional. TRUE if use may click by a rocket to launch it;autoLaunch: boolean
- Optional. TRUE if rocket should be automatically launched;noSound: boolean
- Optional. TRUE if need to mute all sounds;