Repository navigation
PureImage 0.5.0 is a large conformance release. It now passes 919 of the 1,040 Web Platform Tests canvas test files, up from 226 when the WPT runner was added, so drawing behaves much more like a browser canvas.
Because of that, this release has breaking changes. Read Compatibility before upgrading from 0.4.x.
npm install pureimage@0.5.0New APIs
Path2D, including SVG path data (new Path2D("M0 0 L10 10")) andfill(path),stroke(path),clip(path)overloadsisPointInPath,isPointInStroke,resetTransformcreatePattern/CanvasPatternCanvasGradient, the exported base class of every gradientImageDataandcreateImageData- Every
globalCompositeOperationoperator and blend mode roundRectas specifiedletterSpacing,wordSpacingandlang- The
maxWidthargument offillTextandstrokeText ctx.reset()
Fixes and improvements
- Rasterization: fills and gradients are sampled at pixel centres. Arcs,
arcTo,ellipseand arc joins are fixed. Fill rules (nonzero,evenodd) work infill()andclip(). - Strokes: the stroker is rewritten, with line caps, line joins, miter limits and transforms.
- Compositing: source-over alpha blending is correct, and
globalAlphais respected everywhere. - Colors: a full CSS color parser, including
hsl(), CSS Color 4 syntax with/ alpha, and percentages. - Gradients:
- Color stops are sorted by offset.
- Colors are interpolated without premultiplying.
- Radial gradients use the spec's two-circle model.
- Conic gradients sweep clockwise.
- Gradients are positioned using the transform in effect when you fill.
- Text:
measureTextreturns full metrics.ctx.fontparses the whole CSS font shorthand, including family lists, which are tried in order.- Right-to-left alignment works.
- Glyphs with holes (o, 0, B) render correctly with opentype.js 1.3.
- State:
save()andrestore()cover all drawing state. - Types: declarations are generated by
tsc, and@types/opentype.jsis now a dependency, so they type-check withoutskipLibCheck.
Compatibility
Most likely to affect you
getImageDatareturns anImageData, not aBitmap.- Its
datais aUint8ClampedArray, so out-of-range values are clamped instead of wrapping. - Its
width,heightanddataare read-only. - It has no drawing methods.
getPixelRGBA,getPixelRGBA_separateandsetPixelRGBAstill work on it, andputImageDatastill accepts aBitmap.
- Its
- opentype.js is upgraded from 0.4.11 to 1.3.4. Kerning from the font's GPOS table now applies, so some letter pairs draw slightly closer.
- The default font is
10px sans-serif(it was 12px). Text drawn without settingctx.fontis smaller. em,remand%font sizes are converted to px against 10px. Before,1.5emgave a 1.5px font.ptis unchanged and still means px.- Rendering is closer to browsers. Antialiased edges, strokes and gradients come out slightly different, so pixel-exact snapshot tests will probably need updating.
Attribute values read back differently
ctx.font:- Weight 700 reads back as
bold(it was700). bolderandlighterare 700 and 100 (they were 800 and 300).- A family list reads back as
A, B, with quotes kept. - The internal
_font.familyholds only the first family in the list.
- Weight 700 reads back as
fillStyleandstrokeStyleread back as#rrggbborrgba(r, g, b, a). Invalid colors are ignored instead of throwing.- Invalid values are ignored by
textAlign,textBaseline,direction,fontKerning,fontStretch,fontVariantCapsandtextRendering. Keywords are case-sensitive, sotextAlign = "END"no longer takes effect. globalAlphaignores values outside 0–1 and non-finite values.
Behaviour changes
fillTextandstrokeTextuse their 4th argument,maxWidth, to squeeze text to fit. AmaxWidthof 0, a negative number orNaNdraws nothing.- With
direction = "rtl",startandendalign to the right and left. - Gradients follow the transform in effect when you fill, not the one in effect when they were created.
fill()andclip()default to thenonzerofill rule.- Path calls are more lenient.
- Calls with non-finite arguments are ignored.
lineToand curves on an empty path start a new subpath instead of throwing.- Negative radii throw
IndexSizeError.
clearRectandstrokeRecthonour the current transform.strokeRectno longer changes the current path.restore()resets more state: styles,lineWidth,globalAlpha, text attributes, line caps and joins, shadows and compositing.- Setting a
Bitmap'swidthorheightclears it and resets its context. Invalid sizes are rejected. getContext()with no argument throws aTypeError.- Missing required arguments throw a
TypeErrorin many methods. measureTextwith no font registered estimates half an em per character. It used to return a fixed width of 10.
Packaging
- Type declarations come one per module (
dist/*.d.ts) instead of as one bundled file. Imports from"pureimage"are unchanged. @types/opentype.jsis installed as a dependency.
Upgrading
- Change code that calls drawing methods on the result of
getImageData. UseputImageDatato copy pixels onto a canvas instead. - If you relied on the old default font or on
emsizes, setctx.fontexplicitly in px. - Regenerate image snapshots and check them by eye.
The full list of changes is in changes.md and in the compare view.