Drawing draggable shapes on Canvas with CreateJS

EaselJS, part of the CreateJS suite, provides a display list and pointer events for HTML Canvas. This guide shows how to draw circles, rectangles, and stars, then drag them with a mouse or touch input. The updated example uses the current EaselJS 1.0 API instead of the article's original 2013 bundle.

Draggable shapes on canvas
Screenshot of the original draggable-shapes demo; the legacy embed appears below.

CreateJS and EaselJS

CreateJS is a group of JavaScript libraries for interactive HTML5 content. EaselJS is the part used here: it adds display objects, a stage, and mouse or touch events on top of the Canvas element. If you only need a few static drawings, the browser's Canvas API may be enough. For a jQuery-based drawing API, jCanvas is another option.

See the CreateJS project, the EaselJS overview, and the EaselJS API documentation for the full library and its display-list APIs.

Add a canvas and load EaselJS

Give the canvas explicit dimensions. They define its drawing coordinate space; CSS can make it fit a smaller screen without changing that coordinate system.

<canvas id="canvas" width="500" height="500">
  A circle, rectangle, and star are drawn on this canvas.
</canvas>
<script src="https://code.createjs.com/1.0.0/easeljs.min.js"></script>

The versioned EaselJS 1.0.0 bundle is hosted on the official CreateJS CDN. Keep the version pinned in a production app and update it deliberately. If your project already uses a package manager, install and pin EaselJS through that workflow instead of adding another page-level script.

You can scale the display size while preserving the canvas aspect ratio:

canvas {
  display: block;
  max-width: 100%;
  height: auto;
  touch-action: none;
}

touch-action: none prevents browser gestures such as scrolling from taking over while a user interacts with the drawing area. Apply it only to a canvas that is intended for direct manipulation.

Draw and drag shapes

Create a Stage for the canvas, enable touch support, and add shapes to it. EaselJS dispatches a pressmove event while a pointer drags a shape. The event's stageX and stageY values are coordinates in the stage's coordinate space, so the same handler can move every shape.

const canvas = document.getElementById("canvas");
const stage = new createjs.Stage(canvas);

createjs.Touch.enable(stage);

function addDraggable(shape, x, y) {
  shape.x = x;
  shape.y = y;
  shape.on("pressmove", (event) => {
    const draggedShape = event.currentTarget;
    draggedShape.x = event.stageX;
    draggedShape.y = event.stageY;
    stage.update();
  });
  stage.addChild(shape);
}

const circle = new createjs.Shape();
circle.graphics.beginFill("#e74c3c").drawCircle(0, 0, 40);
addDraggable(circle, 100, 120);

const rectangle = new createjs.Shape();
rectangle.graphics.beginFill("#3498db").drawRect(0, 0, 90, 60);
rectangle.regX = 45;
rectangle.regY = 30;
addDraggable(rectangle, 250, 120);

const star = new createjs.Shape();
star.graphics.beginFill("#f1c40f").drawPolyStar(0, 0, 45, 5, 0.6, -90);
addDraggable(star, 390, 120);

stage.update();

The circle and star are drawn around their local origin, so moving them to the pointer keeps the pointer near their centers. The rectangle is drawn from its top-left corner; setting regX and regY moves its registration point to the center so it behaves the same way when dragged. If you prefer top-left dragging, omit those registration values and adjust the target's position by the pointer offset.

createjs.Touch.enable(stage) lets EaselJS translate supported touch input into its pointer events; it does not require separate mouse and touch handlers. Disable touch when disposing of a stage that is no longer used:

createjs.Touch.disable(stage);

See the Stage event documentation, MouseEvent coordinate properties, and Touch API for event details.

Resizing and accessibility

Changing the canvas element's width or height attributes resets its drawing buffer. If the drawing must adapt to a changing viewport, update the canvas dimensions in a resize handler and redraw or rebuild the stage content afterward. Avoid setting the canvas to window.innerWidth and window.innerHeight once at startup and assuming that handles later resizes. See MDN's notes on canvas sizing.

Canvas exposes a bitmap, not semantic controls for each drawn shape. Fallback text inside the <canvas> helps when Canvas is unavailable, but it does not make the shapes keyboard-operable. For a production interface, provide an equivalent keyboard-accessible DOM control or representation and a useful text description; do not rely on an aria-label alone to explain interactive objects.

Original draggable-shapes demo

The original CodePen demo is retained below as a visual reference. Its embedded code uses the older CreateJS 2013 bundle; use the updated snippets above when starting a new project.

See the Pen Drawing draggable shapes on Canvas by Kanishk Kunal on CodePen.

9 thoughts on “Drawing draggable shapes on Canvas with CreateJS”

    • You need to call square.graphics.clear().beginFill(fill).drawRoundRect(0, 0, s, s, r) with new dimensions followed by stage.update() to change the shape of the square.

      Reply
  1. Kanishk,

    I have achieved what you have demonstrated, however, is there a way to track the percentage two objects overlap? I am trying to track the percentage of two circles overlapping. Then track all three circles overlapping.

    Reply
  2. Hi Kanishk,

    Thank you for this, it was super useful! I’m wondering if you knew an easy trick to automatically reset the layout to the default position after, say, three “drags”?

    Reply

Leave a Reply