Skip to content

Add typescript definitions #1330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ addons:
env:
- CXX=g++-4.9
sudo: false
script:
- node_version=$(node -v); if [ ${node_version:1:2} = 10 ]; then npm run dtslint; fi
- npm test
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
### Added
* (Actually) added `resolution` option for `canvas.toBuffer("image/png")` and
`canvas.createPNGStream()`. This was documented since 2.0.0 but not working.
* Add typescript definitions.
### Fixed
* PDF metadata (added in 2.3.0) wasn't being set with `canvas.createPDFStream()`
* Fix custom "inspect" function deprecation warnings (#1326)
Expand Down
6 changes: 4 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ This is a standard API, but several non-standard calls are supported. The full l
```js
dataUrl = canvas.toDataURL() // defaults to PNG
dataUrl = canvas.toDataURL('image/png')
dataUrl = canvas.toDataURL('image/jpeg')
dataUrl = canvas.toDataURL('image/jpeg', quality) // quality from 0 to 1
canvas.toDataURL((err, png) => { }) // defaults to PNG
canvas.toDataURL('image/png', (err, png) => { })
canvas.toDataURL('image/jpeg', (err, jpeg) => { }) // sync JPEG is not supported
Expand Down Expand Up @@ -423,9 +425,9 @@ In `glyph` mode, `ctx.strokeText()` and `ctx.fillText()` behave the same (aside

This property is tracked as part of the canvas state in save/restore.

### CanvasRenderingContext2D#globalCompositeOperator = 'saturate'
### CanvasRenderingContext2D#globalCompositeOperation = 'saturate'

In addition to all of the standard global composite operators defined by the Canvas specification, the ['saturate'](https://www.cairographics.org/operators/#saturate) operator is also available.
In addition to all of the standard global composite operations defined by the Canvas specification, the ['saturate'](https://www.cairographics.org/operators/#saturate) operation is also available.

### CanvasRenderingContext2D#antialias

Expand Down
69 changes: 0 additions & 69 deletions lib/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
* MIT Licensed
*/

/**
* Module dependencies.
*/

const bindings = require('./bindings')
const Canvas = module.exports = bindings.Canvas
const Context2d = require('./context2d')
Expand All @@ -24,15 +20,6 @@ Canvas.prototype[util.inspect.custom || 'inspect'] = function () {
return `[Canvas ${this.width}x${this.height}]`
}

/**
* Get a context object.
*
* @param {String} contextType must be "2d"
* @param {Object {alpha: boolean, pixelFormat: PIXEL_FORMAT} } contextAttributes Optional
* @return {Context2d}
* @api public
*/

Canvas.prototype.getContext = function (contextType, contextAttributes) {
if ('2d' == contextType) {
var ctx = this._context2d || (this._context2d = new Context2d(this, contextAttributes));
Expand All @@ -42,77 +29,21 @@ Canvas.prototype.getContext = function (contextType, contextAttributes) {
}
};

/**
* Create a `PNGStream` for `this` canvas.
*
* @param {Object} [options]
* @param {number} [options.compressionLevel] Number from 0 to 9 corresponding
* to the ZLIB compression level. Defaults to 6.
* @param {number} [options.filters] Any bitwise combination of
* `PNG_FILTER_NONE`, `PNG_FITLER_SUB`, `PNG_FILTER_UP`, `PNG_FILTER_AVG`,
* `PNG_FILTER_PATETH`; or one of `PNG_ALL_FILTERS` or `PNG_NO_FILTERS`. These
* specify which filters *may* be used by libpng. During encoding, libpng will
* select the best filter from this list of allowed filters.
* @param {Uint8ClampedArray} [options.palette] Provide for indexed PNG
* encoding. Entries should be R-G-B-A values.
* @param {number} [options.backgroundIndex] Optional index of background color
* for indexed PNGs. Defaults to 0.
* @return {PNGStream}
* @public
*/
Canvas.prototype.pngStream =
Canvas.prototype.createPNGStream = function(options){
return new PNGStream(this, options);
};

/**
* Create a `PDFStream` for `this` canvas.
*
* @param {object} [options]
* @param {string} [options.title]
* @param {string} [options.author]
* @param {string} [options.subject]
* @param {string} [options.keywords]
* @param {string} [options.creator]
* @param {Date} [options.creationDate]
* @param {Date} [options.modDate]
* @return {PDFStream}
* @public
*/
Canvas.prototype.pdfStream =
Canvas.prototype.createPDFStream = function(options){
return new PDFStream(this, options);
};

/**
* Create a `JPEGStream` for `this` canvas.
*
* @param {Object} [options]
* @param {number} [options.quality] Number from 0 to 1. Defaults to 0.75.
* @param {boolean|1|2} [options.chromaSubsampling] Enables 2x2 chroma
* subsampling. `true` is equivalent to `2`, `false` is equivalent to `1`.
* @param {boolean} [options.progressive] Enables progressive encoding. Defautls
* to false.
* @return {JPEGStream}
* @public
*/
Canvas.prototype.jpegStream =
Canvas.prototype.createJPEGStream = function(options){
return new JPEGStream(this, options);
};

/**
* Returns a data URI. Pass a function for async operation (non-standard).
*
* @param {"image/png"|"image/jpeg"} [type="image/png"] Type.
* @param {Object|Number} [encoderOptions] A number between 0 and 1 indicating
* image quality if the requested type is image/jpeg (standard), or an options
* object for image encoding (see documentation for Canvas#toBuffer)
* (non-standard).
* @param {Function} [fn] Callback for asynchronous operation (non-standard).
* @return {String} data URL if synchronous (callback omitted)
* @api public
*/
Canvas.prototype.toDataURL = function(a1, a2, a3){
// valid arg patterns (args -> [type, opts, fn]):
// [] -> ['image/png', null, null]
Expand Down
20 changes: 0 additions & 20 deletions lib/jpegstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,9 @@
* MIT Licensed
*/

/**
* Module dependencies.
*/

var Readable = require('stream').Readable;
var util = require('util');

/**
* Initialize a `JPEGStream` with the given `canvas`.
*
* "data" events are emitted with `Buffer` chunks, once complete the
* "end" event is emitted. The following example will stream to a file
* named "./my.jpeg".
*
* var out = fs.createWriteStream(__dirname + '/my.jpeg')
* , stream = canvas.createJPEGStream();
*
* stream.pipe(out);
*
* @param {Canvas} canvas
* @private
*/

var JPEGStream = module.exports = function JPEGStream(canvas, options) {
if (!(this instanceof JPEGStream)) {
throw new TypeError("Class constructors cannot be invoked without 'new'");
Expand Down
20 changes: 0 additions & 20 deletions lib/pdfstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,9 @@
* Canvas - PDFStream
*/

/**
* Module dependencies.
*/

var Readable = require('stream').Readable;
var util = require('util');

/**
* Initialize a `PDFStream` with the given `canvas`.
*
* "data" events are emitted with `Buffer` chunks, once complete the
* "end" event is emitted. The following example will stream to a file
* named "./my.pdf".
*
* var out = fs.createWriteStream(__dirname + '/my.pdf')
* , stream = canvas.createPDFStream();
*
* stream.pipe(out);
*
* @param {Canvas} canvas
* @api public
*/

var PDFStream = module.exports = function PDFStream(canvas, options) {
if (!(this instanceof PDFStream)) {
throw new TypeError("Class constructors cannot be invoked without 'new'");
Expand Down
9 changes: 0 additions & 9 deletions lib/pngstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,9 @@
* MIT Licensed
*/

/**
* Module dependencies.
*/

var Readable = require('stream').Readable;
var util = require('util');

/**
* @param {Canvas} canvas
* @param {Object} options
* @private
*/
var PNGStream = module.exports = function PNGStream(canvas, options) {
if (!(this instanceof PNGStream)) {
throw new TypeError("Class constructors cannot be invoked without 'new'");
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"test": "mocha test/*.test.js",
"pretest-server": "node-gyp build",
"test-server": "node test/server.js",
"install": "node-pre-gyp install --fallback-to-build"
"install": "node-pre-gyp install --fallback-to-build",
"dtslint": "dtslint types"
},
"binary": {
"module_name": "canvas",
Expand All @@ -42,14 +43,18 @@
"binding.gyp",
"lib/",
"src/",
"util/"
"util/",
"types/index.d.ts"
],
"types": "types",
"dependencies": {
"nan": "^2.12.1",
"node-pre-gyp": "^0.11.0"
},
"devDependencies": {
"@types/node": "^10.12.18",
"assert-rejects": "^1.0.0",
"dtslint": "^0.5.3",
"express": "^4.16.3",
"mocha": "^5.2.0",
"pixelmatch": "^4.0.2",
Expand Down
3 changes: 3 additions & 0 deletions types/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Notes:

* `"unified-signatures": false` because of https://github.com/Microsoft/dtslint/issues/183
Loading