Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
Creating Magic Web Component with Source Code - Creating Magic Web Component with Source Code

Creating Magic Web Component with Source Code

Posted on November 10, 2024November 10, 2024 By Rishabh saini No Comments on Creating Magic Web Component with Source Code

Web Component

In the world of modern web development, adding interactive elements can elevate a website’s experience from simple browsing to dynamic engagement. The <pointer-particles> web component is an innovative tool that can help you achieve just that by adding captivating particle effects that follow the user’s mouse movements. This simple but powerful component creates a layer of responsiveness that enhances user experience, and with its straightforward setup, it’s accessible to developers of all levels.

Creating Interactive Magic Web Component with Source Code

Step 1: Include the <pointer-particles> Library

To use <pointer-particles>, you’ll need to add the script to your HTML file. You can either use a CDN link or download the library to host it locally.

Here’s an example using a CDN link:

<pointer-particles></pointer-particles>

Step 2: CSS Customize the Particle Settings

Now that the component is added, it’s time to configure it. You can use JavaScript or add specific attributes to the <pointer-particles> element itself for easy customization. Here’s an example:

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  overflow: hidden;
  display: grid;
  color: white;
  background: black;
}

In this example, we’re setting:

  • color to a hex color code (e.g., pink particles),
  • particle-size to define particle dimensions,
  • trail-length to adjust how long the particle trail stays on the screen,
  • speed to control the animation speed.

With these simple configurations, you can tailor the particle effects to match your website’s style and feel.

Download New Real Time Projects :-Click here

Step 3: JS

class PointerParticle {
  constructor(spread, speed, component) {
    const { ctx, pointer, hue } = component;

    this.ctx = ctx;
    this.x = pointer.x;
    this.y = pointer.y;
    this.mx = pointer.mx * 0.1;
    this.my = pointer.my * 0.1;
    this.size = Math.random() + 1;
    this.decay = 0.01;
    this.speed = speed * 0.08;
    this.spread = spread * this.speed;
    this.spreadX = (Math.random() - 0.5) * this.spread - this.mx;
    this.spreadY = (Math.random() - 0.5) * this.spread - this.my;
    this.color = `hsl(${hue}deg 90% 60%)`;
  }

  draw() {
    this.ctx.fillStyle = this.color;
    this.ctx.beginPath();
    this.ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
    this.ctx.fill();
  }

  collapse() {
    this.size -= this.decay;
  }

  trail() {
    this.x += this.spreadX * this.size;
    this.y += this.spreadY * this.size;
  }

  update() {
    this.draw();
    this.trail();
    this.collapse();
  }
}

class PointerParticles extends HTMLElement {
  static register(tag = "pointer-particles") {
    if ("customElements" in window) {
      customElements.define(tag, this);
    }
  }

  static css = `
    :host {
      display: grid;
      width: 100%;
      height: 100%;
      pointer-events: none;
    }
  `;

  constructor() {
    super();

    this.canvas;
    this.ctx;
    this.fps = 60;
    this.msPerFrame = 1000 / this.fps;
    this.timePrevious;
    this.particles = [];
    this.pointer = {
      x: 0,
      y: 0,
      mx: 0,
      my: 0
    };
    this.hue = 0;
  }

  connectedCallback() {
    const canvas = document.createElement("canvas");
    const sheet = new CSSStyleSheet();

    this.shadowroot = this.attachShadow({ mode: "open" });

    sheet.replaceSync(PointerParticles.css);
    this.shadowroot.adoptedStyleSheets = [sheet];

    this.shadowroot.append(canvas);

    this.canvas = this.shadowroot.querySelector("canvas");
    this.ctx = this.canvas.getContext("2d");
    this.setCanvasDimensions();
    this.setupEvents();
    this.timePrevious = performance.now();
    this.animateParticles();
  }

  createParticles(event, { count, speed, spread }) {
    this.setPointerValues(event);

    for (let i = 0; i < count; i++) {
      this.particles.push(new PointerParticle(spread, speed, this));
    }
  }

  setPointerValues(event) {
    this.pointer.x = event.x - this.offsetLeft;
    this.pointer.y = event.y - this.offsetTop;
    this.pointer.mx = event.movementX;
    this.pointer.my = event.movementY;
  }

  setupEvents() {
    const parent = this.parentNode;

    parent.addEventListener("click", (event) => {
      this.createParticles(event, {
        count: 300,
        speed: Math.random() + 1,
        spread: Math.random() + 50
      });
    });

    parent.addEventListener("pointermove", (event) => {
      this.createParticles(event, {
        count: 20,
        speed: this.getPointerVelocity(event),
        spread: 1
      });
    });

    window.addEventListener("resize", () => this.setCanvasDimensions());
  }

  getPointerVelocity(event) {
    const a = event.movementX;
    const b = event.movementY;
    const c = Math.floor(Math.sqrt(a * a + b * b));

    return c;
  }

