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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Ultimate Guide to FaceIO
  • Testing Business Programs Without Constructing Domain Objects
  • Momento Migrates Object Cache as a Service to Ampere® Altra®
  • Why I Ditched Redis for Cloudflare Durable Objects in My Rate Limiter

Trending

  • Why Role-Based Access Control Fails for AI Agents — and How to Fix It With Memory Lineage Engines
  • Designing Frontend Applications for Failure, Not Just Success
  • The Permission Was Valid. The Action Was Not Authorized.
  • Moving 900+ GB of Prometheus Without a Cutover Weekend

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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.

By 
Swathi Prasad
·
May. 01, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Ultimate Guide to FaceIO
  • Testing Business Programs Without Constructing Domain Objects
  • Momento Migrates Object Cache as a Service to Ampere® Altra®
  • Why I Ditched Redis for Cloudflare Durable Objects in My Rate Limiter

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook