VueWordCloud

repository·master·Indexed 19 days ago

https://github.com/seregpie/vuewordcloud

A 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.

Tokens
3.3K
Snippets
11
Records
13
Agent score
65%

What's inside vuewordcloud

  1. Configure custom animations

    master

    Animations for enter-animation and leave-animation can be defined in three ways:

    1. Object-based: Define CSS properties directly.
    2. String-based (CSS Classes): Use existing CSS class names (e.g., for Animate.css).
    3. Complex Object: Combine properties like 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';
  2. Register VueWordCloud as an ES module

    master

    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,
      },
    };
  3. Use VueWordCloud via browser `<script>` tags

    master

    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>
  4. Basic usage of VueWordCloud

    master

    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"
    />
  5. Customize word rendering with slots

    master

    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>
  6. Implement custom `loadFont` and `createWorker` functions

    master

    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])));
    };
  7. Reference: VueWordCloud properties

    master

    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`. |
  8. Use the VueWordCloud component

    master

    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>