Install TagCloud
masterYou can install TagCloud via npm for Node.js environments or include it directly in your HTML via a <script> tag for browser environments.
$ npm i -S TagCloudrepository·master·Indexed 19 days ago
https://github.com/cong-min/tagcloudA lightweight (6KB), dependency-free 3D tag cloud library for browser and Node.js environments. Version 2.5.0 features a rolling animation controlled by mouse movement and provides a constructor for initialization along with instance methods for updating content, pausing, resuming, and destroying the cloud.
You can install TagCloud via npm for Node.js environments or include it directly in your HTML via a <script> tag for browser environments.
$ npm i -S TagCloudInclude the minified script in your HTML and then call the TagCloud function.
<!-- html -->
<script type="text/javascript" src="./dist/TagCloud.min.js"></script>
<script>
TagCloud(container, texts, options);
</script>Include the minified script directly in your HTML file to use TagCloud in the browser.
<script type="text/javascript" src="./dist/TagCloud.min.js"></script>Use the TagCloud constructor to create a 3D tag cloud. It requires a container, a list of texts, and an optional configuration object.
const TagCloud = require('TagCloud');
const container = '.tagcloud';
const texts = [
'3D', 'TagCloud', 'JavaScript',
'CSS3', 'Animation', 'Interactive',
'Mouse', 'Rolling', 'Sphere',
'6KB', 'v2.x',
];
const options = {};
TagCloud(container, texts, options);const TagCloud = require('TagCloud');
const container = '.tagcloud';
const texts = [
'3D', 'TagCloud', 'JavaScript',
'CSS3', 'Animation', 'Interactive',
'Mouse', 'Rolling', 'Sphere',
'6KB', 'v2.x',
];
const options = {};
TagCloud(container, texts, options);Install the TagCloud package using npm to use it in your Node.js or bundled frontend projects.
$ npm i -S TagCloudThe options object allows you to customize the behavior and appearance of the tag cloud.
| Option | Type | Default | Description |
|---|---|---|---|
radius | Number | 100 | Rolling radius in px. |
maxSpeed | 'slow'|'normal'|'fast' | 'normal' | Maximum rolling speed. |
initSpeed | 'slow'|'normal'|'fast' | 'normal' | Initial rolling speed. |
direction | Number | 135 | Initial rolling direction in clockwise degrees (e.g., 0 is up, 90 is left, 135 is bottom-right). |
keep | Boolean | true | If true, the cloud continues to roll (decelerating to initial speed) when the mouse leaves the container. |
reverseDirection | Boolean | false | Whether to reverse the direction when controlled by the mouse. |
containerClass | String | 'tagcloud' | CSS class for the container. |
itemClass | String | 'tagcloud--item' | CSS class for each tag item. |
useContainerInlineStyles | Boolean | true | If true, adds inline styles to the container. If false, you must provide your own CSS. |
useItemInlineStyles | Boolean | true | If true, adds inline styles to each item. If false, you must provide your own CSS. |
Since TagCloud items are generated dynamically, use event delegation on a parent element to handle clicks. Check if e.target.className matches the itemClass (default is tagcloud--item).
let rootEl = document.querySelector('.content');
rootEl.addEventListener('click', function clickEventHandler(e) {
if (e.target.className === 'tagcloud--item') {
window.open(`https://www.google.com/search?q=${e.target.innerText}`, '_blank');
// your code here
}
});TagCloud items do not have individual event listeners attached. To handle clicks or other interactions on specific tags, use event delegation on the container element. Check the className of the target to ensure you are interacting with a tag item (tagcloud--item).
Example: Opening a Google search for the clicked tag text:
let rootEl = document.querySelector('.content');
rootEl.addEventListener('click', function clickEventHandler(e) {
if (e.target.className === 'tagcloud--item') {
window.open(`https://www.google.com/search?q=${e.target.innerText}`, '_blank');
}
});let rootEl = document.querySelector('.content');
rootEl.addEventListener('click', function clickEventHandler(e) {
if (e.target.className === 'tagcloud--item') {
window.open(`https://www.google.com/search?q=${e.target.innerText}`, '_blank');
// your code here
}
});Import TagCloud and call the constructor with a container selector (or element), an array of strings, and an options object.
const TagCloud = require('TagCloud');
const container = '.tagcloud';
const texts = [
'3D', 'TagCloud', 'JavaScript',
'CSS3', 'Animation', 'Interactive',
'Mouse', 'Rolling', 'Sphere',
'6KB', 'v2.x',
];
const options = {};
TagCloud(container, texts, options);The TagCloud(container, texts, options) function initializes a 3D rolling tag cloud. It returns a TagCloud instance that allows you to control the animation.
// Returns tagcloud instance
const instance = TagCloud(container, texts, options);The object returned by the TagCloud constructor provides methods to manage the lifecycle and state of the animation.
const instance = TagCloud(container, texts, options);
instance.update(texts); // Update the tag list
instance.pause(); // Pause the animation
instance.resume(); // Resume the animation
instance.destroy(); // Destroy the instanceThe TagCloud constructor returns an instance that provides methods to control the lifecycle and data of the cloud.
tagcloud.update(texts): Updates the list of tag texts.tagcloud.pause(): Pauses the animation.tagcloud.resume(): Resumes the animation.tagcloud.destroy(): Destroys the tag cloud instance.