Introduction to CreateJS – Part 1

DZone 's Guide to

Introduction to CreateJS – Part 1

In this article, we introduce the JavaScript library suite known as CreateJS, and show you how to make some basic animations using one of it's component libraries.

· Web Dev Zone ·
Free Resource

Recently I have been attempting to build casual games. I came across the 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 the 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, graphics-based experiences, and generative art.

Creating Drag and Drop Shapes on Canvas Using EaselJS

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

<body onload="init()">

In the HTML’s header, include the 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 resizes to the browser window’s viewport on the initial page load.

To draw a 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 of 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 the mouse-over functionality for the stage’s display objects. This can be achieved through the following line of code:


Drawing Geometrical Shapes on Canvas

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

circle = new createjs.Shape();



circle.x = stageWidth/2;

circle.y = stageHeight/2;

circle.on("pressmove", pressHandler);


drawCircle() of the graphics object, creates the circle shape for us. Similarly, the 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() which will redraw the canvas. For other geometrical shapes, refer to the code on GitHub.

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

function pressHandler(e) {

e.target.x = e.stageX;

e.target.y = 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 to the GitHub repository for complete source code and a demo is available here. Other tutorials can be found on the CreateJS website. Thank you for taking the time to read this article.

javascript, javascript libraries, web dev

Published at DZone with permission of Swathi Prasad , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}