Giter Site home page Giter Site logo

sprites's Introduction

DEPRECATED

This library is no longer supported. We recommend using the Element Component to render 2D images in PlayCanvas


Rendering Sprites in PlayCanvas

Download sprite.js and upload it to your project or copy paste the code in a new script.

Add the script to an Entity with a Script Component. You will see the following script attributes:

  • textureAsset: This is the sprite that you want to render. If you want transparency for your sprite make sure that this is a .png file.
  • pos: This is the Vec2 (x,y) screen coordinate for your sprite.
  • size: This is the width and height as Vec2 of your sprite in pixels. For best results use the actual width of the uploaded image. Powers of 2 have better quality.
  • depth: This is the z-index of your sprite. If you want it to appear behind other sprites increase this value.
  • uPercentage: A value between [0,1] that specifies the maximum u value of the texture.
  • vPercentage: A value between [0,1] that specifies the maximum v value of the texture.
  • anchor: Determines where to anchor the sprite on the screen, for example top, center, bottom right etc.
  • pivot: Determines the alignment (or pivot point) of the sprite.
  • tint: A color to multiply the current color of the sprite with.
  • maxResHeight: Set this to the target resolution height of your app. The final scale of your sprite will be calculated as canvasHeight / maxResHeight.

Creating a UI with Sprites

Using the sprite.js script you can create a user interface for your application.

  • Create images using your favorite tool. For best quality sprites should be png files with power of 2 width and height.
  • Upload them to PlayCanvas.

For each one of your sprites:

  • Create an Entity
  • Add a script component to the Entity
  • Add sprite to script component
  • Click on textureAsset and pick the desired image
  • Set the rest of the fields to your liking
  • Launch the application

Buttons

You can attach an event handler for each sprite for the 'click' event. For example add this script on the same Entity as the sprite:

var MyScript = pc.createScript('myScript');

MyScript.prototype.initialize = function() {
    this.entity.script.sprite.on('click', this.onClick, this);
};

MyScript.prototype.onClick = function() {
    console.log('click');
};

That way you can have buttons that do something when you click on them.

Progress Bars

Check out this scene for an example on how to make progress bars using sprites and different anchors:

https://playcanvas.com/editor/scene/443447

Rendering Text

Check out this repository for scripts and details on how to render bitmap fonts: https://github.com/playcanvas/fonts

sprites's People

Contributors

daredevildave avatar liesislukas avatar maksims avatar vkalpias avatar willeastcott avatar

Stargazers

 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sprites's Issues

mobile / tablet click fire twice?

Hi when i do
this.LeftBtn.script.sprite.on('click', this.onLeftClick, this);

this will fire twice, thus making it navigate more than i want it too, for example from selection 3 got es to selection 2 when i click the left button.

Alpha / Opacity option

This could use an alpha or opacity option...is there one already?

Can we access the material to change the opacity on it some how? I'd prefer if I can change the opacity directly from the sprite though.

Thank you!

The 'click' event does not get dispatched on mobile

The onClick event never gets dispatched on mobile devices.

        initialize: function () {
            this.entity.script.sprite.on('click', this.onClick, this);
        },
        onClick: function(event){
            window.open('http://www.google.com/?some-click', '_blank');
        },

Should we be doing something like listening for touchend?

mouseover

Is it possible to have a mouseover event?

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.