<?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: SamrithaS</title>
    <description>The latest articles on DEV Community by SamrithaS (@samrithas).</description>
    <link>https://dev.to/samrithas</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%2F597187%2F742760f4-0247-4f14-95c5-b582c2e1f2f5.jpg</url>
      <title>DEV Community: SamrithaS</title>
      <link>https://dev.to/samrithas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/samrithas"/>
    <language>en</language>
    <item>
      <title>Click to generate crazy particles - with Canvas</title>
      <dc:creator>SamrithaS</dc:creator>
      <pubDate>Thu, 03 Feb 2022 13:05:40 +0000</pubDate>
      <link>https://dev.to/samrithas/click-to-generate-crazy-particles-with-canvas-49po</link>
      <guid>https://dev.to/samrithas/click-to-generate-crazy-particles-with-canvas-49po</guid>
      <description>&lt;p&gt;Let's use HTML Canvas to generate crazy particles on clicking.&lt;/p&gt;

&lt;p&gt;Check it out here:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/samritha/embed/preview/NWazdee?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEPS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The HTML file contains &lt;code&gt;&amp;lt;canvas&amp;gt;&amp;lt;/canvas&amp;gt;&lt;/code&gt; that would create a canvas element.&lt;/p&gt;

&lt;p&gt;The rest of the code belongs to the JS file.&lt;br&gt;
Now that we have the canvas element,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector("canvas");
const c = canvas.getContext("2d");
canvas.style.background = "black";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This helps us get the canvas element from the DOM,&lt;code&gt;const c = canvas.getContext("2d")&lt;/code&gt; defines the drawing context of the canvas which is 2D and &lt;code&gt;canvas.style.background = "black&lt;/code&gt; sets a background color to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
window.addEventListener("resize", () =&amp;gt; {
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we are setting the window's width and height to the canvas's width and height respectively, also making sure that when the window is resized, the width and height are in sync.&lt;/p&gt;

&lt;p&gt;Let's create a class named Circle which would help us generate the circular particles on clicking,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Circle {
    constructor(x, y, color, radius) {
        this.x = x;
        this.y = y;
        this.color = color;
        this.radius = radius;
    }
    draw() {
        c.beginPath();
        c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
        c.fillStyle = this.color;
        c.fill();
        c.closePath();
    }
    update() {
        this.draw();
        this.x += Math.random() * 8 - 4;
        this.y += Math.random() * 8 - 4;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The parameters of the class are x, y, color and radius corresponding to positions, color and radius of the circle.&lt;br&gt;
There are two methods in the class: &lt;code&gt;draw()&lt;/code&gt; and &lt;code&gt;update()&lt;/code&gt;,&lt;br&gt;
draw method will help us draw a circle using &lt;code&gt;beginPath()&lt;br&gt;
&lt;/code&gt; which would begin drawing a path in the canvas and &lt;code&gt;c.arc(x, y, radius, startAngle, endAngle [, counterclockwise])&lt;/code&gt; will help us create an arc, this would take x, y, radius, startAngle, endAngle and counterclockwise(boolean) arguments.&lt;/p&gt;

&lt;p&gt;update() is the method that makes the circle particles act a bit crazy by updating the x and y positions of the particles, making it move around a bit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let prevX;
let prevY;
let circles = [];
document.addEventListener("click", () =&amp;gt; {
    clientx = window.event.clientX;
    clienty = window.event.clientY;
    for (let i = 0; i &amp;lt; 10; i++) {
        const x = Math.random() * i * 20 + clientx;
        const y = Math.random() * i * 20 + clienty;
        const radius = Math.random() * 5;
        let val = i * 200 * Math.random() * clienty;
        let perc = Math.random() * 90;
        let color = `hsl(${val}, ${perc}%, 60%)`;
        let rad = Math.random() * 20;
        circles.push(new Circle(x, y, color, rad));
    }
    animate();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After adding a click event listener, &lt;code&gt;window.event.clientX&lt;/code&gt; and &lt;code&gt;window.event.clientY&lt;/code&gt; gives us the vertical and horizontal coordinates of the mouse pointer.&lt;br&gt;
Since I want 10 circles to be generated on each click, I have a loop running from 0 to 10, and each of the circles would have different positions, colors and sizes with the help of &lt;code&gt;Math.random()&lt;/code&gt;. Next we'll be creating an instance of the class Circle and pushing these instances into the 'circles' array. Finally, the animate function will be called which is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function animate() {
    requestAnimationFrame(animate);
    c.clearRect(0, 0, canvas.width, canvas.height);
    circles.forEach((circle) =&amp;gt; {
        circle.update();
    });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;requestAnimationFrame&lt;/code&gt; tells the browser that we wish to perform an animation, it takes a callback function, in our case it's the animate function itself - it would be called recursively creating the crazy effect we need.&lt;br&gt;
Last but not the least, for each element of the 'circles' array, which contains all the instances of circles created, we would call the &lt;code&gt;update()&lt;/code&gt; method.&lt;br&gt;
Notice that, the update method calls the draw method itself which is why we haven't called it elsewhere. So the update method draws a circle and also updates it's position everytime the animate function is called.&lt;/p&gt;

&lt;p&gt;and that's about it.&lt;/p&gt;

&lt;p&gt;Thank you for coming this far, hope you loved the blog as much as I loved writing it.&lt;br&gt;
Here's where we can connect:&lt;/p&gt;

&lt;p&gt;instagram: &lt;a href="https://www.instagram.com/artzy_artoholic/"&gt;https://www.instagram.com/artzy_artoholic/&lt;/a&gt;&lt;br&gt;
twitter: &lt;a href="https://twitter.com/Samritha22"&gt;https://twitter.com/Samritha22&lt;/a&gt;&lt;br&gt;
codepen: &lt;a href="https://codepen.io/samritha"&gt;https://codepen.io/samritha&lt;/a&gt;&lt;/p&gt;

</description>
      <category>canvas</category>
      <category>html</category>
      <category>javascript</category>
      <category>animation</category>
    </item>
    <item>
      <title>My CSS-art Codepens!</title>
      <dc:creator>SamrithaS</dc:creator>
      <pubDate>Fri, 02 Jul 2021 13:54:53 +0000</pubDate>
      <link>https://dev.to/samrithas/my-css-art-codepens-209m</link>
      <guid>https://dev.to/samrithas/my-css-art-codepens-209m</guid>
      <description>&lt;p&gt;Hello there!&lt;br&gt;
This is Samritha.S, a front-end dev exploring things that make a website interesting. &lt;br&gt;
Welcome to my list of CSS-art codepens&lt;/p&gt;
&lt;h1&gt;
  
  
  1. Css owl with sneaky eyes
&lt;/h1&gt;

&lt;p&gt;This is a css owl with some javascript.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/samritha/embed/eYWNBJR?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Pure CSS Pizza loader
&lt;/h1&gt;

&lt;p&gt;Who doesn't love pizza? checkout my pure CSS Pizza loader!&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/samritha/embed/ExWqdQx?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  3. Pure Css Parrot
&lt;/h1&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/samritha/embed/oNYRjdj?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  4. Moving car
&lt;/h1&gt;

&lt;p&gt;Speedy car with pure css&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/samritha/embed/eYgdXye?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  5. Css man
&lt;/h1&gt;

&lt;p&gt;He says hello.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/samritha/embed/oNBvLNq?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Thank you for coming this far, hope you loved them as much as I loved creating them.&lt;br&gt;
Here's where we can connect:&lt;/p&gt;

&lt;p&gt;instagram: &lt;a href="https://www.instagram.com/artzy_artoholic/"&gt;https://www.instagram.com/artzy_artoholic/&lt;/a&gt;&lt;br&gt;
twitter: &lt;a href="https://twitter.com/Samritha22"&gt;https://twitter.com/Samritha22&lt;/a&gt;&lt;br&gt;
codepen: &lt;a href="https://codepen.io/samritha"&gt;https://codepen.io/samritha&lt;/a&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>codepen</category>
      <category>cssart</category>
      <category>cssanimation</category>
    </item>
  </channel>
</rss>
