@napi-rs/canvas 0.1.10 → 0.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## [0.1.11](https://github.com/Brooooooklyn/canvas/compare/v0.1.10...v0.1.11) (2021-09-30)
2
+
3
+ ### Features
4
+
5
+ - support colorSpace: 'display-p3' ([4b64310](https://github.com/Brooooooklyn/canvas/commit/4b64310ff3adb41888e7f4bfae7f3557c062620c))
6
+
1
7
  ## [0.1.10](https://github.com/Brooooooklyn/canvas/compare/v0.1.9...v0.1.10) (2021-09-27)
2
8
 
3
9
  ### Bug Fixes
package/index.d.ts CHANGED
@@ -177,11 +177,15 @@ export class ImageData {
177
177
  */
178
178
  readonly width: number
179
179
 
180
- constructor(sw: number, sh: number)
180
+ constructor(sw: number, sh: number, attr?: { colorSpace?: ColorSpace })
181
+ constructor(imageData: ImageData, attr?: { colorSpace?: ColorSpace })
181
182
  constructor(data: Uint8ClampedArray, sw: number, sh?: number)
182
183
  }
183
184
 
184
185
  export class Image {
186
+ constructor()
187
+ // attrs only affects SVG
188
+ constructor(width: number, height: number, attrs?: { colorSpace?: ColorSpace })
185
189
  width: number
186
190
  height: number
187
191
  readonly naturalWidth: number
@@ -277,10 +281,17 @@ export interface SKRSContext2D
277
281
  }
278
282
  }
279
283
 
284
+ export type ColorSpace = 'srgb' | 'display-p3'
285
+
286
+ export interface ContextAttributes {
287
+ alpha?: boolean
288
+ colorSpace?: ColorSpace
289
+ }
290
+
280
291
  export interface SvgCanvas {
281
292
  width: number
282
293
  height: number
283
- getContext(contextType: '2d', contextAttributes?: { alpha: boolean }): SKRSContext2D
294
+ getContext(contextType: '2d', contextAttributes?: ContextAttributes): SKRSContext2D
284
295
 
285
296
  getContent(): Buffer
286
297
  }
@@ -290,7 +301,7 @@ export class Canvas {
290
301
 
291
302
  width: number
292
303
  height: number
293
- getContext(contextType: '2d', contextAttributes?: { alpha: boolean }): SKRSContext2D
304
+ getContext(contextType: '2d', contextAttributes?: ContextAttributes): SKRSContext2D
294
305
  encodeSync(format: 'webp' | 'jpeg', quality?: number): Buffer
295
306
  encodeSync(format: 'png'): Buffer
296
307
  encode(format: 'webp' | 'jpeg', quality?: number): Promise<Buffer>
package/index.js CHANGED
@@ -163,20 +163,17 @@ function createCanvas(width, height, flag) {
163
163
  const canvasElement = isSvgBackend ? new SVGCanvas(width, height) : new CanvasElement(width, height)
164
164
 
165
165
  let ctx
166
- canvasElement.getContext = function getContext(type, attr) {
166
+ canvasElement.getContext = function getContext(type, attr = {}) {
167
167
  if (type !== '2d') {
168
168
  throw new Error('Unsupported type')
169
169
  }
170
+ const attrs = { alpha: true, colorSpace: 'srgb', ...attr }
170
171
  ctx = ctx
171
172
  ? ctx
172
173
  : isSvgBackend
173
- ? new CanvasRenderingContext2D(this.width, this.height, GlobalFontsSingleton, flag)
174
- : new CanvasRenderingContext2D(this.width, this.height, GlobalFontsSingleton)
175
- if (attr) {
176
- createContext(ctx, this.width, this.height, attr)
177
- } else {
178
- createContext(ctx, this.width, this.height)
179
- }
174
+ ? new CanvasRenderingContext2D(this.width, this.height, GlobalFontsSingleton, attrs.colorSpace, flag)
175
+ : new CanvasRenderingContext2D(this.width, this.height, GlobalFontsSingleton, attrs.colorSpace)
176
+ createContext(ctx, this.width, this.height, attrs)
180
177
 
181
178
  // napi can not define writable: true but enumerable: false property
182
179
  Object.defineProperty(ctx, '_fillStyle', {
@@ -194,11 +191,11 @@ function createCanvas(width, height, flag) {
194
191
  })
195
192
 
196
193
  Object.defineProperty(ctx, 'createImageData', {
197
- value: function createImageData(widthOrImage, height) {
194
+ value: function createImageData(widthOrImage, height, attrs = {}) {
198
195
  if (widthOrImage instanceof ImageData) {
199
- return new ImageData(widthOrImage.width, widthOrImage.height)
196
+ return new ImageData(widthOrImage.data, widthOrImage.width, widthOrImage.height)
200
197
  }
201
- return new ImageData(widthOrImage, height)
198
+ return new ImageData(widthOrImage, height, { colorSpace: 'srgb', ...attrs })
202
199
  },
203
200
  configurable: false,
204
201
  enumerable: false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@napi-rs/canvas",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "description": "Canvas for Node.js with skia backend",
5
5
  "main": "index.js",
6
6
  "repository": "https://github.com/Brooooooklyn/canvas.git",
@@ -134,14 +134,14 @@
134
134
  "parser": "typescript"
135
135
  },
136
136
  "optionalDependencies": {
137
- "@napi-rs/canvas-win32-x64-msvc": "0.1.10",
138
- "@napi-rs/canvas-darwin-x64": "0.1.10",
139
- "@napi-rs/canvas-linux-x64-gnu": "0.1.10",
140
- "@napi-rs/canvas-linux-arm-gnueabihf": "0.1.10",
141
- "@napi-rs/canvas-linux-x64-musl": "0.1.10",
142
- "@napi-rs/canvas-linux-arm64-gnu": "0.1.10",
143
- "@napi-rs/canvas-linux-arm64-musl": "0.1.10",
144
- "@napi-rs/canvas-darwin-arm64": "0.1.10",
145
- "@napi-rs/canvas-android-arm64": "0.1.10"
137
+ "@napi-rs/canvas-win32-x64-msvc": "0.1.11",
138
+ "@napi-rs/canvas-darwin-x64": "0.1.11",
139
+ "@napi-rs/canvas-linux-x64-gnu": "0.1.11",
140
+ "@napi-rs/canvas-linux-arm-gnueabihf": "0.1.11",
141
+ "@napi-rs/canvas-linux-x64-musl": "0.1.11",
142
+ "@napi-rs/canvas-linux-arm64-gnu": "0.1.11",
143
+ "@napi-rs/canvas-linux-arm64-musl": "0.1.11",
144
+ "@napi-rs/canvas-darwin-arm64": "0.1.11",
145
+ "@napi-rs/canvas-android-arm64": "0.1.11"
146
146
  }
147
147
  }