Introduction to CreateJS – Part I

Recently I have been attempting to build casual games, I came across JavaScript library CreateJS, which provides a suite of modular libraries that work together or independently to enable interactive content via HTML5. The modular libraries are:

  1. EaselJS
  2. TweenJS
  3. SoundJS
  4. PreloadJS

This article is first of the CreateJS series and today, let’s look at EaselJS and how it can be used for creating some draggable shapes on Canvas. EaselJS works with HTML Canvas elements and is useful for creating games, graphical experiences and generative art.

Creating drag and drop shapes on canvas using EaselJS

First, let’s define a canvas element in HTML for drawing shapes later in JavaScript. I will hook init() onto body onload event, which is going to initialize our entire work.

<body onload="init()">

<canvas id="canvas"></canvas>


In the HTML’s header, include EaselJS library either through CDN or by downloading source code from GitHub’s repository.

Some initial work

Before creating shapes in JavaScript, I will first scale the canvas to occupy width and height of the browser window’s viewport. In init(), I will write the following piece of code.

var canvas = document.getElementById("canvas");

canvas.height = window.innerHeight;

canvas.width = window.innerWidth;

This code ensures the canvas resize to browser window’s view port on the initial page load.

To draw shape on the canvas, first we will need to create a Stage object. Stage is the root level container for display objects. We will pass the canvas object, which we created earlier. This canvas object will be used by the Stage object for the rendering display objects.

var stage = new createjs.Stage(canvas);

Note that, CreateJS provides a collection of classes that are shared across all the CreateJS libraries. These classes are included in the minified files of each library and are available on the createjs namespace.

We will enable mouse over for the stage’s display objects. This can be achieved through:


Drawing geometrical shapes on canvas

The geometrical shapes can be created by using Shape object of EaselJS library. This Shape allows us to display vector graphics in the canvas. Each shape has a Graphics instance which is the interface to the canvas drawing system.

circle = new createjs.Shape();,255,255));,0,40);

circle.x = stageWidth/2;

circle.y = stageHeight/2;

circle.on("pressmove", pressHandler);


drawCircle() of the graphics object creates the circle shape for us. Similarly, Graphics object provides several functions to create shapes. Take a look at the official documentation to see how to create different shapes. Once we create a shape, we add it to the Stage object and call shape.update() will redraw the canvas. For other geometrical shapes, refer the code on GitHub.

As you will notice, I have registered presshandler function with pressmove event for all the shapes. This function will update the co-ordinates of shapes at the touch point and redraws the canvas.

function pressHandler(e) { = e.stageX; = e.stageY;



Working with text

In my demo, I have added a text above the shapes. This can be done by Text objects of EaselJS. It allows you to change color, alignment, size, font, wrapping and other settings of Text objects.

var text = new createjs.Text("Drag & drop these shapes", "30px Arial", "#000000");

text.x = stageWidth/2 - text.getMeasuredWidth()/2;

text.y = 80;



Wrapping Up

Refer the GitHub repository for complete source code and demo is available here.  Other tutorials can be found on CreateJS website. Thank you for taking the time to read this article.

One thought on “Introduction to CreateJS – Part I

Comments are closed.