EaselJS
repository·master·Indexed 27 days ago
https://github.com/createjs/easeljsA high-performance 2D library for HTML5 that provides a feature-rich display list for manipulating and animating graphics, games, and interactive content. Part of the CreateJS suite, it includes a robust interactive model for mouse and touch interactions and supports both Canvas and WebGL (via StageGL) rendering.
What's inside easeljs
- EaselJS is a high-performance 2D library for HTML5 that provides a feature-rich display list for manipulating and animating graphics. It includes a robust interactive model for mouse and touch interactions. It is suitable for games, generative art, ads, and data visualization. It has no external dependencies and is compatible with most frameworks. It integrates well with the CreateJS suite: SoundJS, PreloadJS, and TweenJS.
Run a local web server using Python
masterIf you have Python installed, you can quickly start a local web server for testing your EaselJS project. Navigate to your project's root directory in your terminal and run the following command:
python -m SimpleHTTPServerOnce started, your project will be accessible at
http://localhost:8000/.Export EaselJS stage or container to SVG using SVGExporter
masterThe
SVGExporterallows you to export an EaselJSStageorContainerto an SVG format. It supports most EaselJS features, including vector art, sprites, text, and bitmaps.Note: This feature is currently experimental and has not been extensively tested. For full technical details, refer to the documentation within the
SVGExporter.jssource file.Select the appropriate EaselJS library version
masterEaselJS provides several distribution files depending on whether you need stable releases, the latest development updates, or minified files for production.
- Use
easeljs.jsfor the most recent stable tagged version (useful for debugging). - Use
easeljs.min.jsfor the most recent stable tagged version, minified for deployment. - Use
easeljs-NEXT.jsfor the latest in-progress EaselJS classes. - Use
easeljs-NEXT.min.jsfor a minified version of the latest updates.
Note: WebGL support is provided via
StageGL, which is included in the minified source files.- Use
Use FauxCanvas to isolate EaselJS performance
masterYou can pass a
FauxCanvasinstance to acreatejs.Stageinstead of a standard HTML5<canvas>element. This is useful for performance profiling or benchmarking, as it eliminates the browser-specific overhead of drawing graphics to the screen, allowing you to isolate the time spent specifically within EaselJS logic.Note that
FauxCanvasis considered a rough implementation and may lack certain methods or properties required by specific EaselJS features.var stage = new createjs.Stage(new FauxCanvas(500, 400));Load EaselJS via CDN
masterTo benefit from faster load times and shared caching across sites, you can link to the EaselJS libraries hosted on the CreateJS CDN athttp://code.createjs.com/.Generate performance test reports
masterYou can generate structured reports from automated performance tests by appending the
reportparameter to the test URL. This sends the results to a specific report template (e.g.,table.html) for analysis.To run a test multiple times for each library version and output the results to a table, use the
autoparameter to specify the number of iterations and thereportparameter to specify the template name.myTest.html?auto=5&report=tableUse ScaleBitmap for scalable 9-slice rendering
masterUse
createjs.ScaleBitmapto render a bitmap texture using a 3x3 grid (often called a "Scale9" approach). This allows you to scale an image while preserving the integrity of its corners.How the scaling works:
- Corners: Rendered at 100% scale in their current container.
- Top and bottom edges: Stretched horizontally.
- Left and right edges: Stretched vertically.
- Center region: Stretched in both directions.
To use it, provide the image source and a
createjs.Rectanglethat defines the center region (x, y, width, height) of the grid. UsesetDrawSize(width, height)to define the final dimensions of the scaled shape.var sb = new createjs.ScaleBitmap(imagePathOrSrc, new createjs.Rectangle(10, 10, 80, 80)); sb.setDrawSize(newWidth, newHeight); stage.addChild(sb);Use Context2DLog to track Canvas method calls and property changes
masterThe
Context2DLogutility logs all method calls and property changes on aContext2Dobject. This is useful for debugging how EaselJS features translate into standard Canvas API calls or for identifying optimization opportunities.// setup: var myCanvas = document.getElementById("foo"); var logger = new Context2DLog(myCanvas); // enable or disable: logger.setEnabled(false); // implement custom logging: logger.logMethod = function(method, args, returned) { ... }; logger.logProperty = function(prop, oldVal, newVal) { ... };Use BitmapCache to improve rendering performance
masterBitmapCacheis used to render aDisplayObjectinto an image (a canvas or a WebGL texture) instead of re-rendering its complex parts every frame. This is highly effective for containers with many parts that do not change often.Key usage notes:
- Caching is a visual process. It is best used on containers, not single
Bitmapobjects. - A cached object will not visually update until
update()is explicitly called. - Caching is a prerequisite for applying certain filters efficiently.
WebGL vs Context2D:
- Use
options.useGL = 'stage'when working with aStageGLto use high-performanceRenderTextures(GPU-side textures). - Use
options.useGL = 'new'to create a newStageGLinstance for the cache. - If
useGLis undefined, it defaults to a standard Context2D canvas cache.
- Caching is a visual process. It is best used on containers, not single
Manage WebGL textures in StageGL
masterStageGL handles the loading and uploading of image data to the GPU. When an image is used in a
BitmaporSprite, StageGL manages its lifecycle within a texture batch. If an image is not yet loaded, StageGL attaches a load listener to update the texture data once the image is ready.Note on VRAM: If you encounter errors regarding texture creation, it is often due to exceeding available VRAM. Ensure you are releasing WebGL texture instances when they are no longer needed.
Generate SpriteSheets at runtime with SpriteSheetBuilder
masterThe
SpriteSheetBuilderclass allows you to generateSpriteSheetinstances at runtime from anyDisplayObject. This is useful for maintaining assets as vector graphics (low file size) and rendering them asSpriteSheetsfor better performance.Key Features:
- Supports both synchronous (
build()) and asynchronous (buildAsync()) builds. - Asynchronous builds use a
timeSliceto avoid locking the UI. - Frames can be added via
addFrame()or by passing aMovieClipviaaddMovieClip().
Configuration Properties:
maxWidth/maxHeight: Maximum dimensions for the generated images (default: 2048). Recommended to use powers of 2.scale: Scale applied to all frames (default: 1).padding: Padding between frames to preserve antialiasing (default: 1).timeSlice: Percentage of time (0.01 to 0.99) the builder uses per frame during async builds (default: 0.3).framerate: Framerate for the resultingSpriteSheet(default: 0, uses Ticker framerate).
- Supports both synchronous (