JSFiddle - React, Tailwind, and code Playground
by Alexey Ossikine
HTML
<canvas id="canvas"></canvas>
CSS
html,
body {
width: 100%;
height: 100%;
margin:0;
padding:0;
background: #e8e8e8;
}
#canvas {
width: 100%;
height: 100%;
margin: 0 auto;
}
JavaScript
window.requestAnimFrame = function()
{
return (
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback){
window.setTimeout(callback, 1000 / 60);
}
);
}();
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
//get DPI
let dpi = window.devicePixelRatio || 1;
context.scale(dpi, dpi);
console.log(dpi);
function fix_dpi() {
//get CSS height
//the + prefix casts it to an integer
//the slice method gets rid of "px"
let style_height = +getComputedStyle(canvas).getPropertyValue("height").slice(0, -2);
let style_width = +getComputedStyle(canvas).getPropertyValue("width").slice(0, -2);
//scale the canvas
canvas.setAttribute('height', style_height * dpi);
canvas.setAttribute('width', style_width * dpi);
}
var particle_count = 80,
particles = [],
couleurs = ["#0db7e0", "#a8c1cc"];
function Particle()
{
this.radius = Math.round((Math.random()*3)+5);
this.x = Math.floor((Math.random() * ((+getComputedStyle(canvas).getPropertyValue("width").slice(0, -2) * dpi) - this.radius + 1) + this.radius));
this.y = Math.floor((Math.random() * ((+getComputedStyle(canvas).getPropertyValue("height").slice(0, -2) * dpi) - this.radius + 1) + this.radius));
this.color = couleurs[Math.round(Math.random()*couleurs.length)];
this.speedx = Math.round((Math.random()*201)+0)/1500;
this.speedy = Math.round((Math.random()*201)+0)/1500;
switch (Math.round(Math.random()*couleurs.length))
{
case 1:
this.speedx *= 1;
this.speedy *= 1;
break;
case 2:
this.speedx *= -1;
this.speedy *= 1;
break;
case 3:
this.speedx *= 1;
this.speedy *= -1;
break;
...