GithubHelp home page GithubHelp logo

pixijs / html-text Goto Github PK

View Code? Open in Web Editor NEW
38.0 38.0 12.0 3.4 MB

Multi-Style Text Rendering Plugin for PixiJS

Home Page: https://www.npmjs.com/package/@pixi/text-html

TypeScript 100.00%
html javascript pixijs rendering svg text

html-text's Introduction

PixiJS โ€” The HTML5 Creation Engine

pixi.js logo

Discord npm version Node.js CI Financial Contributors

This project aims to provide a fast, lightweight 2D library that works across all devices. The PixiJS renderer allows everyone to enjoy the power of hardware acceleration without prior knowledge of WebGL. Also, it's fast. Really fast.

If you want to keep up to date with the latest PixiJS news then feel free to follow us on Twitter @PixiJS and we will keep you posted! You can also check back on our site as any breakthroughs will be posted up there too!

We are now a part of the Open Collective and with your support you can help us make PixiJS even better. To make a donation, simply click the button below and we'll love you forever!

What to Use PixiJS for and When to Use It

PixiJS is a rendering library that will allow you to create rich, interactive graphics and cross-platform applications and games without having to dive into the WebGL API or deal with browser and device compatibility.

PixiJS supports WebGPU with fallback support for WebGL. As a library, PixiJS is a fantastic tool for authoring interactive content. Use it for your graphics-rich, interactive websites, applications, and HTML5 games. Out-of-the-box, cross-platform compatibility and graceful degradation mean you have less work to do and more fun doing it! If you want to create polished and refined experiences relatively quickly without delving into dense, low-level code, all while avoiding the headaches of browser inconsistencies, then sprinkle your next project with some PixiJS magic!

Boost your development and feel free to use your imagination!

Current features

  • WebGL renderer (with automatic smart batching, allowing for REALLY fast performance)
  • WebGPU renderer (new to the latest browsers!)
  • Canvas renderer (Fastest in town!)
  • Full scene graph
  • Super easy to use API (similar to the flash display list API)
  • Support for texture atlases
  • Asset loader / sprite sheet loader
  • Auto-detect which renderer should be used
  • Full Mouse and Multi-touch Interaction
  • Text
  • BitmapFont text
  • Multiline Text
  • Render Texture
  • Primitive Drawing
  • Masking
  • Filters
  • Community-Supported Plugins

Setup

It's easy to get started with PixiJS! Simply download a prebuilt build!

Alternatively, PixiJS can be installed with npm or simply using a content delivery network (CDN) URL to embed PixiJS directly on your HTML page.

NPM Install

npm install pixi.js

There is no default export. The correct way to import PixiJS is:

import * as PIXI from 'pixi.js';

CDN Install

Via jsDelivr:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/pixi.min.js"></script>

Or via unpkg:

<script src="https://unpkg.com/[email protected]/dist/pixi.min.js"></script>

Basic Usage Example

import { Application, Sprite, Assets } from 'pixi.js';

// The application will create a renderer using WebGL, if possible,
// with a fallback to a canvas render. It will also setup the ticker
// and the root stage PIXI.Container
const app = new Application();

// Wait for the Renderer to be available
await app.init();

// The application will create a canvas element for you that you
// can then insert into the DOM
document.body.appendChild(app.canvas);

// load the texture we need
const texture = await Assets.load('bunny.png');

// This creates a texture from a 'bunny.png' image
const bunny = new Sprite(texture);

// Setup the position of the bunny
bunny.x = app.renderer.width / 2;
bunny.y = app.renderer.height / 2;

// Rotate around the center
bunny.anchor.x = 0.5;
bunny.anchor.y = 0.5;

// Add the bunny to the scene we are building
app.stage.addChild(bunny);

// Listen for frame updates
app.ticker.add(() => {
    // each frame we spin the bunny around a bit
    bunny.rotation += 0.01;
});

