CreateJS is a suite of modular libraries and tools that simplify HTML5 canvas development. It provides a consistent, easy-to-use API for working with shapes, text, images, video, and more, abstracting away many of the complexities of the underlying Canvas API. The libraries are designed to be lightweight, performant, and cross-browser compatible, making them suitable for a wide range of projects, from simple animations to complex interactive applications. CreateJS includes several key libraries: EaselJS (for display and animation), TweenJS (for animation), SoundJS (for audio playback), and PreloadJS (for asset loading).
CreateJS offers several compelling reasons for choosing it as your HTML5 canvas framework:
Setting up your development environment for CreateJS is straightforward. You primarily need:
A Text Editor or IDE: Choose your preferred code editor (e.g., VS Code, Sublime Text, Atom) or an Integrated Development Environment (IDE) like WebStorm.
A Web Browser: Modern browsers (Chrome, Firefox, Safari, Edge) all support the HTML5 Canvas API and will work with CreateJS.
The CreateJS Libraries: Download the CreateJS libraries from the official CreateJS website or use a CDN (Content Delivery Network) to include them in your project. CDNs provide quick access and avoid the need for local downloads. An example using a CDN is shown below:
<script src="https://code.createjs.com/1.0.0/createjs.min.js"></script>
Remember to replace 1.0.0
with the desired CreateJS version.
An HTML File: Create an HTML file to structure your project. This file will include the CreateJS libraries and your JavaScript code.
A JavaScript File (Optional): For larger projects, it’s recommended to keep your CreateJS code in a separate JavaScript file for better organization and maintainability.
CreateJS consists of several core libraries, each focusing on a specific aspect of canvas development:
EaselJS: This is the core library for managing the display list, creating and manipulating shapes, text, bitmaps, and containers. It handles drawing, transformations, and event handling.
TweenJS: This library provides powerful and flexible tweening capabilities, allowing you to animate properties of EaselJS objects smoothly over time. You can control timing, easing functions, and multiple chained tweens.
SoundJS: This library simplifies audio playback and management. It provides cross-browser compatibility and handles loading, playing, and controlling audio files.
PreloadJS: This library manages the loading of assets such as images, sounds, and other files, providing progress events and error handling to ensure smooth loading of your application’s resources.
Each library is self-contained and can be used independently, or in combination with the others to create complex and interactive applications. Understanding the functionality of each library is crucial for effective CreateJS development.
EaselJS uses a Stage
object as the root of the display list. The stage represents the canvas element where everything is drawn. All display objects, including shapes, bitmaps, text, and containers, are added to the stage. Containers are crucial for organizing and managing complex scenes; they act as invisible holders for other display objects, allowing you to group and manipulate them together.
Shapes are created using classes like Shape
, Circle
, Rectangle
, and Ellipse
. You define their properties (like color, fill, stroke) and add them to the stage or a container. For complex shapes, you can use the graphics
API to draw paths.
Example of creating a simple circle and adding it to the stage:
let stage = new createjs.Stage("myCanvas"); // "myCanvas" is the ID of your canvas element
let circle = new createjs.Shape();
.graphics.beginFill("red").drawCircle(50, 50, 30);
circle.addChild(circle);
stage.update(); stage
EaselJS allows you to easily display images using the Bitmap
class. You create a Bitmap
object by providing an image URL or an existing HTMLImageElement. Once added to the stage, bitmaps can be scaled, rotated, and repositioned like other display objects. For optimal performance, preload images using PreloadJS before adding them to the stage.
Example of loading and displaying a bitmap:
let image = new Image();
.onload = function() {
imagelet bitmap = new createjs.Bitmap(image);
.addChild(bitmap);
stage.update();
stage;
}.src = "myImage.png"; image
EaselJS provides the Text
class for rendering text on the canvas. You can control font size, family, color, and alignment. You can also use rich text with HTML-like formatting, though this may affect rendering performance for large amounts of text. For improved performance with multiple lines of text, consider using a TextField
instead.
Example of creating and adding text:
let text = new createjs.Text("Hello, world!", "20px Arial", "#000000");
.x = 100;
text.y = 100;
text.addChild(text);
stage.update(); stage
EaselJS makes event handling straightforward. You can add event listeners to display objects to respond to mouse events (click, mouseover, mouseout, etc.) and stage events. The addEventListener
method is used to attach listeners.
Example of adding a click event listener to a shape:
.addEventListener("click", function(event) {
circleconsole.log("Circle clicked!");
; })
Animation in EaselJS is typically achieved by repeatedly updating the properties (position, rotation, scale) of display objects within a tick
event handler. The Ticker
class provides a consistent update loop. You modify the object’s properties in each tick, creating the illusion of movement.
Example of basic animation using the Ticker:
.Ticker.addEventListener("tick", handleTick);
createjsfunction handleTick(event) {
.x += 2; // Move the circle 2 pixels to the right each frame.
circle.update(event);
stage }
TweenJS offers a powerful and efficient way to create smooth animations. It handles transitions between property values over a specified duration, using various easing functions. Timers provide a simple way to execute functions at specified intervals.
Example of a simple tween:
.Tween.get(circle).to({x: 300, y: 200}, 1000, createjs.Ease.linear); // Move circle over 1 second. createjs
The EaselJS display list is a hierarchical structure of display objects. Efficient organization is crucial for performance, especially in complex scenes. Minimize the number of objects on the stage, group related objects into containers, and use caching strategically to improve rendering speeds. Avoid excessive use of rich text and unnecessary updates to improve performance further. Understanding the display list structure and using optimization techniques is essential for creating high-performance applications.
TweenJS simplifies the creation of animations by providing methods to smoothly transition the properties of objects over time. The core function is createjs.Tween.get()
, which takes a target object and allows you to define the properties to animate, their target values, and the duration of the animation.
A basic example of tweening an object’s x
and y
properties:
let circle = new createjs.Shape();
// ... (add circle to stage) ...
.Tween.get(circle)
createjs.to({ x: 200, y: 100 }, 1000); // Move to (200, 100) over 1000 milliseconds (1 second)
This code will smoothly animate the circle
’s position from its current location to (200, 100) over one second. You can specify multiple properties within the to()
method.
Easing functions control the speed and timing of the animation. TweenJS provides a range of built-in easing functions (e.g., createjs.Ease.linear
, createjs.Ease.quadIn
, createjs.Ease.cubicOut
, etc.), each creating a different animation curve. These control the rate of change of the animated property over time, affecting the perceived smoothness and character of the animation.
Example using a different easing function:
.Tween.get(circle)
createjs.to({ x: 200, y: 100 }, 1000, createjs.Ease.backOut); // Uses a bounce-back effect.
Tweens can be chained together to create complex animations with multiple stages. Each .to()
call adds a new tween segment to the animation sequence. You can define different properties and durations for each segment.
Example of chaining tweens:
.Tween.get(circle)
createjs.to({ x: 200, y: 100 }, 1000, createjs.Ease.quadOut)
.to({ x: 400, y: 200, alpha: 0 }, 500, createjs.Ease.sineIn); // Fade out while moving
This code moves the circle to (200, 100) using a quadratic easing, then to (400, 200) while fading it out using a sine easing.
TweenJS is capable of animating various properties beyond simple position. You can animate alpha (opacity), scale, rotation, color, and even custom properties. For more complex animations involving multiple objects or coordinated movements, you may need to use callbacks or event listeners to synchronize actions. Consider structuring your code efficiently, perhaps by grouping animations using containers, for better organization.
Example animating multiple properties:
.Tween.get(circle)
createjs.to({ x: 200, y: 100, scaleX: 2, scaleY: 2, alpha: 0.5 }, 1000);
TweenJS provides events that allow you to execute functions at specific points in an animation’s lifecycle (e.g., change
, complete
). This allows for precise control and the triggering of other actions based on animation progress. Event listeners are added using addEventListener()
.
Example of using a complete
event:
.Tween.get(circle)
createjs.to({ x: 200, y: 100 }, 1000)
.addEventListener("change", function(event) {
console.log("Tween is changing");
}).addEventListener("complete", function(event) {
console.log("Tween is complete");
// Perform additional actions here
; })
The change
event fires during the animation, and the complete
event fires when the animation concludes. Other useful events include paused
and resume
.
SoundJS simplifies the process of playing audio files in your CreateJS applications. The core class is createjs.Sound
. Before playing sounds, you must register them using createjs.Sound.registerSound()
, which takes a path to the sound file. Once registered, sounds can be played using createjs.Sound.play()
.
Example of playing a sound:
// Preload the sound (recommended for better performance)
.Sound.registerSound("./mySound.mp3", "mySound");
createjs
// Play the sound
.Sound.play("mySound"); createjs
Ensure that the sound file exists at the specified path and that it’s in a supported format (MP3, Ogg, WAV are commonly supported).
SoundJS can handle both short sound effects and longer music tracks. The distinction is primarily in how you manage their playback and potential looping. Short sound effects are typically played once, while music tracks may loop continuously or play for a specific duration.
Example of playing a looping music track:
.Sound.registerSound("./myMusic.mp3", "myMusic");
createjslet instance = createjs.Sound.play("myMusic", {loop: -1}); // -1 loops indefinitely
You can stop the music using instance.stop();
where instance
is the reference to the sound instance obtained from createjs.Sound.play()
.
SoundJS provides various methods to control sound playback:
pause()
: Pauses the currently playing sound.resume()
: Resumes a paused sound.stop()
: Stops the sound and resets its position.setVolume()
: Adjusts the volume of a playing sound.setPosition()
: Sets the playback position (in milliseconds).getVolume()
: Retrieves the current volume.getPosition()
: Retrieves the current playback position.These methods operate on the sound instance returned by createjs.Sound.play()
.
Audio sprites are a technique for storing multiple sounds within a single audio file. SoundJS supports audio sprites by specifying the start and end times of each sound within the file. This is useful for optimizing loading times and reducing the number of individual audio files.
Example using an audio sprite:
// Preload the sprite sheet.
.Sound.registerSound("./soundSprite.mp3", "soundSprite");
createjslet soundManifest = [
id: "sound1", src: "soundSprite.mp3", data:{offset:0, duration: 1000}}, // 1-second sound
{id: "sound2", src: "soundSprite.mp3", data:{offset:1000, duration: 500}} // 0.5-second sound
{;
].Sound.registerManifest(soundManifest);
createjs//Play the audio sprites
.Sound.play("sound1");
createjs.Sound.play("sound2"); createjs
Note: offset
is in milliseconds.
SoundJS provides mechanisms for handling errors and events during audio playback:
Error Handling: SoundJS can throw errors if a sound file fails to load or if there’s a problem during playback. Implement try...catch
blocks to handle potential exceptions.
Event Handling: Events such as complete
(sound finished playing) or interrupt
(sound interrupted by another) can be listened for using addEventListener()
.
Example of adding an event listener:
let instance = createjs.Sound.play("mySound");
.addEventListener("complete", handleSoundComplete);
instance
function handleSoundComplete(event) {
console.log("Sound finished playing!");
}
Proper error and event handling ensures robust audio management in your application. Remember to handle potential issues gracefully to improve the user experience.
PreloadJS is a powerful library for managing the loading of assets in your CreateJS applications. It handles the asynchronous loading of various asset types (images, sounds, text files, JSON data, etc.), providing a structured and efficient way to manage the loading process. The core class is createjs.LoadQueue
. You create a LoadQueue
instance and add files to it using the loadManifest()
method, which takes an array of file specifications.
Example of loading images:
let queue = new createjs.LoadQueue(true); // true enables file progress events
.loadManifest([
queueid: "image1", src: "image1.jpg"},
{id: "image2", src: "image2.png"}
{;
])
.on("complete", handleComplete, this);
queue
function handleComplete() {
let image1 = queue.getResult("image1");
let bitmap1 = new createjs.Bitmap(image1);
// ... add bitmap to stage ...
}
This code creates a LoadQueue
, loads two images, and uses an event listener to execute handleComplete()
after the loading is finished, where queue.getResult("image1")
retrieves the loaded image.
PreloadJS provides progress events that allow you to monitor the loading status of your assets. The progress
event fires periodically, giving you the percentage of completed loading. The complete
event indicates that all assets have been loaded successfully.
Example of monitoring progress:
.on("progress", handleProgress, this);
queue.on("complete", handleComplete, this);
queue
function handleProgress(event) {
console.log("Loading progress: " + Math.round(event.progress * 100) + "%");
}
function handleComplete() {
console.log("All assets loaded!");
}
This code adds event listeners to track both progress and completion.
PreloadJS provides mechanisms for handling errors during asset loading. The error
event fires when an asset fails to load. The event object provides information about the failed asset.
Example of handling errors:
.on("error", handleError, this);
queue
function handleError(event) {
console.error("Error loading asset:", event.item);
}
This code adds an error handler to gracefully manage asset loading failures.
For larger projects, it’s often beneficial to use manifest files (typically JSON files) to define the assets to be loaded. Manifest files provide a centralized and organized way to specify asset paths, IDs, and other metadata. PreloadJS can load assets directly from a manifest file.
Example manifest file (manifest.json
):
[
{id: "image1", src: "image1.jpg"},
{id: "sound1", src: "sound1.mp3"},
{id: "data1", src: "data1.json"}
]
Example loading from a manifest:
let queue = new createjs.LoadQueue(true);
.loadManifest("./manifest.json");
queue// ... add event listeners ...
PreloadJS offers advanced features for optimizing asset loading:
priority
property in the manifest. Higher priority assets are loaded first.crossOrigin
property allows you to load assets from different domains.By understanding these advanced techniques, you can optimize your asset loading strategy to enhance the performance of your CreateJS applications. Remember to always test and profile your asset loading procedures to ensure optimal efficiency for your target audience and devices.
The CreateJS Ticker
is a core utility class providing a consistent and high-performance mechanism for executing code at regular intervals, forming the basis for animation and other time-based events. The Ticker
dispatches “tick” events, which are crucial for updating the display and performing animations in EaselJS and other CreateJS libraries. It’s designed to be highly efficient and cross-browser compatible, handling inconsistencies in browser timing mechanisms.
The Ticker
can be used in different modes:
Ticker.framerate = 60;
: Sets a target frames-per-second (fps) rate. The Ticker
will attempt to maintain this rate, but performance limitations may cause the actual frame rate to vary.
Time-based updates: The Ticker
’s time
property provides the elapsed time in milliseconds since the last tick, allowing for time-based animations and physics calculations. This approach is generally preferred for smoother and more consistent results.
Event listeners: You add listeners to the Ticker
to execute code on each tick. The tick
event provides an object with details such as the current time and delta time.
Example using the Ticker
for animation:
.Ticker.addEventListener("tick", handleTick);
createjs
function handleTick(event) {
// Update game state based on the time elapsed since the last tick (event.delta)
.x += 5; // Example: move an object 5 pixels
myObject.update(event); // Update the display
stage }
The stage.update(event)
call is crucial to refresh the canvas display after each update.
The createjs.EventDispatcher
class is a fundamental utility for managing events throughout CreateJS. Many CreateJS objects (including the Stage
, DisplayObject
, LoadQueue
, and SoundInstance
classes) inherit from EventDispatcher
, providing a consistent event handling mechanism. This promotes modularity and provides a clean pattern for managing interactions.
Key functionalities include:
addEventListener(type, listener, [scope], [once])
: Attaches an event listener to the dispatcher. type
specifies the event type (e.g., “tick”, “complete”, “click”); listener
is the function to execute; scope
(optional) specifies the this
context for the listener; once
(optional, boolean) removes the listener after it’s executed once.
removeEventListener(type, listener, [scope])
: Removes an event listener.
dispatchEvent(event)
: Dispatches an event. This is frequently used internally by CreateJS objects but can also be utilized to create custom events.
hasEventListener(type)
: Checks if there are any listeners for a specific event type.
Example using EventDispatcher
:
let myObject = new createjs.EventDispatcher();
.addEventListener("myCustomEvent", handleMyEvent);
myObject
function handleMyEvent(event) {
console.log("My custom event triggered!", event);
}
.dispatchEvent({type: "myCustomEvent", data: "some data"}); myObject
This example shows creating a custom event and attaching a listener to handle it. The EventDispatcher
provides a structured and powerful way to manage events within and between different CreateJS components. Understanding its use is crucial for building more complex and interactive applications.
Optimizing performance is crucial for creating smooth and responsive CreateJS applications, especially when dealing with complex scenes or animations. Key strategies include:
Minimize Display Objects: Reduce the number of objects on the stage. Group similar objects into containers to decrease the number of individual objects that need to be rendered.
Caching: Use cache()
on display objects to improve rendering speed. Caching creates a bitmap representation of the object, improving drawing performance. However, be mindful that frequently updating cached objects can negate the performance benefits.
Update Only When Necessary: Avoid unnecessary updates to the stage using stage.update()
. Only update when the display needs to be refreshed. This is particularly important for animations, where frequent updates can impact performance.
Efficient Animation Techniques: Use TweenJS for smooth animations and avoid excessive calculations within the tick
handler. Consider using techniques like object pooling to reuse objects instead of constantly creating and destroying them.
Reduce Rich Text Use: Rich text can impact performance; consider using plain text whenever possible for large amounts of text.
Image Optimization: Optimize images for web use (e.g., using appropriate formats and compression). Preload images using PreloadJS to prevent visual hiccups.
Profiling: Use browser developer tools to profile your application and identify performance bottlenecks. This allows you to focus your optimization efforts on the most impactful areas.
Debugging CreateJS applications often involves standard JavaScript debugging techniques, but specific CreateJS considerations include:
Browser Developer Tools: Use your browser’s developer tools (Console, Network, and Profiler) to inspect the application’s behavior, identify errors, and profile performance.
Logging: Use console.log()
strategically to track variable values and the execution flow of your code.
Inspecting the Stage: Use the browser’s developer tools to inspect the stage and its children to understand the structure of your display list. This can help identify unexpected object positions or hierarchies.
Error Handling: Implement proper error handling in your code to catch and address potential issues during asset loading, animation, or event handling.
CreateJS is designed to work well with other JavaScript libraries. Common integration points include:
Game Engines: Integrate CreateJS with game engines like Phaser or PixiJS for enhanced functionality.
Physics Engines: Use physics engines like Matter.js or Box2D for realistic physics simulations in your applications.
UI Libraries: Integrate UI libraries for building richer user interfaces.
Remember to consider potential conflicts between libraries and ensure proper initialization and compatibility.
CreateJS provides a strong foundation for building interactive applications. Effective strategies include:
Event Handling: Utilize the EventDispatcher
effectively to manage user interactions (mouse clicks, keyboard presses, touch events).
State Management: Implement a clear state management system to track the application’s status and respond to user input accordingly. Consider using state machines or similar design patterns.
User Interface (UI) Design: Design a well-structured and intuitive user interface for improved usability.
Game Loop/Animation Loop: Use the Ticker
to manage the application’s main loop, updating game state and rendering the display.
Fetching and using external data (from JSON APIs, databases, etc.) involves:
Data Fetching: Use fetch
, XMLHttpRequest, or libraries like jQuery or Axios to retrieve data from external sources.
Data Parsing: Parse the received data (often JSON) into usable formats using JavaScript’s JSON.parse()
.
Data Integration: Integrate the parsed data into your CreateJS application, updating objects, animations, or other components.
Accessibility is crucial for inclusive application design. Consider these points:
Keyboard Navigation: Ensure that users can navigate and interact with the application using only the keyboard.
Screen Readers: Use semantic HTML and ARIA attributes to provide screen reader support.
Alternative Text: Provide alternative text (alt
attributes) for images to describe their content for users who cannot see them.
Color Contrast: Maintain sufficient color contrast between text and background colors for readability.
Modular Design: Break down your code into smaller, reusable modules.
Code Comments: Write clear and concise comments to explain your code’s functionality.
Version Control: Use a version control system (like Git) to manage your code changes.
Testing: Write unit tests to verify the functionality of individual components.
Consistent Code Style: Follow a consistent code style to improve readability and maintainability.
Performance Monitoring: Regularly monitor the performance of your application and optimize as needed. Keep abreast of performance optimization techniques relevant to CreateJS and the technologies you integrate it with.
This section provides a starting point for exploring the capabilities of CreateJS through various examples and tutorials. More extensive examples and tutorials can be found on the official CreateJS website and community resources.
Simple animations are a great way to start learning CreateJS. These examples focus on basic animation principles using EaselJS and TweenJS. They often involve animating the position, scale, rotation, or alpha (opacity) of simple shapes or bitmaps.
Example: A bouncing ball animation can be created by using TweenJS to repeatedly change the ball’s vertical position, applying an easing function to simulate the bounce. This would involve setting up a Ticker
event listener to update the ball’s position in each tick and using createjs.Tween.get()
to control the animation’s timing and easing.
Interactive games showcase the capabilities of CreateJS for creating engaging user experiences. These examples usually demonstrate the use of event handling, collision detection, and game logic.
Example: A simple “Breakout” clone could be developed using EaselJS to render the bricks, paddle, and ball. TweenJS would handle the ball’s movement, and event listeners would respond to mouse or touch input for paddle control. Collision detection would be implemented to handle ball-brick and ball-paddle interactions, managing score and game state.
Complex visualizations demonstrate how CreateJS can handle large datasets and sophisticated graphics. These examples highlight the importance of performance optimization techniques.
Example: A data visualization displaying stock prices over time could use EaselJS to create a line graph. The graph would dynamically update based on data fetched from an external source. Efficient techniques would be employed to handle large datasets smoothly. This might involve techniques like data chunking or using containers to manage the visual elements efficiently. Consider the use of efficient data structures and algorithms to optimize the update process.
Case studies offer in-depth looks at real-world applications built with CreateJS. These provide insight into architectural decisions, problem-solving approaches, and best practices.
Example: A case study could analyze the implementation of a web-based interactive map using CreateJS. The study would discuss how assets were managed (e.g., using PreloadJS), how map interactions were handled, and strategies employed for optimizing performance when displaying large maps. It might highlight challenges encountered and the solutions implemented. It could also include performance benchmarks and user feedback.
These examples and tutorials are intended as starting points. Experimenting with different techniques and expanding on these basic concepts will help you master CreateJS and build sophisticated applications. Remember to refer to the official CreateJS documentation and community resources for more detailed information and inspiration.
This glossary defines key terms used throughout the CreateJS documentation.
Stage
. Efficient organization of the display list is crucial for performance.Ticker
dispatches tick
events.A comprehensive API reference provides detailed information about all classes, methods, and properties within the CreateJS libraries. This reference typically includes:
The location of the complete API reference will vary depending on the version of CreateJS and the preferred method of access (e.g., online documentation, locally downloaded files).
This section addresses frequently encountered problems and provides solutions. Common issues may include:
Canvas not rendering: Check that your canvas element is correctly added to the HTML and that you are referencing it properly in your JavaScript code. Ensure that your script is loading correctly and that there are no JavaScript errors preventing execution.
Assets not loading: Verify that the asset paths specified in your manifest or loadManifest()
calls are correct and that the assets exist at the specified locations. Use the browser’s developer tools to inspect network requests and check for any errors.
Animations not working: Ensure that the Ticker
is properly configured and that the stage.update()
method is called in each tick
event handler. Double-check that your tween configurations are correct and that the target objects are added to the stage.
Event listeners not firing: Ensure event listeners are added correctly using addEventListener()
, with the proper event type and callback function. Check the event bubbling and capturing phases to make sure the event is reaching your listener.
Performance problems: Optimize your code using techniques discussed in the “Performance Optimization” section. Use the browser’s developer tools to profile your application to pinpoint performance bottlenecks.
The CreateJS community offers various resources for support and collaboration:
Official CreateJS Website: The official website provides up-to-date documentation, examples, and release notes.
Online Forums and Communities: Search for CreateJS forums and online communities where developers discuss issues, share solutions, and assist each other.
GitHub Repository: The CreateJS GitHub repository contains the source code and issue tracker. You can report bugs, suggest features, and contribute to the project’s development.
Stack Overflow: Search Stack Overflow for answers to frequently asked questions. When asking for help, provide detailed information about your issue, including relevant code snippets.
Engaging with the community is a valuable way to resolve issues, learn best practices, and stay informed about updates and developments in CreateJS.