CircleType

repository·master·Indexed 20 days ago

https://github.com/peterhry/circletype

A JavaScript library (v2.3.2) that allows developers to create curved text effects on the web. It provides a CircleType class to instantiate circular text on HTML elements with support for adjustable radius, text direction, and layout controls like forceWidth() and forceHeight().

Tokens
2.3K
Snippets
14
Records
14
Agent score
71%

What's inside circletype

  1. Install CircleType

    master

    You can install CircleType using several methods depending on your environment:

    Browser (Script Tag)

    Include the minified file directly in your HTML:

    <script src="circletype.min.js"></script>

    CDN

    Use JSDelivr to load the library via a URL:

    <script src="https://cdn.jsdelivr.net/gh/peterhry/CircleType@2.3.1/dist/circletype.min.js"></script>

    npm

    Install via npm for use in modern build pipelines:

    $ npm i circletype --save

    ES Modules

    Import the class directly in your JavaScript files:

    import CircleType from `circletype`;
    import CircleType from `circletype`;
  2. Initialize a new CircleType instance

    master

    To create a circular text effect, instantiate the CircleType class by passing a target HTMLElement.

    Optional: You can provide a splitter function. This function is used to split the element's text content into individual characters. This is useful for handling complex characters like emojis using libraries like grapheme-splitter.

    Setter methods on the instance are chainable.

    // Instantiate `CircleType` with an HTML element.
    const circleType = new CircleType(document.getElementById('myElement'));
    
    // Set the text radius and direction. Note: setter methods are chainable.
    circleType.radius(200).dir(-1);
    
    // Provide your own splitter function to handle emojis
    const splitter = new GraphemeSplitter()
    new CircleType(
      document.getElementById('myElement'),
      splitter.splitGraphemes.bind(splitter)
    );
  3. Initialize CircleType

    master

    To create circular text, instantiate the CircleType class by passing a target HTML element. You can optionally provide a splitter function to define how the text content is broken down into individual characters (e.g., to support emojis via a grapheme splitter).

    Setter methods on the instance are chainable.

    // Basic instantiation
    const circleType = new CircleType(document.getElementById('myElement'));
    
    // Set radius and direction using chaining
    circleType.radius(200).dir(-1);
    
    // Using a custom splitter (e.g., for emojis)
    const splitter = new GraphemeSplitter();
    new CircleType(
      document.getElementById('myElement'),
      splitter.splitGraphemes.bind(splitter)
    );
  4. Import CircleType

    master

    You can import the CircleType class from the circletype package using CommonJS require or ES module import syntax. This class is the primary entry point for creating circular text effects on DOM elements.

    const CircleType = require('circletype');
  5. Control layout with forceWidth() and forceHeight()

    master

    These methods control whether the calculated arc dimensions are applied as inline styles to the container element.

    forceWidth(value)

    • Getter: Returns a boolean indicating if the width is being forced.
    • Setter: Sets the forceWidth option. If true, the width of the arc is calculated and applied to the element as an inline style. Defaults to false.

    forceHeight(value)

    • Getter: Returns a boolean indicating if the height is being forced.
    • Setter: Sets the forceHeight option. If true, the height of the arc is calculated and applied to the element as an inline style. Defaults to true.
    const circleType = new CircleType(document.getElementById('myElement'));
    
    // Enable force width
    circleType.forceWidth(true);
    
    // Disable force height
    circleType.forceHeight(false);
  6. Configure text radius with radius()

    master

    The radius() method allows you to get or set the text radius in pixels.

    • Getter: Returns the current text radius as a number.
    • Setter: Sets a new radius. The minimum radius is the amount required for the text to form a complete circle. If the provided value is less than this minimum, the minimum radius is used instead.

    Returns the CircleType instance for chaining.

    const circleType = new CircleType(document.getElementById('myElement'));
    
    // Get radius
    circleType.radius(); // returns number
    
    // Set radius
    circleType.radius(150);
  7. Refresh or Destroy a CircleType instance

    master

    refresh()

    Schedules a task to recalculate the height of the element. Call this if the font size of the target element changes.

    destroy()

    Removes the CircleType effect from the element, restoring it to its original state.

    const circleType = new CircleType(document.getElementById('myElement'));
    
    // Recalculate if font size changed
    circleType.refresh();
    
    // Remove effect and restore original element state
    circleType.destroy();
  8. Set text direction with dir()

    master

    The dir() method allows you to get or set the text direction.

    • Getter: Returns the current direction as a number.
    • Setter: Sets the direction. 1 is clockwise, -1 is counter-clockwise.

    Returns the CircleType instance for chaining.

    const circleType = new CircleType(document.getElementById('myElement'));
    
    // Get direction
    circleType.dir(); // returns 1 (clockwise)
    
    // Set direction
    circleType.dir(-1); // counter-clockwise
    circleType.dir(1);  // clockwise
  9. Make a complete circle with fullCircle()

    master

    The fullCircle() method adds equal spacing between letters to ensure the text forms a complete circle, regardless of the current radius.

    const circleType = new CircleType(document.getElementById('myElement'));
    circleType.radius(200);
    
    // Adds spacing between letters to make a full rotation
    circleType.fullCircle();
  10. Control arc dimensions with forceWidth() and forceHeight()

    master

    By default, CircleType manages the height of the container to fit the arc, but not the width. You can toggle these behaviors:

    • forceHeight(value): If true, the height of the arc is calculated and applied to the element as an inline style. Defaults to true.
    • forceWidth(value): If true, the width of the arc is calculated and applied to the element as an inline style. Defaults to false.

    Both methods return the CircleType instance for chaining.

    const circleType = new CircleType(document.getElementById('myElement'));
    
    // Enable width calculation
    circleType.forceWidth(true);
    
    // Disable height calculation
    circleType.forceHeight(false);