### Start Local Python Web Server Source: https://github.com/createjs/easeljs/blob/master/README_SECURITY_ERROR.txt Use this command to start a simple HTTP server for local testing. Navigate to your project directory before running. ```bash python -m SimpleHTTPServer ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/createjs/easeljs/blob/master/build/README.md Installs all project-specific dependencies defined in the package.json file. Ensure you are in the build directory. ```bash cd /path/to/libraryName/build/ npm install ``` -------------------------------- ### Install Dependencies with npm Source: https://github.com/createjs/easeljs/blob/master/tests/README.md Before running tests, install project dependencies using npm. ```bash npm install ``` -------------------------------- ### EaselJS Setup and Asset Loading Source: https://github.com/createjs/easeljs/blob/master/extras/SVGExporter/SVGExport.html Initializes EaselJS, sets up a stage with a mask, and loads assets using a LoadQueue. The setup function is called upon completion of asset loading. ```javascript var c = createjs, stage, exporter; var queue = new c.LoadQueue(); queue.on("complete", setup); queue.loadManifest([ {id:"daisy", src:"daisy.png"}, {id:"bmpfont", src:"spritesheet_font.png"} ], true, "../../_assets/art/"); function setup() { stage = new c.Stage("test"); var mask = new c.Shape(); mask.graphics.beginFill("red").drawEllipse(0,0,500,800); stage.mask = mask; // this will cause Sprites to disappear in Illustrator, because sprites use masks, and AI doesn't import nested masks correctly var shape = new c.Shape(), g = shape.graphics; stage.addChild(shape); // ... rest of the drawing code ... } ``` -------------------------------- ### Initialize EaselJS Stage and Load Image Source: https://github.com/createjs/easeljs/blob/master/examples/AlphaMaskReveal.html Sets up the EaselJS stage, loads an image, and prepares text elements. This is the initial setup for the demo. ```javascript var stage; var isDrawing; var drawingCanvas; var oldPt; var oldMidPt; var image; var bitmap; var maskFilter; var cursor; var text; var blur; function init() { examples.showDistractor(); image = new Image(); image.onload = handleComplete; image.src = "../\_assets/art/flowers.jpg"; stage = new createjs.Stage("testCanvas"); text = new createjs.Text("Loading...", "20px Arial", "#FFF"); text.set({x: stage.canvas.width / 2, y: stage.canvas.height - 40}); text.textAlign = "center"; } ``` -------------------------------- ### Setup UI Elements Source: https://github.com/createjs/easeljs/blob/master/examples/WebGL/Runners.html Adds background elements, sprite containers for runners and sparkles, and a bitmap text header to the stage. ```javascript function setupUI(queue) { // background hill - just to demonstrate that bitmaps can be added to a StageGL: var hill1 = new createjs.Bitmap(queue.getResult("hill")); hill1.setTransform(345, 110, 3, 3); var hill2 = hill1.clone(); hill2.x -= 600; // SpriteContainers for runners and sparkles. // Each SpriteContainer specifies a SpriteSheet that all its descendants must share: runnerContainer = new createjs.Container(runnerSS); sparkleContainer = new createjs.Container(sparkleSS); // Bitmap Text header. // Again, the children must all share the SpriteSheet specified by the container. var topText = new createjs.BitmapText("CLICK TO DETONATE!", textSS); topText.setTransform(10, 10); stage.addChild(hill1, hill2, runnerContainer, topText, sparkleContainer); } ``` -------------------------------- ### Install Grunt CLI Source: https://github.com/createjs/easeljs/blob/master/build/README.md Installs the Grunt command line interface globally. This is a necessary step before running Grunt tasks. ```bash sudo npm install grunt-cli -g ``` -------------------------------- ### Initialize EaselJS Stage and Event Listeners Source: https://github.com/createjs/easeljs/blob/master/examples/GlobalToLocal1.html Sets up the EaselJS stage, enables touch and DOM events, and attaches mouse event listeners for drawing functionality. This is the initial setup for the interactive drawing example. ```javascript var canvas; var stage; var _mouseIsDown; var _mouseX; var _mouseY; var spin1; // nested invisble container to generate a spirograph effect var spin2; // nested invisble container to generate a spirograph effect var shape; // drawing shape var color; // drawing color var lastPt; // last draw position var text; var graphics; var count = 0; function init() { // create a new stage and point it at our canvas: canvas = document.getElementById("testCanvas"); stage = new createjs.Stage(canvas); createjs.Touch.enable(stage); // attach mouse handlers directly to the source canvas // better than calling from canvas tag for cross browser stage.enableDOMEvents(true); stage.addEventListener("stagemousemove", mouseMove); stage.addEventListener("stagemousedown", mouseDown); stage.addEventListener("stagemouseup", mouseUp); text = new createjs.Text("Click and Drag", "36px Arial", "#777777"); text.x = 360; text.y = 200; stage.addChild(text); // shape to draw vector data into: shape = new createjs.Shape(); shape.x = 41; //position in parent container graphics = shape.graphics; // middle spinner: spin2 = new createjs.Container(); spin2.addChild(shape); spin2.x = 303; //position in parent container // outside spinner: spin1 = new createjs.Container(); spin1.addChild(spin2); // center it on the stage: spin1.x = canvas.width / 2; spin1.y = canvas.height / 2; stage.addChild(spin1); // start the tick and point it at the window so we can do some work before updating the stage: createjs.Ticker.framerate = 30; createjs.Ticker.addEventListener("tick", tick); } ``` -------------------------------- ### Install Sass Source: https://github.com/createjs/easeljs/blob/master/build/README.md Installs or updates Sass, which is required for the build process. Ruby is a prerequisite for Sass. ```bash gem install sass; ``` -------------------------------- ### Setup Stages and Create Buttons Source: https://github.com/createjs/easeljs/blob/master/examples/WebGL/TwoStages.html Sets up both a WebGL stage and a Context2D stage, enabling mouse over events for both. It then configures the Context2D stage to pass unhandled events to the WebGL stage and creates two button instances, one for each stage. ```javascript function handleImageLoad(evt) { webGLStage = new createjs.StageGL("webGLCanvas"); webGLStage.enableMouseOver(); c2dStage = new createjs.Stage("c2dCanvas"); c2dStage.enableMouseOver(10); c2dStage.nextStage = webGLStage; // spritesheet "bitmap" button: var spriteSheet = new createjs.SpriteSheet({ images: [evt.target], frames: {width: 300, height: 100}, animations: { out: 0, over: 1, down: 2 } }); var bitmapButton = new createjs.Sprite(spriteSheet, "up"); webGLStage.addChild(bitmapButton).set({x: 250, y: 150}); new createjs.ButtonHelper(bitmapButton); // movieclip "vector" button: var vectorButton = new lib.VectorButton(); c2dStage.addChild(vectorButton).set({x: 50, y: 30}); new createjs.ButtonHelper(vectorButton); createjs.Ticker.addEventListener("tick", c2dStage); createjs.Ticker.addEventListener("tick", webGLStage); } ``` -------------------------------- ### Handle Image Load and Setup Filters Source: https://github.com/createjs/easeljs/blob/master/examples/AlphaMaskReveal.html Completes the initialization after the image is loaded. It enables touch interactions, sets up mouse event listeners, creates the mask and filters, and adds display objects to the stage. ```javascript function handleComplete() { examples.hideDistractor(); createjs.Touch.enable(stage); stage.enableMouseOver(); stage.addEventListener("stagemousedown", handleMouseDown); stage.addEventListener("stagemouseup", handleMouseUp); stage.addEventListener("stagemousemove", handleMouseMove); drawingCanvas = new createjs.Shape(); drawingCanvas.cache(0, 0, image.width, image.height); bitmap = new createjs.Bitmap(image); maskFilter = new createjs.AlphaMaskFilter(drawingCanvas.cacheCanvas); bitmap.filters = [maskFilter]; bitmap.cache(0, 0, image.width, image.height); blur = new createjs.Bitmap(image); blur.filters = [new createjs.BlurFilter(24, 24, 2), new createjs.ColorMatrixFilter(new createjs.ColorMatrix(60))]; blur.cache(0, 0, 960, 400); text.text = "Click and Drag to Reveal the Image."; stage.addChild(blur, text, bitmap); cursor = new createjs.Shape(new createjs.Graphics().beginFill("#FFFFFF").drawCircle(0, 0, 25)); cursor.cursor = "pointer"; stage.addChild(cursor); } ``` -------------------------------- ### Initialize EaselJS Stage and Load Assets Source: https://github.com/createjs/easeljs/blob/master/examples/DragAndDrop.html Sets up the EaselJS stage, enables touch and mouseover events, and loads an image asset. This is the initial setup required for most EaselJS applications. ```javascript var canvas, stage; var mouseTarget; // the display object currently under the mouse, or being dragged var dragStarted; // indicates whether we are currently in a drag operation var offset; var update = true; function init() { examples.showDistractor(); // create stage and point it to the canvas: canvas = document.getElementById("testCanvas"); stage = new createjs.Stage(canvas); // enable touch interactions if supported on the current device: createjs.Touch.enable(stage); // enabled mouse over / out events stage.enableMouseOver(10); stage.mouseMoveOutside = true; // keep tracking the mouse even when it leaves the canvas // load the source image: var image = new Image(); image.src = "../\_assets/art/daisy.png"; image.onload = handleImageLoad; } function stop() { createjs.Ticker.removeEventListener("tick", tick); } function handleImageLoad(event) { var image = event.target; var bitmap; var container = new createjs.Container(); stage.addChild(container); // create and populate the screen with random daisies: for (var i = 0; i < 100; i++) { bitmap = new createjs.Bitmap(image); container.addChild(bitmap); bitmap.x = canvas.width * Math.random() | 0; bitmap.y = canvas.height * Math.random() | 0; bitmap.rotation = 360 * Math.random() | 0; bitmap.regX = bitmap.image.width / 2 | 0; bitmap.regY = bitmap.image.height / 2 | 0; bitmap.scale = bitmap.originalScale = Math.random() * 0.4 + 0.6; bitmap.name = "bmp_" + i; bitmap.cursor = "pointer"; // using "on" binds the listener to the scope of the currentTarget by default // in this case that means it executes in the scope of the button. bitmap.on("mousedown", function (evt) { this.parent.addChild(this); this.offset = {x: this.x - evt.stageX, y: this.y - evt.stageY}; }); // the pressmove event is dispatched when the mouse moves after a mousedown on the target until the mouse is released. bitmap.on("pressmove", function (evt) { this.x = evt.stageX + this.offset.x; this.y = evt.stageY + this.offset.y; // indicate that the stage should be updated on the next tick: update = true; }); bitmap.on("rollover", function (evt) { this.scale = this.originalScale * 1.2; update = true; }); bitmap.on("rollout", function (evt) { this.scale = this.originalScale; update = true; }); } examples.hideDistractor(); createjs.Ticker.addEventListener("tick", tick); } function tick(event) { // this set makes it so the stage only re-renders when an event handler indicates a change has happened. if (update) { update = false; // only update once stage.update(event); } } ``` -------------------------------- ### CreateJS Text Example Source: https://github.com/createjs/easeljs/blob/master/examples/APITest.html Creates and adds a text object to the stage. Configure font, size, and color. ```javascript function textDemo(stage) { var txt = new createjs.Text("Hello CreateJS!", "24px Arial", FILL_COLOR); txt.lineWidth = 150; stage.addChild(txt); } ``` -------------------------------- ### Handle Image Load and Setup Display List Source: https://github.com/createjs/easeljs/blob/master/extras/PerformanceTests/bitmap.html Callback function executed after the image is loaded. It populates the stage with containers and bitmaps, then sets up the Ticker for animation. ```javascript function handleLoad() { time("setup"); for (var i=0; i 0; j--) { shape.graphics.beginFill(colors[Math.random() * colors.length | 0]).drawCircle(0, 0, radius * j / rings); } shape.x = Math.random() * canvas.width; shape.y = Math.random() * canvas.height; shape.velX = Math.random() * 10 - 5; shape.velY = Math.random() * 10 - 5; // turn snapToPixel on for all shapes - it's set to false by default on Shape. // it won't do anything until stage.snapToPixelEnabled is set to true. shape.snapToPixel = true; stage.addChild(shape); } // add a text object to output the current FPS: fpsLabel = new createjs.Text("-- fps", "bold 18px Arial", "#FFF"); stage.addChild(fpsLabel); fpsLabel.x = 10; fpsLabel.y = 20; // start the tick and point it at the window so we can do some work before updating the stage: createjs.Ticker.timingMode = createjs.Ticker.RAF; createjs.Ticker.addEventListener("tick", tick); } ``` -------------------------------- ### Handle Game Start Click Source: https://github.com/createjs/easeljs/blob/master/examples/Game/Game.html Removes the click listener, hides the welcome message, plays a 'begin' sound, and calls the restart function to initialize the game. ```javascript function handleClick() { //prevent extra clicks and hide text canvas.onclick = null; stage.removeChild(messageField); // indicate the player is now on screen createjs.Sound.play("begin"); restart(); } ``` -------------------------------- ### Using EaselJS in a Backbone View Source: https://github.com/createjs/easeljs/wiki/Using-easeljs-and-tweenjs-with-requirejs Integrate EaselJS into a Backbone view by defining it as a dependency. This example initializes a createjs.Stage. ```javascript define([ 'jquery', 'underscore', 'backbone', 'easel' ], function($,_,Backbone) { var GameView = Backbone.View.extend({ initialize : function(){ var canvas = document.getElementById('gameCanvas'); var stage = new createjs.Stage(canvas); // all good - soldier on... console.log(stage, "GameView / initialize"); // quick test - stage is not null }); return GameView; }); ``` -------------------------------- ### Stage Setup Function Source: https://github.com/createjs/easeljs/blob/master/examples/TwoStages.html A helper function to create and configure an EaselJS stage with event listeners and the ticker. Call this to initialize new stages. ```javascript /* create a stage object to work with the canvas add event listeners for stage events start tick on stage */ function stageSetup(canvasName, handler) { stage = new createjs.Stage(canvasName); //stage.addEventListener("stagemousemove", handler); // too noisy stage.addEventListener("stagemousedown", handler); stage.addEventListener("stagemouseup", handler); stage.addEventListener("mouseleave", handler); stage.addEventListener("mouseenter", handler); createjs.Ticker.addEventListener("tick", stage); stage.log = []; return stage; } ``` -------------------------------- ### EaselJS Transform Example Source: https://github.com/createjs/easeljs/blob/master/examples/Transform.html Initializes the EaselJS stage, loads an image, and sets up event listeners for mouse interaction and touch. This code is essential for setting up the canvas and image loading. ```javascript var stage, sliceContainer, sliceWidth, degToRad = Math.PI / 180; function init() { examples.showDistractor(); stage = new createjs.Stage("testCanvas"); stage.enableMouseOver(); createjs.Touch.enable(stage); stage.mouseMoveOutside = true; var img = new Image(); img.onload = handleImageLoad; img.src = "../\_assets/art/flowers.jpg"; } ``` -------------------------------- ### EaselJS Performance Test Setup Source: https://github.com/createjs/easeljs/blob/master/extras/PerformanceTests/displayList.html Initializes the EaselJS stage, graphics, and populates the display list with containers and shapes. This function is called by the performance framework. ```javascript var c; var containers=200, shapes=200, w, h, count= 0, offset= 0, g, stage; // runTest is a special method called by the performance framework // it passes the version array (ex. [0,7,1]) and a hash of all the query string values function runTest(version, qs) { if (!checkVersion("0.6")) { return endTest(); } c = checkVersion("0.5") ? createjs : window; var canvas = document.getElementById("testCanvas"); w=canvas.width; h=canvas.height; stage = new c.Stage(canvas); stage.tickOnUpdate = false; stage.enableDOMEvents(false); // stage.canvas = new FauxCanvas(true); g = new c.Graphics().beginFill("red").drawRect(-1,-1,2,2); time("setup"); for (var i=0; i> 1; bmp.regY = img.height >> 1; bmp.x = canvas.width >> 1; bmp.y = canvas.height >> 1; bmp.scale = 0.1; stage.addChild(bmp); stage.update(); createjs.Ticker.timingMode = createjs.Ticker.RAF; createjs.Ticker.addEventListener("tick", tick); } function tick(event) { angle += 0.01; var value = Math.sin(angle) * 360; bmp.rotation = value; bmp.scale = value / 360; stage.update(event); } ``` -------------------------------- ### Input Handling for Game Controls in EaselJS Source: https://github.com/createjs/easeljs/blob/master/examples/Game/Game.html Sets up event listeners for keyboard input to control game actions like movement and firing. This snippet shows the start of the keydown handler. ```javascript //allow for WASD and arrow control scheme function handleKeyDown(e) { //cross browser issues exist if (!e) { var e = window.event; } switch (e.k ``` -------------------------------- ### Running the SVG Export Process Source: https://github.com/createjs/easeljs/blob/master/extras/SVGExporter/SVGExport.html Initializes the SVGExporter with the stage and export options, then runs the export process. It measures the time taken for the export and schedules the addition of a download link. ```javascript exporter = new SVGExporter(stage, false, false, false); var t = new Date().getTime(); exporter.run(); statusEl.innerHTML = "Export took: "+(new Date().getTime() - t)+"ms "; setTimeout(addDownload, 1); ``` -------------------------------- ### Apply ColorFilter to a Bitmap Source: https://context7.com/createjs/easeljs/llms.txt Applies a ColorFilter to a Bitmap to modify its colors based on multiplier and offset values per channel. The example shows how to turn an image solid blue and then tint it red. ```javascript var colorFilter = new createjs.ColorFilter( 0, 0, 0, 1, // redMult, greenMult, blueMult, alphaMult 0, 0, 255, 0 // redOffset, greenOffset, blueOffset, alphaOffset ); // turns anything solid blue var bmp = new createjs.Bitmap("photo.jpg"); bmp.filters = [colorFilter]; bmp.cache(0, 0, 400, 300); // Tint red: keep red channel, zero out others bmp.filters = [new createjs.ColorFilter(1, 0, 0, 1)]; bmp.updateCache(); ```