{
  "id": 14868426,
  "title": "Building a Drawing App with Canvas — Smooth Lines, Undo, and Shape Tools",
  "url": "https://urgent.news/2026/10/16/building-a-drawing-app-with-canvas-smooth-lines-undo-and-shape-tools",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-10-16T04:30:12.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/toolzip/building-a-drawing-app-with-canvas-smooth-lines-undo-and-shape-tools-54le"
  },
  "original_language": "en",
  "account": "Creating a drawing application using the HTML5 Canvas element can be an engaging project. Smooth lines, an undo feature, and shape tools are key components that enhance functionality. To achieve smooth lines, quadratic Bezier curves are used to interpolate between mouse movement points. This is done by saving the last known coordinates of both the X and Y axes, then calculating the midpoint between these coordinates and the current position. The quadratic curve is then drawn between the last position and the midpoint, resulting in a visually smoother line.\n\nUndo functionality in the drawing app can be implemented through state snapshots. After each stroke, the entire canvas state is saved as a data URL. The maximum number of undo states is limited, with older states being removed to conserve memory. The undo function simply pops the most recent state from the history stack, clears the canvas, and redraws the previous state. However, this method can be slow for large canvases, so using ImageData objects instead of data URLs can improve performance.\n\nDrawing shapes, such as rectangles, requires a preview feature. This preview updates in real time as the user moves the mouse, without permanently drawing on the canvas. The current state of the canvas before drawing the shape is saved as a snapshot. When the mouse moves, the snapshot is restored, and the shape preview is drawn using the strokeRect method.\n\nTo add image loading capabilities, the app can load an existing image onto the canvas. This is useful for annotation tasks. The image is first loaded into an Image object and once it's fully loaded, the canvas width and height are set to match the image's dimensions. The image is then drawn onto the canvas, and a state is saved for undo purposes.\n\nFinally, touch support can be added to the drawing app by adding touch event listeners alongside the mouse event listeners. This allows users to interact with the application using touch screens. The getPos function is used to retrieve the X and Y coordinates of the touch event, taking into account the browser's touch event structure.",
  "summary": "Building a Drawing App with Canvas — Smooth Lines, Undo, and Shape Tools A drawing app is a great Canvas project. Here's how to get smooth lines, implement undo, and add shape tools — including the non-obvious parts. Smooth Lines with Quadratic Curves Connecting every mousemove point with straight lines looks jagged. The fix: use quadratic Bezier curves through the midpoints: let lastX , lastY ;…",
  "key_points": [
    "Smooth lines achieved using quadratic Bezier curves",
    "Undo feature implemented with state snapshots",
    "Shape preview allows real-time drawing without permanent canvas changes"
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}