1.4.0 • Published 9 months ago

svg-text-animate v1.4.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

svg-text-animate.js

[中文] [English]

Svg-text-animate is a JavaScript library for convert text to SVG stroke animations in the browser.

Alt text Alt text Alt text

See svg-text-animate for a live demo.


Usage

NPM

npm i svg-text-animate

CDN

<script src="https://cdn.jsdelivr.net/gh/oubenruing/svg-text-animate@latest/dist/svg-text-animate.min.js"></script>

Demo

    import SVGTextAnimate from 'svt-text-animate;
    
    const opensans = new SVGTextAnimate("https://cdn.jsdelivr.net/gh/oubenruing/svg-text-animate@latest/docs/fonts/OpenSans-Regular-webfont.woff", {
      "duration": 300,
      "direction": "normal",
      "fill-mode": "forwards",
      "delay": 150,
      "mode": "delay"
    }, {
      "stroke": "#005792",
      "stroke-width": "2px",
      "font-size": 55,
      "fill-color":"white"
    });

    await opensans.setFont();
    
    opensans.create("svg-text-animate", "#name");

Constructor

Creates an instance of SVGTextAnimate.

SVGTextAnimate(fontfile, options, stroke, cretor)

  • @param {String} Required fontfile: Path of font file with WOFF, OTF, TTF (both with TrueType glyf and PostScript cff outlines)
  • @param {Object} Required options: {duration,timing-function,iteration-count,direction,fill-mode,delay,mode}
  • @param {Object} Required stroke: {stroke,stroke-width,font-size}
  • @param {String} Optional creator: The mode of animation, use CSSCreator by default.

Font-size moved from ‘options’ to ‘stroke’ in version 1.2.0

Options

An Object for controlling animation

NameTypeDefault valueDescription
durationNumber1000this option sets the length of time that an animation takes to complete one cycle in milliseconds.
timing-functionStringlinearSame as the animation-timing-function CSS property.
iteration-countNumber1Same as the animation-iteration-count CSS property.
directionStringnormalSame as the animation-direction CSS property.
fill-modeStringforwardsSame as the animation-fill-mode CSS property.
modeStringsync"sync":All symbols appear at the same time; "onebyone":a symbol appears after the last symbol animation;"delay":a symbol appears n milliseconds later when the last symbol starts drawing.(The value of n is taken from the next option)
delayNumber0Only work with mode:"delay"

Stroke

An Object for controlling stroke

NameTypeDefault valueDescription
strokeString#000000The hex color of stroke
stroke-widthString1pxThe width of stroke
font-sizeNumber72Output font size.
fill-colorStringtransparentFill color.

Creator

A string describing the animation mode.

ParamDescription
css(default) Using CSSCreator. Create a CSS style SVG animation.(Using \<style> tag and @keyframes).
svgUsing SVGCreator. Create a SMIL SVG animation.(Using \<animate> tag).In this case:1.The option 'timing-function' always works in 'linear' mode.2.The option 'fill-mode' only works in 'forwards' mode or 'none' mode.3.The option 'direction' always works in 'normal' mode.The 'svg' mode can be used in the scenario where CSS is not supported

Methods

setfont()

An asynchronous method for loading font files passed in from the constructor. This method will returns a Promise. Each font just needs to be loaded only once.

  await opensans.setFont();

or

  opensans.setFont().then();

setFontFromBuffer()

Load a font file from an ArrayBuffer Each font just needs to be loaded only once. returns current instance

setOptions(options)

set an Object for controlling animation, same as options returns current instance

setStroke(stroke)

set an Object for controlling animation, same as stroke returns current instance

create(text,selector) add(text,selector)

create a svg animation from the given string. Clear the selector first, then insert SVG into the DOM of delector. add(v1.2.0) a svg animation from the given string and inserts it into the DOM of the selector.

returns current instance

NameTypeDescription
textStringThe text you want to animate
selectorStringThe DOM selector you want to insert into
    opensans.create("svg-text-animate", "#name");

    opensans.setOptions({
      "duration": 500,
      "timing-function": "linear",
      "direction": "normal",
      "fill-mode": "forwards",
      "delay": 50,
      "mode": "sync"
    }).setStroke({
      "stroke": "white",
      "stroke-width": "2px",
      "font-size": 23,
      "fill-color":"white"
    }).create("Try it", ".button");

Special fonts

  • Fonts with a large number of characters, such as chinese.
  • Fonts exported from icons, for example the fonts download from fontawesom.

In the first case, I recommend compressing the font first. Try font-spider or other font compress tools;

In the second case, you can use like this

    fontawesome.setOptions({
      "duration": 2000,
      "timing-function": "linear",
      "direction": "alternate",
      "delay": 500,
      "iteration-count": "infinite",
      "mode": "sync"
    }).setStroke({
      "stroke": "white",
      "stroke-width": "1px",
      "font-size": 22,
      "fill-color":"white"
    }).create(String.fromCharCode(0xf581), "#symbols")
      .create(String.fromCharCode(0xf164), "#symbols2");

      //you can find hex code on the website where you downloaded the iconfont.
      //Using String.fromCharCode(hexcode) as "create" method`s first parameter.

Thanks

opentype.js Read and write OpenType fonts using JavaScript. fontawesom The iconic SVG, font, and CSS toolkit. font-spider Smart webfont compression and format conversion tool.


(c) oubenruing 2019 | MIT License

1.4.0

9 months ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago