Install VueWordCloud via npm
masterTo use VueWordCloud in your project, install it using npm. For Vue 3 compatibility, ensure you are using version >18.
npm i vuewordcloudrepository·master·Indexed 19 days ago
https://github.com/seregpie/vuewordcloudA Vue.js component for generating word clouds from text data. It supports customizable styling, animations, and custom rendering via slots. Version 19.0.0 provides compatibility for Vue 3. Key features include support for custom font loading, Web Worker implementations, and flexible animation configurations using objects or CSS classes.
To use VueWordCloud in your project, install it using npm. For Vue 3 compatibility, ensure you are using version >18.
npm i vuewordcloudAnimations for enter-animation and leave-animation can be defined in three ways:
opacity and transform.// Object-based
let enterAnimation = {opacity: 0};
let leaveAnimation = {opacity: 0};
// Complex object
let enterAnimation = {
opacity: 0,
transform: 'scale3d(0.3,0.3,0.3)'
};
// CSS Class-based
let enterAnimation = 'animated bounceIn';
let leaveAnimation = 'animated hinge';You can register the component either globally in your main Vue instance or locally within a specific component's scope.
// Global registration
import Vue from 'vue';
import VueWordCloud from 'vuewordcloud';
Vue.component(VueWordCloud.name, VueWordCloud);// Local registration in a component
import VueWordCloud from 'vuewordcloud';
export default {
components: {
[VueWordCloud.name]: VueWordCloud,
},
};For direct browser usage, include Vue and VueWordCloud via unpkg. If Vue is detected on the page, the component will be registered automatically as VueWordCloud.
<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/vuewordcloud"></script>Render a word cloud by passing a words array and configuring styles like color, font-family, and dimensions via CSS.
<vue-word-cloud
style="
height: 480px;
width: 640px;
"
:words="[['romance', 19], ['horror', 3], ['fantasy', 7], ['adventure', 3]]"
:color="([, weight]) => weight > 10 ? 'DeepPink' : weight > 5 ? 'RoyalBlue' : 'Indigo'"
font-family="Roboto"
/>Use the default slot with slot-scope to provide a custom renderer for each word. This allows you to wrap words in custom HTML elements and attach event listeners like @click.
<vue-word-cloud :words="words">
<template slot-scope="{text, weight, word}">
<div :title="weight" style="cursor: pointer;" @click="onWordClick(word)">
{{ text }}
</div>
</template>
</vue-word-cloud>You can provide custom functions for load-font, create-canvas, and create-worker to extend functionality or support older browsers.
Example: Custom loadFont using FontFaceObserver
import FontFaceObserver from 'fontfaceobserver';
let loadFont = function(family, style, weight, text) {
return (new FontFaceObserver(family, {style, weight})).load(text);
};Example: Custom createWorker
let createWorker = function(code) {
return new Worker(URL.createObjectURL(new Blob([code])));
};let createCanvas = function() {
return document.createElement('canvas');
};
let loadFont = function(fontFamily, fontStyle, fontWeight, text) {
return document.fonts.load([fontStyle, fontWeight, '1px', fontFamily].join(' '), text);
};
let createWorker = function(code) {
return new Worker(URL.createObjectURL(new Blob([code])));
};The following events are emitted by the vue-word-cloud component.
| event | description |
| ---: | :--- |
| `update:progress` | The current progress of the cloud words computation. |The following properties are available for configuring the vue-word-cloud component.
| property | type | default | description |
| ---: | :--- | :--- | :--- |
| `animation-duration` | `Number` | `1000` | The duration of the animation. |
| `animation-easing` | `String` | `'ease'` | The easing of the animation. |
| `animation-overlap` | `Number` | `1` | The overlap of the animation. Set the value to `1` to animate words all at once. Set the value to `0` to animate words one by one. The value `5` has the same effect as the value `1/5`. |
| `color` | `[String, Function]` | `'Black'` | The default color for each word. |
| `create-canvas` | `Function` | * | Creates a new `Canvas` instance. |
| `create-worker` | `Function` | * | Creates a new `Worker` instance. |
| `enter-animation` | `[Object, String]` | * | The enter animation. |
| `font-family` | `[String, Function]` | `'serif'` | The default font family for each word. |
| `font-size-ratio` | `Number` | `0` | The font size ratio between the words. For example, if the value is `5`, then the largest word will be 5 times larger than the smallest one. The value `5` has the same effect as the value `1/5`. |
| `font-style` | `[String, Function]` | `'normal'` | The default font style for each word. |
| `font-variant` | `[String, Function]` | `'normal'` | The default font variant for each word. |
| `font-weight` | `[String, Function]` | `'normal'` | The default font weight for each word. |
| `leave-animation` | `[Object, String]` | * | The leave animation. |
| `load-font` | `Function` | * | Loads the font. |
| `rotation-unit` | `[String, Function]` | `'turn'` | The default rotation unit for each word. Possible values are `'turn'`, `'deg'` and `'rad'.` |
| `rotation` | `[Number, Function]` | `0` | The default rotation for each word. |
| `spacing` | `Number` | `0` | The spacing between the words. The value is relative to the font size. |
| `text` | `[String, Function]` | `''` | The default text for each word. |
| `weight` | `[Number, Function]` | `1` | The default weight for each word. |
| `words` | `Array` | `[]` | The words to place into the cloud. A value of the array could be either an object, an array or a string.<br/>If the value is an object, it will be resolved to `{text, weight, rotation, rotationUnit, fontFamily, fontStyle, fontVariant, fontWeight, color}`.<br/>If the value is an array, it will be resolved to `[text, weight]`.<br/>If the value is a string, it will be resolved to `text`. |To use VueWordCloud, import the default export and pass your word data to the words prop. You can provide words as simple strings or as objects for fine-grained control over individual word properties.
<template>
<VueWordCloud
:words="myWords"
:animation-duration="2000"
color="#333"
/>
</template>
<script setup>
import VueWordCloud from 'vuewordcloud';
const myWords = [
'Hello',
{ text: 'World', weight: 10, color: 'red', rotation: 45, rotationUnit: 'deg' },
{ text: 'Vue', weight: 5, fontFamily: 'serif' }
];
</script>The VueWordCloud component emits the following events:
update:cloudWords: Emitted when the cloudWords data is updated.update:progress: Emitted with the current progress of the word cloud generation.The vuewordcloud package exports the VueWordCloud component as the default export. You can import it directly into your Vue application to use the word cloud functionality.
import VueWordCloud from 'vuewordcloud';