DEV Community

Cover image for Generating Calibrated 1:1 Multi-Page PDF 1.4 Vector Grids & CIELAB Flat-Block Palettes in Vanilla JS
miao wang
miao wang

Posted on

Generating Calibrated 1:1 Multi-Page PDF 1.4 Vector Grids & CIELAB Flat-Block Palettes in Vanilla JS

When converting a pet photo or illustration into a physical 13-mesh or 18-mesh needlepoint chart, two classic browser-side bugs ruin most naive canvas converters:

  1. RGB Euclidean distance scatters "confetti" stitches across fur and shadows because RGB is not perceptually uniform.
  2. Rasterized browser window.print() scales charts by 94%–97% due to OS printer margins, causing a 10-inch pattern to drift by nearly half an inch on physical mono canvas.

While building StitchChartPDF, I wanted the entire pipeline—palette quantization, DMC thread matching, Basketweave (+38%) vs. Continental skein math, and multi-page 1:1 Actual-Size trace PDF generation—to run 100% locally in browser memory with zero server uploads.

1. Killing "Confetti" Pixels Before Thread Matching

If you quantize a 144×180 grid (an 8"×10" design on 18-mesh canvas) directly against 450+ DMC thread colors in sRGB, specular highlights on a dog's nose or dark coat turn into isolated single-stitch islands that are miserable to paint or stitch.

Converting sRGB → linear RGB → XYZ → CIELAB (L*a*b*) and running spatial region merging on sub-threshold neighbor clusters eliminates isolated 1-stitch noise while preserving crisp eye catchlights—especially useful for pet needlepoint portraits.

// Convert linear RGB [0..1] channel to XYZ -> CIELAB perceptual space
function srgbToLinear(c) {
  const v = c / 255;
  return v <= 0.04045 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
}
Enter fullscreen mode Exit fullscreen mode

2. True 1:1 Physical PDF 1.4 Coordinates (72 pt = 1.000 inch)

Instead of screenshotting an HTML <canvas> into a blurry PNG PDF, writing raw PDF 1.4 vector content streams directly in JavaScript guarantees exact physical geometry:

  • 1 inch === 72.00 PDF points
  • 13 mesh spacing = 72 / 13 = 5.5385 pt per stitch
  • 18 mesh spacing = 72 / 18 = 4.0000 pt per stitch

Every exported PDF includes a physical 1.00 inch × 1.00 inch verification box on Page 1 so stitchers can verify Actual Size / 100% Scale with a ruler before tracing onto canvas.

3. Basketweave (+38%) vs. Continental DMC Skein Estimation

Needlepoint stitch choice dramatically changes yarn consumption because Basketweave interlocks diagonally on the reverse side of the canvas whereas Continental tent stitch travels shorter horizontal/vertical steps. In practice, Basketweave consumes roughly 38% more thread per square inch—a distinction we baked directly into our Needlepoint Canvas & DMC Skein Calculator alongside an open MIT dataset at needlepoint-mesh-dmc-skein-reference.

Top comments (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

The 94-97% window.print() scaling figure is the detail that makes this post — most converters treat print as 1:1 and only find out at the ruler. Writing raw PDF 1.4 streams instead of rasterizing the canvas is the right call for that reason alone, and the 1-inch verification box on page 1 is the honest check that closes the loop. One thing I wondered about: the region merging on sub-threshold clusters — how do you keep it from eating genuine single-stitch detail in high-contrast areas like eyes, where one dark stitch among light ones is the whole feature? Do you threshold by cluster size, or does the merge only run below a certain local contrast?