How We Built a Browser-Based Photo Text Editor
Inside our local photo text editor: browser file handling, Canvas text layout, draggable captions and PNG/JPG exports that use the preview pixels.

Our Add Text to Photo tool puts a new caption, title or message over an image. You choose a photo, type your words, adjust their appearance and download the result. The selected photo and caption stay in the browser during this workflow.
This feature sits alongside our AI editor for replacing existing image text. Adding a caption has a different job: draw the words exactly where the user places them, without asking a model to repaint the scene. We built it with a regular React component and the browser’s Canvas 2D API.
Here is how the implementation works, including a small export issue we found during testing and the change that fixed it.
The local editing pipeline
The image-editing path is short:
Choose a local file
→ Decode it as an image
→ Draw the photo and caption on a canvas
→ Adjust the text and redraw
→ Encode the canvas as a PNG or JPG download
There is no image-processing API request between those steps. The page itself still loads website resources, and local image processing should not be confused with a promise that the entire website makes no network requests.
React stores the caption and its settings. A rendering function draws the image and text whenever those settings change. The component handles file selection, pointer interaction, validation messages and downloading; the renderer handles the pixels. We did not need an additional image-editing library for this scope.
Reading a photo without uploading it
A file input gives the component a browser File object. After checking its type and size, the tool creates an object URL and assigns it to an image element for decoding. An object URL is a browser reference to that file, rather than an upload destination. The relevant browser mechanism is documented in MDN’s createObjectURL reference.
Once the image loads, the component checks its decoded dimensions. It then keeps the decoded image for drawing and releases the temporary input URL. A request counter prevents an older image load from replacing a more recently selected file.
The current limits are explicit:
| Input check | Current limit |
|---|---|
| File types | JPG, PNG and WebP |
| File size | Up to 15 MB |
| Longest side | Up to 8192 pixels |
| Total image area | Up to 24 megapixels |
| Selection | One image at a time |
The dimension checks matter because a compressed file’s size does not describe how much space its decoded pixels will need. These limits bound the supported workload; they do not guarantee that every accepted image will be fast on every phone.
HEIC, SVG and PDF are outside the supported input formats. We also do not offer an animation-editing workflow.
Keeping image pixels separate from screen size
The canvas uses the decoded image’s original pixel dimensions. CSS scales its visible preview to fit the available space, including a smaller phone screen.
These are two different sizes. A 1200 × 800 image can appear much smaller in the page while the underlying canvas still contains 1200 × 800 pixels. Exporting uses that canvas, so the download does not inherit the reduced size of the on-screen preview.
The renderer begins by setting the backing dimensions and drawing the original image:
canvas.width = image.naturalWidth;
canvas.height = image.naturalHeight;
ctx.drawImage(image, 0, 0);
This excerpt shows the core operation. The production renderer also clears previous pixels and only resets canvas dimensions when they need to change.
We scale the text-size setting relative to image width, using a 600-pixel reference width. The same setting therefore occupies a similar proportion of a small photo and a larger one. It is not a promise of a particular print point size.
Laying out the caption
The text model contains the caption, font, size, color, alignment, shadow, background and horizontal and vertical positions. It describes one multiline text box.
The renderer measures text with measureText() using the selected canvas font. It respects explicit newline characters, wraps long lines at word boundaries and splits oversized words into Unicode code points when necessary. Tests cover manual blank lines, long words, CJK text and an emoji example.
Splitting by code point avoids cutting a surrogate pair in half, but it is not full grapheme-cluster or language-aware line breaking. Complex joined emoji and advanced typography remain outside the layout guarantees.
After wrapping, the renderer calculates the text box’s width and height, adds padding and clamps its position inside the photo. It reduces the font size when the caption would otherwise exceed the image height. The text input is limited to 300 characters to keep this a short-caption tool.
The font menu currently offers Arial, Georgia, Verdana and Courier New with fallback families. These are system-font choices, so fallback availability and rasterization can vary across devices. There is no custom font upload or automatic font matching in this tool.

A second real export: a serif caption with a dark background. This display copy is WebP; the tool’s original PNG is available as a full-size export. Original photo:
Max Fischer on Pexels
.
Making the text draggable
Pointer coordinates arrive in screen pixels. The text layout is calculated in image pixels. Moving the caption correctly requires converting between them.
The conversion uses the canvas’s visible bounding rectangle:
const imageX = ((event.clientX - rect.left) * canvas.width) / rect.width;
const imageY = ((event.clientY - rect.top) * canvas.height) / rect.height;
When a pointer goes down inside the measured caption box, the tool records the distance from that point to the box’s origin. Keeping this offset prevents the caption from jumping so that its corner suddenly sits under the pointer.
Pointer capture keeps the drag connected to the canvas while the pointer moves. Position values are stored as fractions of image width and height, and the renderer clamps the resulting box to the image boundaries.
Dragging is not the only way to position text. Horizontal and vertical range controls provide a keyboard-operable alternative. On mobile, the preview sits above the settings, and the same coordinate conversion applies to the smaller canvas display.
Why PNG export uses the existing preview canvas
Our first export path drew the photo and caption onto a fresh canvas before encoding it. The preview and export used the same renderer, but decoded-pixel comparisons still found small differences around the letter edges.
The images looked alike, yet they were not pixel-identical. Rather than treating a shared function as proof that the two outputs matched, we changed the PNG path to encode the canvas that was already visible in the preview.
The important decision in the current code is:
const preview = canvas.current;
const output =
format === 'image/png' ? preview : document.createElement('canvas');
The component then calls toBlob() to produce the downloadable image. As described in MDN’s toBlob reference, this encodes the image contained in the canvas into a Blob.
For PNG, the existing rendered pixels are the source. In our final live browser test, the downloaded PNG’s decoded pixels matched the preview exactly. That is a result from the tested browser and file, rather than a claim about identical font rendering across every device.
For JPG, the tool copies the preview pixels to a separate canvas and paints white behind any transparent areas before encoding. JPG is lossy, so its decoded pixels should not be expected to match a PNG exactly. Choose PNG when transparency or crisp text edges are important.
The download uses a temporary object URL. The component revokes it after a delay to release the reference.
What we verified before publishing
We tested the complete path, not only the text-layout helper. Local browser checks covered entering a caption, changing its style, dragging it, choosing another JPG, rejecting an SVG and downloading both PNG and JPG files. The sample downloads retained their 1200 × 800 dimensions.
The final production check covered a text and color change followed by a real PNG download. We compared decoded pixels with the preview, checked that the displayed examples loaded, and confirmed that the image-editing workflow made no edit or upload API calls in that test. A 320-pixel-wide mobile layout also passed the horizontal-overflow check.
The two examples on the tool page are actual exports from the tool, with optimized WebP copies for page display. They are demonstrations using a credited stock photo, not customer submissions or AI-generated success examples.
What this tool does not do
The current version supports one multiline caption, with a fixed bold text weight, four font choices, color, alignment, optional shadow and optional background. It does not provide independently positioned text layers, editable project files, custom font uploads, OCR, perspective matching or batch editing.
Downloading flattens the words into the image. Closing or refreshing the page does not preserve an editable project. Canvas export is also not a workflow for preserving all original camera metadata or producing a layered print master.
If you need to change words already printed on a photographed sign or label, use the separate photo text replacement editor and inspect the generated result. If you want to put a new caption over an image, open Add Text to Photo, try the sample, and download the result to see how the local workflow behaves.
Your image. A better choice of words.
Start with one short phrase, then review the lettering and background before you download.