Author's Posts

This week I made some contributions to EaselJS project. These contributions were simple pull requests to the official repository, but I’ve never did such things on Github or other version control systems, so it was a bit confusing. This post just register the steps I took to do this.

1. Create a fork of the project you want to contribute. This can be made on github interface:

github_fork

2. Clone the forked repository, for example:

git clone git@github.com:<username>/EaselJS.git

3. Create a new branch to make the changes. A branch is an isolated version of the project which will contain all modifications you made.

git checkout -b <branch_name>

4. Make the changes and commit it:

git add -A

git commit -m “<description>”

5. Push the changes to your forked project:

git push origin <branch_name>

6. Pull request the branch to the official repository. In the Github interface, all branches will created will be highlighted on the page of the forked repository together with a “Pull Request” button. Just press this button and send it.

You can send multiple pull requests, just adding new branches.

Read more

Few days ago, after all sprites for the player character, monsters and items were done. I was creating the background images for my game, and I notice how big they were. For example, the following image is the background of the first level and was originally saved in PNG, which resulted in 1.08MB. A whole megabyte is a lot for a single image for a simple web game, and it was in the resolution of 1024×768. The HD version of this image (2048×1536) had 2.54MB!

Dungeon Thief -Level 1 Background

These ridiculous file sizes are the result of a bad choice for the image format. An image format describes how the pixels of an image are represented. A format can trade the image quality to make the file size smaller (Lossy), or can save the image in the best quality but in the cost of the file size (Lossless). A format also can specify if the image will have a limited number of colors (Indexed) or it will have no limitation (Direct). Common image formats includes: BMP, GIF, JPEG, and PNG.

BMP is a Lossless format and can be Indexed or Direct. There is no compression at all in this format, resulting in very large file sizes. It is an old format and have no application nowadays.

GIF uses a Lossless compression which preserves the image data, but it is an Indexed format used for images with less than 256 colors, impacting on the image quality. GIF also accepts transparency, but only in 100% (totally transparent) or 0% (totally opaque) levels. It is a pretty good format for simple images with limited colors, such as logos and some simple icons.

JPEG (or JPG) is a Lossy and Direct format, preserving the colors but losing image data. This combination makes the format specially useful for rich and complex images such as photographies, where losing some data are not (or barely) noticeable.

The PNG format also uses a Lossless compression, resulting in files with much less size than BMP, and is not limited to a fixed number of colors, such as GIF, thus, resulting in high quality images. PNG accepts alpha transparency, i.e., the transparency may range from 0% to 100%. PNG was created to improve and replace the GIF format.

Given these descriptions and examples, we can summarize:

  • JPEG for complex and detailed artworks and photographies without transparency;
  • PNG for simple images such as sprites, icons, logos, and all images that need transparency;

The first image presented here, which had 1.08MB for 1024×768 and 2.54MB for 2048×1536, converted to JPEG (and keeping a good quality) resulted in 201KB and 336KB for the 1024 and 2048 resolutions, respectively. This show the importance of knowing the different formats and choosing the right one for your images.

References:

Read more

Inkscape LogoI love inkscape! It is a great tool to create and edit vector graphics. It is free, has a lot of features, has an intuitive interface and usability. It works in Windows, Linux or Mac. It can produce high-quality bitmaps. Do I have to say more?

Inkscape is one of that kind of programs that you must have to install in your computer, even if you don’t have plans to use it in a near future. This is my favorite tool to create graphics for my games or other visual applications, and in this post I will give you a little introduction of how you can do that too.

Before anything, download and install the last version of inkscape here.

Start with simple things, rounded ones. Create some circles, reshape them, apply some colors, try to do something without any help. Inkscape is a very intuitive tool, you will probably discover the important features by yourself.

There are several tutorials around the internet and you can follow them. My favorites are in the blog 2D Game Art for Programmer. Chris Hildenbrand, the author, presents basic and advanced tips to create characters, objects, backgrounds, among other elements for 2D games.

Recommended tutorials:

When you feel comfortable with inkscape features, try creating things by yourself. My suggestion is using references such as images and drawings, cartoons are a very good choice. Just remember you are creating a new art, do not copy anything, just use the references as references!

My final tip to you. I have no skills to choose colors and to painting my drawings. Most of you probably feel the same. To overcome this problem, what I do now is searching for color palettes or creating ones from images.

Sites with color palettes:

Sites to create your own color palettes:

Note: I will start to publish some tutorials here, stay sharp!

Read more

EaselJS is a Javascript library that allows the programmer to work on html canvas in an easy way. It means that EaselJS abstracts the low level API of the canvas element and provides classes, structures and functions to manipulate visual elements.

Before starting the examples and the talk about the Easel modules, let’s set up the html file which will be used as basis throughout this post. Create a file called easel.html and copy the following code:

<!DOCTYPE html>
<html>
<head>

