GithubHelp home page GithubHelp logo

avgjs / pixi-richtext Goto Github PK

View Code? Open in Web Editor NEW
75.0 4.0 9.0 160 KB

A Rich Text solution for PixiJS using SDF.

License: MIT License

HTML 0.48% JavaScript 99.52%
pixijs signed-distance-field rich-text avgjs pixi-richtext sdf pixi

pixi-richtext's Introduction

pixi-richtext

Rich-text for PixiJS!

Feature

  • Runtime signed distance field algorithm, with LRU cache for 1024 characters
  • Better rendering effect
  • No font file required, while supporting custom font via @font-face
  • Full support for CJK languages
  • Layout using huozi
  • Rich-text support

Usage

npm install pixi-richtext --save
import PIXI from 'pixi.js';
import RichText from 'pixi-richtext';

const app = new PIXI.Application({
  view: document.getElementById('app'),
  width: 1280,
  height: 720
});

const chars = '泽材<fillColor=0xff6600>灭<bold>逐</bold></fillColor>莫笔<strokeEnable=true>亡</strokeEnable>鲜,<strokeEnable=true><strokeColor=black><fillColor=red><fontSize=64>如何</fontSize><fillColor=orange><italic>气</italic><fillColor=yellow><bold>死</bold><fillColor=green>你的<fillColor=0xff6600>设<fillColor=blue>计师<fillColor=magenta><fontSize=28>朋</fontSize>友</fillColor></fillColor></fillColor></fillColor></fillColor></fillColor></fillColor></strokeColor></strokeEnable>';
// see more: https://github.com/Icemic/huozi.js
const layoutOptions = {
  // any valid CSS font-family value is supported
  // includes the fonts imported using @font-face
  fontFamily: 'sans-serif',
  // grid width for layout, 1em = gridSize
  gridSize: 26,
  // max width presented by character count
  // (max-width = (gridSize + xInterval) * column - xInterval)
  column: 25,
  // max line number
  row: Infinity,
  // interval between characters (CJK only)
  xInterval: 0,
  // interval between lines
  yInterval: 12,
  // (for western characters)
  letterSpacing: 0,
  // compress punctuation inline (CJK only)
  inlineCompression: true,
  forceGridAlignment: true,
  // enable it if your text do not include CJK characters
  westernCharacterFirst: false,
  forceSpaceBetweenCJKAndWestern: false
}

const defaultStyle = {
  fillEnable: true,
  fillColor: 'black',
  fillAlpha: 1,

  shadowEnable: false,
  shadowColor: 'black',
  shadowAlpha: 1,
  shadowAngle: Math.PI / 6,
  shadowDistance: 5,
  shadowThickness: 2,
  shadowBlur: 0.15,

  strokeEnable: false,
  strokeColor: 'black',
  strokeAlpha: 1,
  strokeThickness: 0,

  fontFamily: 'sans-serif',
  fontSize: 18,

  italic: false,
  bold: false,

  // unsupported now
  strike: false,
  underline: false,

  layout: layoutOptions
};



const text = new RichText(chars, defaultStyle, layoutOptions);
app.stage.addChild(text);
text.x = 0;
text.y = 100;

Just like what you see above, rich-text is expressed by UBB-like code. All the tags in defaultStyle are supported. Tags in text will overwrite the style temporarily until the they were closed.

In addition, you can use text.renderPosition to make a typewriter effect. See more in example/demo.js.

Todos

  • Support pre-generated SDF texture
  • Generating SDF texture in single channel, which will expend cache number to 4096
  • Support strike and underline
  • Custom SDF algorithm, eg. msdf

Related Works

Contribution

Any contribution is welcomed!

License

MIT LICENSE, SEE LICENSE.

pixi-richtext's People

Contributors

icemic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

pixi-richtext's Issues

Can't get demo to work / How can I test in es5?

Hey,

I've been trying to get this working in just a plain index.html page with javascript but my webpack to compile is not working.

Nor can I test the demo as it fails on es6 import.

The usage in the readme doesn't help much

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.