Urgent.News

What's breaking now, across thousands of outlets.

Tech

Building a Drawing App with Canvas — Smooth Lines, Undo, and Shape Tools

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 ;…

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.

Undo 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.

Drawing 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.

To 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.

Finally, 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.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Why “Would You Use This?” Is the Most Useless Question in Product Research

You’ve done it. I’ve done it. Everyone building something has done it. You post your idea, your landing page, your demo. You ask “would you use this?” or “does this solve a problem for you?” Everyone…

  • "Would you use this?" question is hypothetical and unproductive
  • Effective question: "Walk me through the last time you dealt with [the issue]"
  • Insights from this question: actual behavior, true competition, problem significance

SharePoint Everyone Except External Users Not Working: A Read-First Claim, People Picker, and Public-Site Triage

Helpdesk: "A site owner typed Everyone except external users in the People Picker on the Partners site and nothing came back. On the company intranet it used to show up.

  • "Everyone except external users" is a special SharePoint claim, not a security group
  • Users added to organization automatically become members of "Everyone except external users" claim
  • Guests who accepted invitations are not part of "Everyone except external users" claim

How to Find Which Tasks to Automate First: A 1-Hour Workflow Audit

The fastest way to waste time on automation is to start with the tool. A better starting point is the workflow. Before reaching for AI, Zapier, scripts, APIs, or custom software, I like to ask a…

  • Identify tasks that are repeated often, consume time, create errors, and follow predictable rules
  • Estimate time spent on each task using a scoring system from 1 to 5
  • Assess error risk, delay, and predictability of each workflow

More from Friday 16 October →