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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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
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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Ultimate Guide to FaceIO
  • Cutting-Edge Object Detection for Autonomous Vehicles: Advanced Transformers and Multi-Sensor Fusion
  • Writing DTOs With Java8, Lombok, and Java14+
  • Graph API for Entra ID (Azure AD) Object Management

Trending

  • Designing AI Multi-Agent Systems in Java
  • A Guide to Using Amazon Bedrock Prompts for LLM Integration
  • Knowledge Graph Embeddings and NLP Innovations
  • Power BI Embedded Analytics — Part 3: Power BI Embedded Demo
  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 user avatar
Swathi Prasad
·
May. 01, 17 · Tutorial
Likes (0)
Comment
Save
Tweet
Share
9.1K 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.

Related

  • Ultimate Guide to FaceIO
  • Cutting-Edge Object Detection for Autonomous Vehicles: Advanced Transformers and Multi-Sensor Fusion
  • Writing DTOs With Java8, Lombok, and Java14+
  • Graph API for Entra ID (Azure AD) Object Management

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!