  handleParticles() {
    for (let i = 0; i < this.particles.length; i++) {
      this.particles[i].update();

      if (this.particles[i].size <= 0.1) {
        this.particles.splice(i, 1);
        i--;
      }
    }
  }

  setCanvasDimensions() {
    const rect = this.parentNode.getBoundingClientRect();

    this.canvas.width = rect.width;
    this.canvas.height = rect.height;
  }

  animateParticles() {
    requestAnimationFrame(() => this.animateParticles());

    const timeNow = performance.now();
    const timePassed = timeNow - this.timePrevious;

    if (timePassed < this.msPerFrame) return;

    const excessTime = timePassed % this.msPerFrame;

    this.timePrevious = timeNow - excessTime;

    this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
    this.hue = this.hue > 360 ? 0 : (this.hue += 3);

    this.handleParticles();
  }
}

PointerParticles.register();

https://updategadh.com/category/php-project

Creating Interactive Magic Web Component with Source Code
Creating Interactive Magic Web Component with Source Code

Real-World Applications

<pointer-particles> offers a touch of flair that can be used effectively in several scenarios:

  1. Landing Pages: Attract attention on introductory pages by enhancing the user’s first interaction with the site.
  2. Interactive Presentations: Use particles to guide users’ attention through complex information, adding subtle direction and focus.
  3. Gaming Websites: Increase engagement by creating a responsive environment that’s visually exciting for users.
  4. Personal Portfolios: Make your portfolio stand out with a bit of interactivity that demonstrates your attention to detail and modern design skills.
  • web components library
  • web components 2024
  • interactive web components
  • github web components
  • functional web components
  • input web component
  • web components data binding
  • web components 2023
  • building magic web component react
  • building magic web component github
  • building magic web component example
  • building magic web component javascript
Post Views: 438
Portfolio Tags:component, components, components web, custom components, how to create a web component, lit web components, web component, web component 2020, web component api, web component guide, web component intro, web component on lit, web component tutorial, web components, web components best practices, web components javascript, web components js, web components ts, web components tutorial, web components vs react, what are web components

Post navigation

Previous Post: Online Quiz System in PHP
Next Post: Python OS Module :A Comprehensive Guide

More Related Articles

Portfolio Page Using HTML, CSS, and JavaScript - Portfolio Page Using HTML, CSS, and JavaScript Portfolio Page Using HTML, CSS, and JavaScript Portfolio
Portfolio Page Using HTML , CSS - Portfolio Page Using HTML , CSS Portfolio Page Using HTML , CSS Portfolio
Portfolio Page Using HTML, CSS, and JavaScript - Creating Portfolio Page Using HTML, CSS, and JavaScript Portfolio Page Using HTML, CSS, and JavaScript Portfolio

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may also like

  1. Portfolio Page Using HTML, CSS, and JavaScript
  2. Portfolio Page Using HTML, CSS, and JavaScript
  3. Portfolio Page Using HTML, CSS, and JavaScript
  4. Portfolio Page Using HTML , CSS

Most Viewed Posts

  1. Top Large Language Models in 2025
  2. Online Shopping System using PHP, MySQL with Free Source Code
  3. login form in php and mysql , Step-by-Step with Free Source Code
  4. Flipkart Clone using PHP And MYSQL Free Source Code
  5. News Portal Project in PHP and MySql Free Source Code
  6. User Login & Registration System Using PHP and MySQL Free Code
  7. Top 10 Final Year Project Ideas in Python
  8. Blog Site In PHP And MYSQL With Source Code || Best Project
  9. Online Bike Rental Management System Using PHP and MySQL
  10. E learning Website in php with Free source code
  • AI
  • ASP.NET
  • Blockchain
  • ChatCPT
  • code Snippets
  • Collage Projects
  • Data Science Project
  • Data Science Tutorial
  • DBMS Tutorial
  • Deep Learning Tutorial
  • Final Year Projects
  • Free Projects
  • How to
  • html
  • Interview Question
  • Java Notes
  • Java Project
  • Java Script Notes
  • JAVASCRIPT
  • Javascript Project
  • JSP JAVA(J2EE)
  • Machine Learning Project
  • Machine Learning Tutorial
  • MySQL Tutorial
  • Node.js Tutorial
  • PHP Project
  • Portfolio
  • Python
  • Python Interview Question
  • Python Projects
  • PythonFreeProject
  • React Free Project
  • React Projects
  • Spring boot
  • SQL Tutorial
  • TOP 10
  • Uncategorized
  • Agentic RAG AI System Using Python – Complete Final Year Project Guide
  • AI-Powered Online Examination System with Face Detection Using PHP & MySQL
  • Real-Time Medical Queue & Appointment System with Django
  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital

Most Viewed Posts

  • Top Large Language Models in 2025 (8,630)
  • Online Shopping System using PHP, MySQL with Free Source Code (5,248)
  • login form in php and mysql , Step-by-Step with Free Source Code (4,911)

Copyright © 2026 UpdateGadh.

Powered by PressBook Green WordPress theme