You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Readme.md
+87-1Lines changed: 87 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -117,7 +117,7 @@ img.dataMode = Image.MODE_MIME | Image.MODE_IMAGE; // Both are tracked
117
117
118
118
If image data is not tracked, and the Image is drawn to an image rather than a PDF canvas, the output will be junk. Enabling mime data tracking has no benefits (only a slow down) unless you are generating a PDF.
119
119
120
-
### Canvas#pngStream()
120
+
### Canvas#pngStream(options)
121
121
122
122
To create a `PNGStream` simply call `canvas.pngStream()`, and the stream will start to emit _data_ events, finally emitting _end_ when finished. If an exception occurs the _error_ event is emitted.
123
123
@@ -137,6 +137,22 @@ stream.on('end', function(){
137
137
138
138
Currently _only_ sync streaming is supported, however we plan on supporting async streaming as well (of course :) ). Until then the `Canvas#toBuffer(callback)` alternative is async utilizing `eio_custom()`.
139
139
140
+
To encode indexed PNGs from canvases with `pixelFormat: 'A8'` or `'A1'`, provide an options object:
141
+
142
+
```js
143
+
var palette =newUint8ClampedArray([
144
+
//r g b a
145
+
0, 50, 50, 255, // index 1
146
+
10, 90, 90, 255, // index 2
147
+
127, 127, 255, 255
148
+
// ...
149
+
]);
150
+
canvas.pngStream({
151
+
palette: palette,
152
+
backgroundIndex:0// optional, defaults to 0
153
+
})
154
+
```
155
+
140
156
### Canvas#jpegStream() and Canvas#syncJPEGStream()
141
157
142
158
You can likewise create a `JPEGStream` by calling `canvas.jpegStream()` with
@@ -312,6 +328,76 @@ var canvas = new Canvas(200, 500, 'svg');
312
328
fs.writeFile('out.svg', canvas.toBuffer());
313
329
```
314
330
331
+
## Image pixel formats (experimental)
332
+
333
+
node-canvas has experimental support for additional pixel formats, roughly
334
+
following the [Canvas color space proposal](https://github.com/WICG/canvas-color-space/blob/master/CanvasColorSpaceProposal.md).
335
+
336
+
```js
337
+
var canvas =newCanvas(200, 200);
338
+
var ctx =canvas.getContext('2d', {pixelFormat:'A8'});
339
+
```
340
+
341
+
By default, canvases are created in the `RGBA32` format, which corresponds to
342
+
the native HTML Canvas behavior. Each pixel is 32 bits. The JavaScript APIs
343
+
that involve pixel data (`getImageData`, `putImageData`) store the colors in
344
+
the order {red, green, blue, alpha} without alpha pre-multiplication. (The C++
345
+
API stores the colors in the order {alpha, red, green, blue} in native-[endian](https://en.wikipedia.org/wiki/Endianness)
346
+
ordering, with alpha pre-multiplication.)
347
+
348
+
These additional pixel formats have experimental support:
349
+
350
+
*`RGB24` Like `RGBA32`, but the 8 alpha bits are always opaque. This format is
351
+
always used if the `alpha` context attribute is set to false (i.e.
352
+
`canvas.getContext('2d', {alpha: false})`). This format can be faster than
353
+
`RGBA32` because transparency does not need to be calculated.
354
+
*`A8` Each pixel is 8 bits. This format can either be used for creating
355
+
grayscale images (treating each byte as an alpha value), or for creating
356
+
indexed PNGs (treating each byte as a palette index) (see [the example using
357
+
alpha values with `fillStyle`](examples/indexed-png-alpha.js) and [the
358
+
example using `imageData`](examples/indexed-png-image-data.js)).
359
+
*`RGB16_565` Each pixel is 16 bits, with red in the upper 5 bits, green in the
360
+
middle 6 bits, and blue in the lower 5 bits, in native platform endianness.
361
+
Some hardware devices and frame buffers use this format. Note that PNG does
362
+
not support this format; when creating a PNG, the image will be converted to
363
+
24-bit RGB. This format is thus suboptimal for generating PNGs.
364
+
`ImageData` instances for this mode use a `Uint16Array` instead of a `Uint8ClampedArray`.
365
+
*`A1` Each pixel is 1 bit, and pixels are packed together into 32-bit
366
+
quantities. The ordering of the bits matches the endianness of the
367
+
platform: on a little-endian machine, the first pixel is the least-
368
+
significant bit. This format can be used for creating single-color images.
369
+
*Support for this format is incomplete, see note below.*
370
+
*`RGB30` Each pixel is 30 bits, with red in the upper 10, green
371
+
in the middle 10, and blue in the lower 10. (Requires Cairo 1.12 or later.)
372
+
*Support for this format is incomplete, see note below.*
373
+
374
+
Notes and caveats:
375
+
376
+
* Using a non-default format can affect the behavior of APIs that involve pixel
377
+
data:
378
+
379
+
*`context2d.createImageData` The size of the array returned depends on the
380
+
number of bit per pixel for the underlying image data format, per the above
381
+
descriptions.
382
+
*`context2d.getImageData` The format of the array returned depends on the
383
+
underlying image mode, per the above descriptions. Be aware of platform
384
+
endianness, which can be determined using node.js's [`os.endianness()`](https://nodejs.org/api/os.html#os_os_endianness)
385
+
function.
386
+
*`context2d.putImageData` As above.
387
+
388
+
*`A1` and `RGB30` do not yet support `getImageData` or `putImageData`. Have a
389
+
use case and/or opinion on working with these formats? Open an issue and let
390
+
us know! (See #935.)
391
+
392
+
*`A1`, `A8`, `RGB30` and `RGB16_565` with shadow blurs may crash or not render
393
+
properly.
394
+
395
+
* The `ImageData(width, height)` and `ImageData(Uint8ClampedArray, width)`
396
+
constructors assume 4 bytes per pixel. To create an `ImageData` instance with
397
+
a different number of bytes per pixel, use
398
+
`new ImageData(new Uint8ClampedArray(size), width, height)` or
Although node-canvas is extremely new, and we have not even begun optimization yet it is already quite fast. For benchmarks vs other node canvas implementations view this [gist](https://gist.github.com/664922), or update the submodules and run `$ make benchmark` yourself.
0 commit comments