Skip to content

Commit

Permalink
add string tags for HTMLCanvasElement, ImageData and `CanvasGradi…
Browse files Browse the repository at this point in the history
…ent`
  • Loading branch information
brettz9 committed Mar 20, 2023
1 parent fdf709a commit 6bb52ff
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
==================
### Changed
### Added
* Added string tags for `ImageData` and `CanvasGradient` to support class detection
### Fixed
* Add missing property `canvas` to the `CanvasRenderingContext2D` type
* Fixed glyph positions getting rounded, resulting text having a slight `letter-spacing` effect
Expand Down
12 changes: 12 additions & 0 deletions lib/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@ const bindings = require('../build/Release/canvas.node')

module.exports = bindings

Object.defineProperty(bindings.Canvas.prototype, Symbol.toStringTag, {
value: 'HTMLCanvasElement'
})

bindings.ImageData.prototype.toString = function () {
return '[object ImageData]'
}

Object.defineProperty(bindings.ImageData.prototype, Symbol.toStringTag, {
value: 'ImageData'
})

bindings.CanvasGradient.prototype.toString = function () {
return '[object CanvasGradient]'
}

Object.defineProperty(bindings.CanvasGradient.prototype, Symbol.toStringTag, {
value: 'CanvasGradient'
})
14 changes: 13 additions & 1 deletion test/canvas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,11 @@ describe('Canvas', function () {
assert.equal(0, imageData.data[i + 2])
assert.equal(255, imageData.data[i + 3])
})
it('Canvas has class string of `HTMLCanvasElement`', function () {
const canvas = createCanvas(20, 1)

assert.strictEqual(Object.prototype.toString.call(canvas), '[object HTMLCanvasElement]')
})

it('CanvasGradient stringifies as [object CanvasGradient]', function () {
const canvas = createCanvas(20, 1)
Expand All @@ -1447,6 +1452,13 @@ describe('Canvas', function () {
assert.strictEqual(gradient.toString(), '[object CanvasGradient]')
})

it('CanvasGradient has class string of `CanvasGradient`', function () {
const canvas = createCanvas(20, 1)
const ctx = canvas.getContext('2d')
const gradient = ctx.createLinearGradient(1, 1, 19, 1)
assert.strictEqual(Object.prototype.toString.call(gradient), '[object CanvasGradient]')
})

describe('Context2d#putImageData()', function () {
it('throws for invalid arguments', function () {
const canvas = createCanvas(2, 1)
Expand Down Expand Up @@ -1943,7 +1955,7 @@ describe('Canvas', function () {
ctx[k] = v
ctx.restore()
assert.strictEqual(ctx[k], old)

// save() doesn't modify the value:
ctx[k] = v
old = ctx[k]
Expand Down
5 changes: 5 additions & 0 deletions test/imageData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ describe('ImageData', function () {
assert.strictEqual(imageData.toString(), '[object ImageData]')
})

it('gives class string as `ImageData`', function () {
const imageData = createImageData(2, 3)
assert.strictEqual(Object.prototype.toString.call(imageData), '[object ImageData]')
})

it('should throw with invalid numeric arguments', function () {
assert.throws(() => { createImageData(0, 0) }, /width is zero/)
assert.throws(() => { createImageData(1, 0) }, /height is zero/)
Expand Down

0 comments on commit 6bb52ff

Please sign in to comment.