DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Coding
  3. JavaScript
  4. Introduction to CreateJS – Part 1

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.

Swathi Prasad user avatar by
Swathi Prasad
·
May. 01, 17 · Tutorial
Like (0)
Save
Tweet
Share
8.32K Views

Join the DZone community and get the full member experience.

Join For Free

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:

stage.enableMouseOver();

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.graphics.beginFill(createjs.Graphics.getRGB(0,255,255));

circle.graphics.drawCircle(0,0,40);

circle.x = stageWidth/2;

circle.y = stageHeight/2;

circle.on("pressmove", pressHandler);

stage.addChild(circle);


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;

stage.update();

}


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;

stage.addChild(text);


Draggable-shapes

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.

Object (computer science) JavaScript library

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Important Data Structures and Algorithms for Data Engineers
  • Understanding and Solving the AWS Lambda Cold Start Problem
  • Java REST API Frameworks
  • DevOps vs Agile: Which Approach Will Win the Battle for Efficiency?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: