Skip to content

0.5.0

Latest

Choose a tag to compare

@joshmarinacci joshmarinacci released this 25 Nov 00:17
· 2 commits to master since this release
v0.5.0
b0dbf36

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

New APIs

  • Path2D, including SVG path data (new Path2D("M0 0 L10 10")) and fill(path), stroke(path), clip(path) overloads
  • isPointInPath, isPointInStroke, resetTransform
  • createPattern / CanvasPattern
  • CanvasGradient, the exported base class of every gradient
  • ImageData and createImageData
  • Every globalCompositeOperation operator and blend mode
  • roundRect as specified
  • letterSpacing, wordSpacing and lang
  • The maxWidth argument of fillText and strokeText
  • ctx.reset()

Fixes and improvements

  • Rasterization: fills and gradients are sampled at pixel centres. Arcs, arcTo, ellipse and arc joins are fixed. Fill rules (nonzero, evenodd) work in fill() and clip().
  • Strokes: the stroker is rewritten, with line caps, line joins, miter limits and transforms.
  • Compositing: source-over alpha blending is correct, and globalAlpha is 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:
    • measureText returns full metrics.
    • ctx.font parses 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() and restore() cover all drawing state.
  • Types: declarations are generated by tsc, and @types/opentype.js is now a dependency, so they type-check without skipLibCheck.

Compatibility

Most likely to affect you

  • getImageData returns an ImageData, not a Bitmap.
    • Its data is a Uint8ClampedArray, so out-of-range values are clamped instead of wrapping.
    • Its width, height and data are read-only.
    • It has no drawing methods.
    • getPixelRGBA, getPixelRGBA_separate and setPixelRGBA still work on it, and putImageData still accepts a Bitmap.
  • 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 setting ctx.font is smaller.
  • em, rem and % font sizes are converted to px against 10px. Before, 1.5em gave a 1.5px font. pt is 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 was 700).
    • bolder and lighter are 700 and 100 (they were 800 and 300).
    • A family list reads back as A, B, with quotes kept.
    • The internal _font.family holds only the first family in the list.
  • fillStyle and strokeStyle read back as #rrggbb or rgba(r, g, b, a). Invalid colors are ignored instead of throwing.
  • Invalid values are ignored by textAlign, textBaseline, direction, fontKerning, fontStretch, fontVariantCaps and textRendering. Keywords are case-sensitive, so textAlign = "END" no longer takes effect.
  • globalAlpha ignores values outside 0–1 and non-finite values.

Behaviour changes

  • fillText and strokeText use their 4th argument, maxWidth, to squeeze text to fit. A maxWidth of 0, a negative number or NaN draws nothing.
  • With direction = "rtl", start and end align to the right and left.
  • Gradients follow the transform in effect when you fill, not the one in effect when they were created.
  • fill() and clip() default to the nonzero fill rule.
  • Path calls are more lenient.
    • Calls with non-finite arguments are ignored.
    • lineTo and curves on an empty path start a new subpath instead of throwing.
    • Negative radii throw IndexSizeError.
  • clearRect and strokeRect honour the current transform. strokeRect no longer changes the current path.
  • restore() resets more state: styles, lineWidth, globalAlpha, text attributes, line caps and joins, shadows and compositing.
  • Setting a Bitmap's width or height clears it and resets its context. Invalid sizes are rejected.
  • getContext() with no argument throws a TypeError.
  • Missing required arguments throw a TypeError in many methods.
  • measureText with 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.js is installed as a dependency.

Upgrading

  1. Change code that calls drawing methods on the result of getImageData. Use putImageData to copy pixels onto a canvas instead.
  2. If you relied on the old default font or on em sizes, set ctx.font explicitly in px.
  3. Regenerate image snapshots and check them by eye.

The full list of changes is in changes.md and in the compare view.