Learn

  • Website: Find out more about PixiJS on the official website.
  • Getting Started:
    • Check out the getting started guide.
    • Also, check out @miltoncandelero's PixiJS tutorials aimed toward videogames with recipes and best practices here
  • Examples: Get stuck right in and play around with PixiJS code and features right here!
  • API Documentation: Get to know the PixiJS API by checking out the docs.
  • Guide: Supplementary usage guides to the API Documentation here.

Demos

Community

  • Forums: Check out the discussions and Stackoverflow -- both friendly places to ask your PixiJS questions.
  • Chat: You can join us on Discord to chat about PixiJS.

How to build

Note that for most users you don't need to build this project. If all you want is to use PixiJS, then just download one of our prebuilt releases. The only time you should need to build PixiJS is if you are developing it.

If you don't already have Node.js and NPM, go install them. Then, in the folder where you have cloned the repository, install the build dependencies using npm:

npm install

Then, to build the source, run:

npm run build

How to generate the documentation

The docs can be generated using npm:

npm run docs

Contribute

Want to be part of the PixiJS project? Great! All are welcome! We will get there quicker together :) Whether you find a bug, have a great feature request, or you fancy owning a task from the road map above, feel free to get in touch.

Make sure to read the Contributing Guide before submitting changes.

License

This content is released under the (http://opensource.org/licenses/MIT) MIT License.

html-text's People

Contributors

bcazur avatar bigtimebuddy avatar littleboarx avatar rubenlg avatar seleb 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

Watchers

 avatar  avatar  avatar  avatar

html-text's Issues

Support for measuring HTMLText output dimensions

For regular Text objects we have PIXI.TextMetrics.measureText, which doesn't require creating the DisplayObject to know the dimensions of the text. However, that won't work correctly for HTMLText because markup is counted as extra characters.

It would be very useful to have something like measureText for HTMLText so we can have a lightweight way of measuring the size the text would have before rendering the object.

For my particular case, as a workaround I could create a div element, add my text as innerHTML and then get the innerText to pass it to PIXI.TextMetrics.measureText. It wouldn't be completely accurate, since markup can make the font slightly larger, or add extra space, but I don't have these issues. However, for the general case, having an accurate helper in the library would be really nice.

Non Breaking space seems to break the render

Looks like when i have the presence of a non breaking space in the html string passed in to render it goes blank on me
An example of the string to reproduce this is <p class="orwell--align-left"><strong>eeee&nbsp;</strong></p><p class="orwell--align-left"><strong>hello abul atta</strong></p>

Many thanks as you look into it

Performance Profile Slow-down in Chrome

Did some benchmarking of PIXI.HTMLText vs PIXI.Text and found some interesting things:

๐Ÿข HTMLText is about 2-4 times slow in Chrome than Text

chrome-1

โš–๏ธ HTMLText is about equal in Safari with Text

safari-1

๐Ÿ‡ HTMLText is about 50% faster in Firefox than Text

firefox-1

I'm going to profile Chrome to see if we can improve performance. The goal is that this is a non-brainer replacement for Text with no or few concessions.

Example:
https://jsfiddle.net/bigtimebuddy/ntusg3ak/

Can't install, npm ERR! ERESOLVE could not resolve, npm ERR! While resolving: @pixi/[email protected] Found: @pixi/[email protected]

Hi,
I have problem with npm install on Laravel 8 project

package.json:

"dependencies": {
    "@pixi-essentials/bounds": "^2.1.1",
    "@pixi/graphics-smooth": "^0.0.13",
    "@pixi/text-html": "^1.0.1",
    "cocoen": "^2.0.5",
    "d-path-parser": "^1.0.0",
    "event-dispatch": "^0.4.1",
    "geometric": "^2.2.8",
    "history": "^5.3.0",
    "perfect-scrollbar": "^1.5.5",
    "pixi-svg": "^2.3.1",
    "pixi-text-input": "^1.0.6",
    "pixi.js": "^6.0.4",
    "pixijs-grid": "^0.4.1",
    "qr-scanner": "^1.2.0",
    "select2": "^4.0.13",
    "slick-carousel": "^1.8.1",
    "x2js": "^3.4.1"
  },
  "devDependencies": {
    "browser-sync": "^2.26.14",
    "browser-sync-webpack-plugin": "^2.2.2",
    "cross-env": "^6.0.3",
    "eslint": "^6.8.0",
    "laravel-mix": "^5.0.5",
    "resolve-url-loader": "^3.1.0",
    "sass": "^1.35.1",
    "sass-loader": "^8.0.2",
    "vue-template-compiler": "^2.6.11",
    "xml2js": "^0.4.23"
  }

npm install:

npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: @pixi/[email protected]
npm ERR! Found: @pixi/[email protected]
npm ERR! node_modules/@pixi/core
npm ERR!   peer @pixi/core@"6.1.1" from @pixi/[email protected]
npm ERR!   node_modules/@pixi/accessibility
npm ERR!     @pixi/accessibility@"6.1.1" from [email protected]
npm ERR!     node_modules/pixi.js
npm ERR!       pixi.js@"^6.0.4" from the root project
npm ERR!   peer @pixi/core@"6.1.1" from @pixi/[email protected]
npm ERR!   node_modules/@pixi/app
npm ERR!     @pixi/app@"6.1.1" from [email protected]
npm ERR!     node_modules/pixi.js
npm ERR!       pixi.js@"^6.0.4" from the root project
npm ERR!   25 more (@pixi/compressed-textures, @pixi/extract, ...)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer @pixi/core@"^5.0.0" from @pixi/[email protected]
npm ERR! node_modules/@pixi/text-html
npm ERR!   @pixi/text-html@"^1.0.1" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: @pixi/[email protected]
npm ERR! node_modules/@pixi/core
npm ERR!   peer @pixi/core@"^5.0.0" from @pixi/[email protected]
npm ERR!   node_modules/@pixi/text-html
npm ERR!     @pixi/text-html@"^1.0.1" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

Windows 11
Node: 16-lastest
npm: 8-latest

Regards!

Sanitize the text or document the XSS vector

Since the text being added to the HTMLText object is HTML, that opens the door for XSS vulnerabilities if, for example, an application takes the formatted text from user input or an external resource.

Here is an example:

const unsafeString = `Hello <img onerror="console.log('XSS')" src="-"/> world!`;
const text = new HTMLText(unsafeString);

One option is to use an HTML sanitization library. Another option is to document clearly that when using HTMLText, the developer is responsible for sanitizing the text being passed in to prevent XSS vulnerabilities.

About input space

Only one space is displayed when multiple spaces are entered, and the width is not increased if the last character is a space.
Is there any way to enter more than one space?

Custom fonts not working

I'm not able to show custom fonts with HTML Text. If I replace PIXI.HTMLText with PIXI.Text my custom font is showing properly.

const app = new PIXI.Application({
  width: window.innerWidth,
  height: window.innerHeight,
  resolution: window.devicePixelRatio,
  autoDensity: true,
  transparent: true,
  resizeTo: window,
});

const textStyle = new PIXI.TextStyle({
  fontFamily: ["Work Sans", "Courier New", "sans-serif"],
  fontSize: 16 * 4,
  wordWrapWidth: window.innerWidth / 2,
  wordWrap: true,
  fontWeight: 350,
  fill: 0xffffff,
});

const title = new PIXI.HTMLText(
  "<strong>Custom</strong> font family",
  textStyle
);
title.x = 100;
title.y = 100;
app.stage.addChild(title);

Shows the fallback font:
PIXI HTMLText

If I replace new PIXI.HTMLText with new PIXI.Text my fonts are showing properly:
PIXI Text

Pixi 6 not supported

"pixi.js": "^6.0.4"

npm install @pixi/text-html gives the following error:

image

Looks like @pixi/math@"^5.0.0" dependency needs to be updated to @pixi/[email protected]? Possibly other deps too?

Thanks. By the way, I just realised that Pixi v6 is in pre-release, so I tried downgrading to that v5 and that just caused other dependencies to fail.

flashing between updates

When I change css style frequently, it will flash between updates

version: 2.0.2 (I already have saw the commit "fix flashing between updates", but it seemly still exists

Import not working

I've tried implementing this, but for some reason it says PIXI is not defined when I import text-html

ReferenceError: PIXI is not defined
./node_modules/@pixi/text-html/dist/html-text.js
node_modules/@pixi/text-html/dist/html-text.js:8
   5 |  * @pixi/text-html is licensed under the MIT License.
   6 |  * http://www.opensource.org/licenses/mit-license
   7 |  */
>  8 | var _pixi_htmltext=function(t,e,i,o,s,r,n){"use strict";var h=function(t){function e(e,r,n){if(void 0===e&&(e=""),void 0===r&&(r=

Here's my import code:

import * as PIXI from "pixi.js";
import { HTMLText } from "@pixi/text-html";

When loading the library manually, window.PIXI.HTMLText is available as intended:

import("pixi.js")
   .then((pixi) => {
      window.PIXI = pixi
      import("@pixi/text-html")
   })
.catch((error) => console.error("Couldn't load PIXI", error));

Peer dependencies

Please, use ">=" instead "^" for module version. It cause to update to latest version of pixi.js modules.
As example after lock pixi.js to 6.3.2 version i will receive type error, because modules in @pixi which needed for html-text are updated to 6.4.2 version.

Is there a way to make CSS transitions work?

I tried the obvious solution of simply adding transition: all 0.2s to the style, but that doesn't seem to work.
I feel like it's not compatible with the way the text is rendered, but is there a workaround or any other way to add an animated scale-in effect for example?

Resolution with this.stage.scale

We have custom logic to detect both browser zoom (window.devicePixelRatio) and tool bar that controls (this.stage.scale).

When scale event change, we apply resolution as (scale * devicePixelRatio), which looks fine with regular Pixi Text, however, this plugin's resolution is not rendered correctly.

New HTMLText is added to the top-left corner of the webpage, is visible and can be interacted with

Using PixiJS 7, recently updated the text-html package from NPM, ran into this issue. Any HTMLText that gets created is put onto the webpage, is visible, and can be interacted with. I can highlight it and copy it, for example.

Picture of a PixiJS instance with an HTMLText on the stage; the HTMLText appears twice, once on the stage and once outside the borders of the stage. The text outside the stage is highlighted.

Multiple HTMLText on the stage will add them all to the top-left corner of the website. This gets very weird.

Picture of another PixiJS instance. A box of choices is on the stage, and each chose is an HTMLText instance. Each of them also appears over each other in the top-left corner. The text for the fifth choice, "5. Signing the same old song", is highlighted in the top left corner.

A minimum reproduction example (this generated the result in the first image):

import { Application } from 'pixi.js';
import { HTMLText } from '@pixi/text-html';

const canvasElement = document.getElementById("pixi-canvas") as HTMLCanvasElement;

const options = {
	view: canvasElement,
	resolution: window.devicePixelRatio || 1,
	autoDensity: true,
	backgroundColor: "skyblue",
	width: 1280,
	height: 720,
};

const app = new Application(options);

app.stage.addChild(new HTMLText("HTMLText gets added to the DOM, is visible, can be interacted with", {
	fill: "white",
}))

The fill being white isn't necessary. Black HTMLText will still get added to the top left corner, even if I can't see it.

This issue arose on Chrome and Firefox on both Windows and Android.

White screen appears when updating text

Currently I use the rich text editor quill.js to generate the html, and then update it to the text part of the HTMLText.
But now I have the problem that when the text is updated the whole HTMLText object will be white screened and then updated to the new content.
The expectation is to update the text without a white screen even if the update is slower.

incorrect layout compute in safari

in safari, the uses floored width instead of using decimals, which may cause some layout problems.

image
image
image

to avoid it I recommend ceil the computed width and height.

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.