This article explains how to implement a browser-based Sobel edge detection pipeline entirely client-side, using only JavaScript and a <canvas> element. The process converts images into clean line art suitable for stencil printing, with no data leaving your device.
Grayscale Conversion
The first step is converting an RGBA image to grayscale using perceptual weights. The standard luminance formula—0.299*R + 0.587*G + 0.114*B—is applied to each pixel:
function grayscale(rgba, n) {
const g = new Float32Array(n);
for (let i = 0; i < n; i++)
g[i] = 0.299 * rgba[i * 4] + 0.587 * rgba[i * 4 + 1] + 0.114 * rgba[i * 4 + 2];
return g;
}
This produces a flat Float32Array of grayscale values, one per pixel, ready for filtering.
Box Blur for Noise Reduction
A box blur smooths the image to reduce noise before edge detection. The radius r defines the kernel size:
function boxBlur(g, w, h, r) {
if (r <= 0) return g;
const out = new Float32Array(w * h);
for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) {
let s = 0, c = 0;
for (let dy = -r; dy <= r; dy++) for (let dx = -r; dx <= r; dx++) {
const xx = x + dx, yy = y + dy;
if (xx < 0 || yy < 0 || xx >= w || yy >= h) continue;
s += g[yy * w + xx]; c++;
}
out[y * w + x] = s / c;
}
return out;
}
This function applies a uniform average over a square neighborhood, reducing high-frequency noise that could trigger false edges.
Sobel Gradient and Thresholding
The Sobel operator computes gradient magnitude at each pixel using 3×3 kernels for horizontal (gx) and vertical (gy) components:
function sobelEdges(g, w, h, threshold) {
const bin = new Uint8Array(w * h).fill(1);
for (let y = 1; y < h - 1; y++) for (let x = 1; x < w - 1; x++) {
const i = y * w + x;
const gx = -g[i - w - 1] - 2 * g[i - 1] - g[i + w - 1] + g[i - w + 1] + 2 * g[i + 1] + g[i + w + 1];
const gy = -g[i - w - 1] - 2 * g[i - w] - g[i - w + 1] + g[i + w - 1] + 2 * g[i + w] + g[i + w + 1];
bin[i] = Math.sqrt(gx * gx + gy * gy) > threshold ? 0 : 1;
}
return bin;
}
Pixels with gradient magnitude above threshold are marked as edge pixels (0), others as background (1). The default threshold in this module is 60; a higher value keeps only stronger edges.
Dilation to Thicken Lines
To improve line visibility and robustness in printing, dilation expands edges:
function dilate(bin, w, h, iterations) {
for (let k = 0; k < iterations; k++) {
const out = bin.slice();
for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) {
if (bin[y * w + x] === 0) continue;
if ((x > 0 && bin[y * w + x - 1] === 0) || (x < w - 1 && bin[y * w + x + 1] === 0) ||
(y > 0 && bin[(y - 1) * w + x] === 0) || (y < h - 1 && bin[(y + 1) * w + x] === 0))
out[y * w + x] = 0;
}
bin = out;
}
return bin;
}
Each iteration checks 4-neighboring pixels; if any neighbor is a line, the current pixel becomes a line too. This thickens fine lines and closes small gaps.
HD Export Scaling
For high-definition exports, scaling ensures consistent edge quality. The preview is capped at 1400px wide. For HD exports up to 4000px, the scale factor k is:
k = HD width / preview width
All parameters are scaled accordingly:
- Blur radius:
r * k - Line thickness:
thickness * k - Sobel kernel sampling: neighbors are sampled
kpixels apart
This preserves the effective threshold across resolutions.
Verify No Upload
To confirm no data is sent:
- Open DevTools.
- Go to the Network tab.
- Perform a conversion.
- Check that no requests appear.
The entire pipeline runs in the browser canvas. No image data leaves your device.
Tools and Resources
- sobel-line-art – Full source code and module exports.
- photo to stencil tool – Live demo using this algorithm.
In the live tool the whole conversion runs on a canvas in the browser, and the site's FAQ says nothing is sent anywhere.
Top comments (0)