<script src="http://code.createjs.com/easeljs-0.7.1.min.js"></script>

<script>
    function init() {
        var canvas = document.getElementById('canvas')
        var stage = new createjs.Stage(canvas);
        // CODE HERE!
    }
</script>
</head>
<body onLoad="init();">

<canvas id="canvas" width="600" height="300">alternate content</canvas>

</body>
</html>

 The Basics

EaselJS provides a whole architecture to control the objects that will be used at our games. The following list explains a bit of each one of the core components of this architecture.

  • EventDispatcher: the EventDispatcher class provides the structure for managing queues of events listeners and dispatching events. All components of EaselJS inherit from this class, thus, any component can handle events such as mouse clicks, drags, etc. For example, you can register a callback function to the ‘click’ event of a Bitmap object, causing the callback to be called whenever the user clicks that Bitmap.
  • DisplayObject: is the base class for all visual element, exposing properties such as position, rotation, scale and transparency. It means that, you can manipulate the position, rotation and other display properties of any object that draws something at the screen.
  • Container: The Container inherit from DisplayObject and provides methods to group other DisplayObjects. Using Containers, you can create hierarchy of visual objects and manipulate them together. For example, if you have a container within 3 images and move the container to the left, all the three images will be moved to the left too.
  • Stage: the Stage is the root level of the DisplayObject hierarchy, thus, if you want to show an object in canvas you need to add this object to the stage.

After adding objects to stage, you need to call the stage “update” method, otherwise the canvas will be redraw and you won’t see any change. All examples below show how to use it.

Working with Shapes

A Shape object stores a group of vector graphics that are created directly by the canvas element. Each shape has a Graphics instance which is the interface to the canvas drawing system. Check the example below.

var shape = new createjs.Shape();
shape.graphics.beginFill('#99f');
shape.graphics.drawRect(0, 0, canvas.width, canvas.height);

shape.graphics.beginFill('#33f');
shape.graphics.beginStroke('black');
shape.graphics.drawCircle(canvas.width/2, canvas.height/2, 50);

stage.addChild(shape);
stage.update();

Notice how the graphics instance does the hard work. The beginFill function sets the filling color for the next drawings while beginStroke sets the stroke color. The Graphics object provides several functions for the drawing itself, take a look at the official documentation to see how drawRect, drawCircle and other directives work.

Working with Texts

With Text objects you can draw… texts! But just plain texts, not HTML-formatted ones. EaselJS allows you to change the color, alignment, size, font, wrapping and others settings of Text objects, check the official documentation to know more:

var text = new createjs.Text('Hello, World!', '32px Arial', '#53f');
text.textAlign = 'center';
text.x = canvas.width/2;
text.y = canvas.height/2;

stage.addChild(text);
stage.update();

Working with Bitmaps

With Bitmap objects you can display an image, canvas or video (the HTML elements) into the canvas. You can instantiate the Bitmap object using the HTML element explicitly or passing a string, as shown in the example. Notice that, the first lines of the code below is a bit different from the examples above, that is because I am using a external resource (the image ‘assets/html5.png’) and we can use it before the browser totally load it. This process is automated by PreloadJS.

img = new Image();
img.src = 'assets/html5.png';
img.onload = function(event) {
    var bitmap = new createjs.Bitmap('assets/html5.png');
    bitmap.x = canvas.width/2 - bitmap.image.width/2;
    bitmap.y = canvas.height/2 - bitmap.image.width/2;

   stage.addChild(bitmap);
   stage.update();
}

Consult the documentation to know more about bitmaps:

 Working with SpriteSheets

A sprite sheet is a group of several images combined into a single larger file, e.g.:Bubbles Sprite SheetYou can also define animations using sprite sheets as shown in the example.

EaselJS provides the SpriteSheet class to handle this kind of image.  The SpriteSheet object requires some configuration to work, such as the size of each frame and the sequence of animations, check the documentation for a detailed description of each property:

img = new Image();
img.src = 'http://static.guineashots.com/tutorials/easeljs/assets/bubbles.png';
img.onload = function(event) {
    var data = {
        framerate: 10,
        images: [img],
        frames: {width:64, height:64, regX:32, regY:32},
        animations: {
            'explode': [0, 10],
        }
    }

    var spritesheet = new createjs.SpriteSheet(data);
    var animation = new createjs.Sprite(spritesheet, 'explode');
    animation.x = canvas.width/2;
    animation.y = canvas.height/2;

    stage.addChild(animation);

    createjs.Ticker.addEventListener("tick", update);
    function update(event) {
        stage.update();
    }
}

The SpriteSheet only represents the sequence of images, to see them as isolated images or animations you need a Sprite.

Notice that, in the last lines of example, I register a function “update” to the “tick” event, so it will be called every frame by Easel. In this function I call the stage update so it can redraw the canvas and show each frame of our animation.

Other Tutorials

To learn more about EaselJS, follow the official tutorials:

Read more