<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hriteek Kumar</title>
    <description>The latest articles on DEV Community by Hriteek Kumar (@dinesh45).</description>
    <link>https://dev.to/dinesh45</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2629473%2F48fe57fc-afc5-4b80-b1b4-8da063744820.jpg</url>
      <title>DEV Community: Hriteek Kumar</title>
      <link>https://dev.to/dinesh45</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dinesh45"/>
    <language>en</language>
    <item>
      <title>Happy Birthday code.</title>
      <dc:creator>Hriteek Kumar</dc:creator>
      <pubDate>Sun, 29 Dec 2024 13:54:10 +0000</pubDate>
      <link>https://dev.to/dinesh45/happy-birthday-code-2agc</link>
      <guid>https://dev.to/dinesh45/happy-birthday-code-2agc</guid>
      <description>&lt;p&gt;`&amp;lt;!DOCTYPE html&amp;gt;&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;br&gt;
    &lt;/p&gt;
&lt;br&gt;

&lt;p&gt;&amp;lt;p&amp;gt;/* basic styles for black background and crosshair cursor */&amp;lt;br&amp;gt;&lt;br&gt;
body {&amp;lt;br&amp;gt;&lt;br&gt;
    background: #000;&amp;lt;br&amp;gt;&lt;br&gt;
    margin: 0;&amp;lt;br&amp;gt;&lt;br&gt;
}&amp;lt;/p&amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;lt;p&amp;gt;canvas {&amp;lt;br&amp;gt;&lt;br&gt;
    cursor: crosshair;&amp;lt;br&amp;gt;&lt;br&gt;
    display: block;&amp;lt;br&amp;gt;&lt;br&gt;
}&amp;lt;/p&amp;gt;&lt;br&gt;
&amp;lt;div class="highlight"&amp;gt;&amp;lt;pre class="highlight plaintext"&amp;gt;&amp;lt;code&amp;gt;   &amp;amp;lt;/STYLE&amp;amp;gt;&lt;br&gt;&lt;br&gt;
&amp;amp;lt;/HEAD&amp;amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;amp;lt;BODY&amp;amp;gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;amp;amp;lt;canvas id="canvas"&amp;amp;amp;gt; &amp;amp;amp;lt;/canvas&amp;amp;amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;br&gt;
&amp;lt;script&amp;gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// when animating on canvas, it is best to use requestAnimationFrame instead of setTimeout or setInterval
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;// not supported in all browsers though and sometimes needs a prefix, so we need a shim&lt;br&gt;
window.requestAnimFrame = ( function() {&lt;br&gt;
    return window.requestAnimationFrame ||&lt;br&gt;
                window.webkitRequestAnimationFrame ||&lt;br&gt;
                window.mozRequestAnimationFrame ||&lt;br&gt;
                function( callback ) {&lt;br&gt;
                    window.setTimeout( callback, 1000 / 60 );&lt;br&gt;
                };&lt;br&gt;
})();&lt;/p&gt;

&lt;p&gt;// now we will setup our basic variables for the demo&lt;br&gt;
var canvas = document.getElementById( 'canvas' ),&lt;br&gt;
        ctx = canvas.getContext( '2d' ),&lt;br&gt;
        // full screen dimensions&lt;br&gt;
        cw = window.innerWidth,&lt;br&gt;
        ch = window.innerHeight,&lt;br&gt;
        // firework collection&lt;br&gt;
        fireworks = [],&lt;br&gt;
        // particle collection&lt;br&gt;
        particles = [],&lt;br&gt;
        // starting hue&lt;br&gt;
        hue = 120,&lt;br&gt;
        // when launching fireworks with a click, too many get launched at once without a limiter, one launch per 5 loop ticks&lt;br&gt;
        limiterTotal = 5,&lt;br&gt;
        limiterTick = 0,&lt;br&gt;
        // this will time the auto launches of fireworks, one launch per 80 loop ticks&lt;br&gt;
        timerTotal = 80,&lt;br&gt;
        timerTick = 0,&lt;br&gt;
        mousedown = false,&lt;br&gt;
        // mouse x coordinate,&lt;br&gt;
        mx,&lt;br&gt;
        // mouse y coordinate&lt;br&gt;
        my;&lt;/p&gt;

&lt;p&gt;// set canvas dimensions&lt;br&gt;
canvas.width = cw;&lt;br&gt;
canvas.height = ch;&lt;/p&gt;

&lt;p&gt;// now we are going to setup our function placeholders for the entire demo&lt;/p&gt;

&lt;p&gt;// get a random number within a range&lt;br&gt;
function random( min, max ) {&lt;br&gt;
    return Math.random() * ( max - min ) + min;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;// calculate the distance between two points&lt;br&gt;
function calculateDistance( p1x, p1y, p2x, p2y ) {&lt;br&gt;
    var xDistance = p1x - p2x,&lt;br&gt;
            yDistance = p1y - p2y;&lt;br&gt;
    return Math.sqrt( Math.pow( xDistance, 2 ) + Math.pow( yDistance, 2 ) );&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;// create firework&lt;br&gt;
function Firework( sx, sy, tx, ty ) {&lt;br&gt;
    // actual coordinates&lt;br&gt;
    this.x = sx;&lt;br&gt;
    this.y = sy;&lt;br&gt;
    // starting coordinates&lt;br&gt;
    this.sx = sx;&lt;br&gt;
    this.sy = sy;&lt;br&gt;
    // target coordinates&lt;br&gt;
    this.tx = tx;&lt;br&gt;
    this.ty = ty;&lt;br&gt;
    // distance from starting point to target&lt;br&gt;
    this.distanceToTarget = calculateDistance( sx, sy, tx, ty );&lt;br&gt;
    this.distanceTraveled = 0;&lt;br&gt;
    // track the past coordinates of each firework to create a trail effect, increase the coordinate count to create more prominent trails&lt;br&gt;
    this.coordinates = [];&lt;br&gt;
    this.coordinateCount = 3;&lt;br&gt;
    // populate initial coordinate collection with the current coordinates&lt;br&gt;
    while( this.coordinateCount-- ) {&lt;br&gt;
        this.coordinates.push( [ this.x, this.y ] );&lt;br&gt;
    }&lt;br&gt;
    this.angle = Math.atan2( ty - sy, tx - sx );&lt;br&gt;
    this.speed = 2;&lt;br&gt;
    this.acceleration = 1.05;&lt;br&gt;
    this.brightness = random( 50, 70 );&lt;br&gt;
    // circle target indicator radius&lt;br&gt;
    this.targetRadius = 1;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;// update firework&lt;br&gt;
Firework.prototype.update = function( index ) {&lt;br&gt;
    // remove last item in coordinates array&lt;br&gt;
    this.coordinates.pop();&lt;br&gt;
    // add current coordinates to the start of the array&lt;br&gt;
    this.coordinates.unshift( [ this.x, this.y ] );&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// cycle the circle target indicator radius
if( this.targetRadius &amp;amp;lt; 8 ) {
    this.targetRadius += 0.3;
} else {
    this.targetRadius = 1;
}

// speed up the firework
this.speed *= this.acceleration;

// get the current velocities based on angle and speed
var vx = Math.cos( this.angle ) * this.speed,
        vy = Math.sin( this.angle ) * this.speed;
// how far will the firework have traveled with velocities applied?
this.distanceTraveled = calculateDistance( this.sx, this.sy, this.x + vx, this.y + vy );

// if the distance traveled, including velocities, is greater than the initial distance to the target, then the target has been reached
if( this.distanceTraveled &amp;amp;gt;= this.distanceToTarget ) {
    createParticles( this.tx, this.ty );
    // remove the firework, use the index passed into the update function to determine which to remove
    fireworks.splice( index, 1 );
} else {
    // target not reached, keep traveling
    this.x += vx;
    this.y += vy;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;// draw firework&lt;br&gt;
Firework.prototype.draw = function() {&lt;br&gt;
    ctx.beginPath();&lt;br&gt;
    // move to the last tracked coordinate in the set, then draw a line to the current x and y&lt;br&gt;
    ctx.moveTo( this.coordinates[ this.coordinates.length - 1][ 0 ], this.coordinates[ this.coordinates.length - 1][ 1 ] );&lt;br&gt;
    ctx.lineTo( this.x, this.y );&lt;br&gt;
    ctx.strokeStyle = 'hsl(' + hue + ', 100%, ' + this.brightness + '%)';&lt;br&gt;
    ctx.stroke();&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ctx.beginPath();
// draw the target for this firework with a pulsing circle
ctx.arc( this.tx, this.ty, this.targetRadius, 0, Math.PI * 2 );
ctx.stroke();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;// create particle&lt;br&gt;
function Particle( x, y ) {&lt;br&gt;
    this.x = x;&lt;br&gt;
    this.y = y;&lt;br&gt;
    // track the past coordinates of each particle to create a trail effect, increase the coordinate count to create more prominent trails&lt;br&gt;
    this.coordinates = [];&lt;br&gt;
    this.coordinateCount = 5;&lt;br&gt;
    while( this.coordinateCount-- ) {&lt;br&gt;
        this.coordinates.push( [ this.x, this.y ] );&lt;br&gt;
    }&lt;br&gt;
    // set a random angle in all possible directions, in radians&lt;br&gt;
    this.angle = random( 0, Math.PI * 2 );&lt;br&gt;
    this.speed = random( 1, 10 );&lt;br&gt;
    // friction will slow the particle down&lt;br&gt;
    this.friction = 0.95;&lt;br&gt;
    // gravity will be applied and pull the particle down&lt;br&gt;
    this.gravity = 1;&lt;br&gt;
    // set the hue to a random number +-20 of the overall hue variable&lt;br&gt;
    this.hue = random( hue - 20, hue + 20 );&lt;br&gt;
    this.brightness = random( 50, 80 );&lt;br&gt;
    this.alpha = 1;&lt;br&gt;
    // set how fast the particle fades out&lt;br&gt;
    this.decay = random( 0.015, 0.03 );&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;// update particle&lt;br&gt;
Particle.prototype.update = function( index ) {&lt;br&gt;
    // remove last item in coordinates array&lt;br&gt;
    this.coordinates.pop();&lt;br&gt;
    // add current coordinates to the start of the array&lt;br&gt;
    this.coordinates.unshift( [ this.x, this.y ] );&lt;br&gt;
    // slow down the particle&lt;br&gt;
    this.speed *= this.friction;&lt;br&gt;
    // apply velocity&lt;br&gt;
    this.x += Math.cos( this.angle ) * this.speed;&lt;br&gt;
    this.y += Math.sin( this.angle ) * this.speed + this.gravity;&lt;br&gt;
    // fade out the particle&lt;br&gt;
    this.alpha -= this.decay;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// remove the particle once the alpha is low enough, based on the passed in index
if( this.alpha &amp;amp;lt;= this.decay ) {
    particles.splice( index, 1 );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;// draw particle&lt;br&gt;
Particle.prototype.draw = function() {&lt;/p&gt;

&lt;p&gt;ctx. beginPath();&lt;br&gt;
    // move to the last tracked coordinates in the set, then draw a line to the current x and y&lt;br&gt;
    ctx.moveTo( this.coordinates[ this.coordinates.length - 1 ][ 0 ], this.coordinates[ this.coordinates.length - 1 ][ 1 ] );&lt;br&gt;
    ctx.lineTo( this.x, this.y );&lt;br&gt;
    ctx.strokeStyle = 'hsla(' + this.hue + ', 100%, ' + this.brightness + '%, ' + this.alpha + ')';&lt;br&gt;
    ctx.stroke();&lt;/p&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;// create particle group/explosion&lt;br&gt;
function createParticles( x, y ) {&lt;br&gt;
    // increase the particle count for a bigger explosion, beware of the canvas performance hit with the increased particles though&lt;br&gt;
    var particleCount = 30;&lt;br&gt;
    while( particleCount-- ) {&lt;br&gt;
        particles.push( new Particle( x, y ) );&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;// main demo loop&lt;br&gt;
function loop() {&lt;br&gt;
    // this function will run endlessly with requestAnimationFrame&lt;br&gt;
    requestAnimFrame( loop );&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// increase the hue to get different colored fireworks over time
hue += 0.5;

// normally, clearRect() would be used to clear the canvas
// we want to create a trailing effect though
// setting the composite operation to destination-out will allow us to clear the canvas at a specific opacity, rather than wiping it entirely
ctx.globalCompositeOperation = 'destination-out';
// decrease the alpha property to create more prominent trails
ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
ctx.fillRect( 0, 0, cw, ch );
// change the composite operation back to our main mode
// lighter creates bright highlight points as the fireworks and particles overlap each other
ctx.globalCompositeOperation = 'lighter';

var text = "HAPPY BIRTHDAY BUG_CODERS";  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;ctx.font = "55px sans-serif";&lt;br&gt;
  var textData = ctx.measureText(text);&lt;/p&gt;

&lt;p&gt;ctx.fillStyle = "rgba("+parseInt(random(100,255))+","+parseInt(random(0,255))+","+parseInt(random(0,255))+",0.3)";&lt;br&gt;
  ctx.fillText(text,cw /2-textData.width/2,ch/2); &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// loop over each firework, draw it, update it
var i = fireworks.length;
while( i-- ) {
    fireworks[ i ].draw();
    fireworks[ i ].update( i );
}

// loop over each particle, draw it, update it
var i = particles.length;
while( i-- ) {
    particles[ i ].draw();
    particles[ i ].update( i );
}

// launch fireworks automatically to random coordinates, when the mouse isn't down
if( timerTick &amp;amp;gt;= timerTotal ) {
    if( !mousedown ) {
        // start the firework at the bottom middle of the screen, then set the random target coordinates, the random y coordinates will be set within the range of the top half of the screen

  for(var h=0;h&amp;amp;lt;50;h++)
  {
       fireworks.push( new Firework( cw / 2, ch/2, random( 0, cw ), random( 0, ch  ) ) );
  }


        timerTick = 0;
    }
} else {
    timerTick++;
}

// limit the rate at which fireworks get launched when mouse is down
if( limiterTick &amp;amp;gt;= limiterTotal ) {
    if( mousedown ) {
        // start the firework at the bottom middle of the screen, then set the current mouse coordinates as the target
        fireworks.push( new Firework( cw / 2, ch/2, mx, my ) );
        limiterTick = 0;
    }
} else {
    limiterTick++;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;// mouse event bindings&lt;br&gt;
// update the mouse coordinates on mousemove&lt;br&gt;
canvas.addEventListener( 'mousemove', function( e ) {&lt;br&gt;
    mx = e.pageX - canvas.offsetLeft;&lt;br&gt;
    my = e.pageY - canvas.offsetTop;&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;// toggle mousedown state and prevent canvas from being selected&lt;br&gt;
canvas.addEventListener( 'mousedown', function( e ) {&lt;br&gt;
    e.preventDefault();&lt;br&gt;
    mousedown = true;&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;canvas.addEventListener( 'mouseup', function( e ) {&lt;br&gt;
    e.preventDefault();&lt;br&gt;
    mousedown = false;&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;// once the window loads, we are ready for some fireworks!&lt;br&gt;
window.onload = loop;&lt;/p&gt;

&lt;p&gt;&amp;lt;/script&amp;gt;&lt;br&gt;
&amp;lt;div class="highlight"&amp;gt;&amp;lt;pre class="highlight plaintext"&amp;gt;&amp;lt;code&amp;gt;&amp;amp;lt;/BODY&amp;amp;gt;&lt;br&gt;
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;br&gt;
&amp;lt;p&amp;gt;&amp;lt;/HTML&amp;gt;&amp;amp;#39;&amp;lt;/p&amp;gt;&lt;/p&gt;

</description>
      <category>codepen</category>
    </item>
  </channel>
</rss>
