Welcome to Ray's Blog

Stay Hungry Stay Foolish - Steve Jobs

0%

Creating a Stunning 3D Tag Cloud with SVG3DTagCloud

Creating a Stunning 3D Tag Cloud with SVG3DTagCloud 🎨✨πŸ–₯️

πŸš€ Introduction

Ever wanted to make your website pop with interactive animations? 🎭 Imagine a floating, rotating 3D tag cloud that smoothly reacts to your mouse movementβ€”sounds cool, right? 😎 That’s exactly what SVG3DTagCloud helps you achieve. Whether you’re showcasing skills, topics, or categories, this lightweight JavaScript library lets you do it in style.

In this guide, we’ll explore:

  • πŸ›  What makes SVG3DTagCloud special
  • πŸ“Œ How to install and use it
  • 🎨 Customizing it for your needs
  • πŸ— Implementation details for better control

πŸ”— GitHub Repository: SVG3DTagCloud on GitHub


🎯 Why Use a 3D Tag Cloud?

  • πŸ’‘ Visually engaging: 3D tag clouds add an interactive touch to your UI.
  • 🎨 Customizable: Change colors, fonts, and movement settings easily.
  • ⚑ Lightweight & Fast: Works efficiently without bloating your website.
  • πŸ”„ Smooth Animations: Uses JavaScript to provide a seamless experience.

πŸ”§ Installation Guide πŸ—οΈ

Option 1: Install via NPM πŸ“¦

1
npm install svg-3d-tag-cloud

Option 2: Use a CDN 🌍

For quick setup, simply include the following script in your HTML:

1
<script src="https://unpkg.com/svg-3d-tag-cloud/dist/SVG3DTagCloud.global.js"></script>

🎭 Implementation: Core Functionality Explained πŸ”

How Does the 3D Effect Work? πŸŒ€

SVG3DTagCloud creates the illusion of 3D movement using simple mathematical transformations. Each tag is positioned in 3D space and rotated dynamically based on user interaction. The key mechanics include:

  1. 🌐 Spherical Coordinate System: Each tag is assigned a coordinate on an imaginary sphere.
  2. πŸ”„ Rotation Calculation: The library applies trigonometric functions (sine & cosine) to smoothly rotate the sphere.
  3. πŸ” Perspective Scaling: Tags farther from the viewer appear smaller, simulating depth.
  4. 🎯 Mouse Interaction: Moving the mouse changes the sphere’s rotation speed, making it feel interactive.

Step 1: Setting Up the 3D Space 🌍

1
2
3
4
5
6
7
const setChildPosition = (child, radius) => {
const { x, y, z } = child.vectorPosition;
const distance = Math.sqrt(x * x + y * y + z * z);
child.vectorPosition.x = (x / distance) * radius;
child.vectorPosition.y = (y / distance) * radius;
child.vectorPosition.z = (z / distance) * radius;
};

This function normalizes each tag’s position onto a spherical surface.

Step 2: Applying Rotation on Mouse Move πŸ–±οΈ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const updateRotation = (angleX, angleY) => {
const sinX = Math.sin(angleX);
const cosX = Math.cos(angleX);
const sinY = Math.sin(angleY);
const cosY = Math.cos(angleY);

tags.forEach((tag) => {
const { x, y, z } = tag.vectorPosition;
const tempX = cosY * x - sinY * z;
const tempZ = sinY * x + cosY * z;
tag.vectorPosition.x = cosX * tempX + sinX * y;
tag.vectorPosition.y = -sinX * tempX + cosX * y;
tag.vectorPosition.z = tempZ;
});
};

This applies 2D rotation matrices to move the tags based on the mouse position.

Step 3: Rendering the Tags on the Screen πŸ–₯️

1
2
3
4
5
6
tags.forEach((tag) => {
const scale = fov / (fov + tag.vectorPosition.z);
tag.element.style.transform = `translate(${tag.vectorPosition.x * scale}px, ${
tag.vectorPosition.y * scale
}px)`;
});

The perspective projection ensures that tags closer to the viewer appear larger, and those farther away appear smaller.


🎨 Styling the Tag Cloud 🌈

Use CSS to customize the appearance further:

1
2
3
4
5
6
7
8
9
#tag-cloud {
display: flex;
justify-content: center;
align-items: center;
background: radial-gradient(circle, #1e1e1e, #000);
border-radius: 10px;
padding: 20px;
box-shadow: 0px 0px 20px rgba(255, 255, 255, 0.1);
}

This gives it a sleek, futuristic look! ✨


🎯 Final Thoughts πŸ’­

SVG3DTagCloud is a fun and visually appealing way to enhance your UI. Whether you’re building portfolios, documentation sites, or dashboards, this component adds a dynamic element to keep users engaged.

πŸ”₯ Try It Out!

  1. πŸš€ Add a 3D tag cloud to your personal website.
  2. πŸ§ͺ Experiment with different settings & animations.
  3. 🌍 Share your creations with the community! πŸš€

πŸ”— GitHub Repository: SVG3DTagCloud on GitHub

**πŸ’‘ Have ideas for improvements? Connect Me : https://www.linkedin.com/in/lei-chen-364721320/ ** 🎨✨