Giter Club home page Giter Club logo

pixi-cocoontext's Introduction

PIXI-CocoonText

This is a plugin for PIXI v3.0.0 and higher. It adds a new text object optimized for mobile and specifically CocoonJS. However, CocoonJS is not required.

var foo = new PIXI.cocoontext.CocoonText("This is a text with a cache!",{font:"12px Arial"});

It is a modified version of the original PIXI.Text object with the following modifications:

  • Texture cache: when you create a PIXI.Text object, PIXI needs to use an offscreen canvas to generate your text and use this canvas as a texture. While this is the only way to do it for the moment, this can be a huge performance bottleneck when you need to create multiple texts in your game. CocoonText provides you an automated cache system to make sure that if you create the same text with the same style a second time, you won't have to create a new canvas/texture. This allows you to preload your texts at the start of your application like you could do for an image or a sound.

  • CocoonJS compatibility: the way PIXI.Text deals with text resolution is not compatible with CocoonJS v2.0. CocoonText is able to display high density texts for retina screens as long as you provide a font size with a px unit. You can set PIXI.cocoontext.CONST.TEXT_RESOLUTION to set a default resolution for all of your CocoonText objects. This can be useful if you want the best looking texts possible by setting it to the devicePixelRatio multiplied by the scale of your UI.

Typical use case

Building a button with an "active" and "inactive" state that changes the text style with preloaded textures

//I set the resolution of my texts to the devicePixelRatio * the scale of my stage
PIXI.cocoontext.CONST.TEXT_RESOLUTION = window.devicePixelRatio * stage.scale.x;

// While my game is loading, I generate the texts I will need later
function preloadTexts()
{
    var myTexts = [
        new PIXI.cocoontext.CocoonText("Homepage",{font:"14px Arial",fill:"#FFFFFF"}),
        new PIXI.cocoontext.CocoonText("Homepage",{font:"14px Arial",fill:"#FF0000"})
    ];
    for(var i =0;i<myTexts.length;i++)
    {
        myTexts[i].updateText();
    }
}

...

var buttonHomepage = new PIXI.cocoontext.CocoonText("Homepage",{font:"14px Arial",fill:"#FFFFFF"});
buttonHomepage.interactive = true;
buttonHomepage.tap = function(){
    //this will get the preloaded text already generated earlier
    this.style = {font:"14px Arial",fill:"#FF0000"};
};

pixi-cocoontext's People

Contributors

mathatan avatar jidw avatar psyrendust avatar

Watchers

James Cloos avatar  avatar

Forkers

edanchenkov

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.