@leafer/canvas 1.0.9 → 1.1.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer/canvas",
3
- "version": "1.0.9",
3
+ "version": "1.1.0",
4
4
  "description": "@leafer/canvas",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -22,15 +22,15 @@
22
22
  "leaferjs"
23
23
  ],
24
24
  "dependencies": {
25
- "@leafer/file": "1.0.9",
26
- "@leafer/list": "1.0.9",
27
- "@leafer/math": "1.0.9",
28
- "@leafer/data": "1.0.9",
29
- "@leafer/path": "1.0.9",
30
- "@leafer/debug": "1.0.9",
31
- "@leafer/platform": "1.0.9"
25
+ "@leafer/file": "1.1.0",
26
+ "@leafer/list": "1.1.0",
27
+ "@leafer/math": "1.1.0",
28
+ "@leafer/data": "1.1.0",
29
+ "@leafer/path": "1.1.0",
30
+ "@leafer/debug": "1.1.0",
31
+ "@leafer/platform": "1.1.0"
32
32
  },
33
33
  "devDependencies": {
34
- "@leafer/interface": "1.0.9"
34
+ "@leafer/interface": "1.1.0"
35
35
  }
36
36
  }
@@ -1,4 +1,4 @@
1
- import { IBounds, ILeaferCanvas, ICanvasStrokeOptions, ILeaferCanvasConfig, IExportOptions, IMatrixData, IBoundsData, IAutoBounds, IScreenSizeData, IResizeEventListener, IMatrixWithBoundsData, IPointData, InnerId, ICanvasManager, IWindingRule, IBlendMode, IExportImageType, IExportFileType, IBlob, ICursorType, ILeaferCanvasView, IRadiusPointData } from '@leafer/interface'
1
+ import { IBounds, ILeaferCanvas, ICanvasStrokeOptions, ILeaferCanvasConfig, IExportOptions, IMatrixData, IBoundsData, IAutoBounds, IScreenSizeData, IResizeEventListener, IMatrixWithBoundsData, IPointData, InnerId, ICanvasManager, IWindingRule, IBlendMode, IExportImageType, IExportFileType, IBlob, ICursorType, ILeaferCanvasView, IRadiusPointData, IObject } from '@leafer/interface'
2
2
  import { Bounds, tempBounds, BoundsHelper, MatrixHelper, IncrementId } from '@leafer/math'
3
3
  import { Creator, Platform } from '@leafer/platform'
4
4
  import { DataHelper } from '@leafer/data'
@@ -95,8 +95,9 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
95
95
  takeCanvas.copyWorld(this)
96
96
  }
97
97
 
98
- DataHelper.copyAttrs(this.size, size, canvasSizeAttrs)
99
- this.size.pixelRatio || (this.size.pixelRatio = 1)
98
+ const s = this.size as IObject
99
+ DataHelper.copyAttrs(s, size, canvasSizeAttrs)
100
+ canvasSizeAttrs.forEach(key => s[key] || (s[key] = 1)) // fix: width = 0 or height = 0
100
101
 
101
102
  this.bounds = new Bounds(0, 0, this.width, this.height)
102
103
 
@@ -239,6 +240,20 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
239
240
  if (!onlyResetTransform) this.useWorldTransform() // restore world transform
240
241
  }
241
242
 
243
+ public useGrayscaleAlpha(bounds: IBoundsData): void { // 先实现功能,后期需通过 shader 优化性能
244
+ this.setTempBounds(bounds, true, true)
245
+
246
+ let alpha: number, pixel: number
247
+ const { context } = this, imageData = context.getImageData(tempBounds.x, tempBounds.y, tempBounds.width, tempBounds.height), { data } = imageData
248
+
249
+ for (let i = 0, len = data.length; i < len; i += 4) {
250
+ pixel = data[i] * 0.299 + data[i + 1] * 0.587 + data[i + 2] * 0.114
251
+ if (alpha = data[i + 3]) data[i + 3] = alpha === 255 ? pixel : alpha * (pixel / 255)
252
+ }
253
+
254
+ context.putImageData(imageData, tempBounds.x, tempBounds.y)
255
+ }
256
+
242
257
  public useMask(maskCanvas: ILeaferCanvas, fromBounds?: IBoundsData, toBounds?: IBoundsData): void {
243
258
  this.copyWorld(maskCanvas, fromBounds, toBounds, 'destination-in')
244
259
  }
@@ -250,7 +265,7 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
250
265
  public fillWorld(bounds: IBoundsData, color: string | object, blendMode?: IBlendMode): void {
251
266
  if (blendMode) this.blendMode = blendMode
252
267
  this.fillStyle = color
253
- tempBounds.set(bounds).scale(this.pixelRatio)
268
+ this.setTempBounds(bounds)
254
269
  this.fillRect(tempBounds.x, tempBounds.y, tempBounds.width, tempBounds.height)
255
270
  if (blendMode) this.blendMode = 'source-over'
256
271
  }
@@ -258,21 +273,19 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
258
273
  public strokeWorld(bounds: IBoundsData, color: string | object, blendMode?: IBlendMode): void {
259
274
  if (blendMode) this.blendMode = blendMode
260
275
  this.strokeStyle = color
261
- tempBounds.set(bounds).scale(this.pixelRatio)
276
+ this.setTempBounds(bounds)
262
277
  this.strokeRect(tempBounds.x, tempBounds.y, tempBounds.width, tempBounds.height)
263
278
  if (blendMode) this.blendMode = 'source-over'
264
279
  }
265
280
 
266
281
  public clearWorld(bounds: IBoundsData, ceilPixel?: boolean): void {
267
- tempBounds.set(bounds).scale(this.pixelRatio)
268
- if (ceilPixel) tempBounds.ceil()
282
+ this.setTempBounds(bounds, ceilPixel)
269
283
  this.clearRect(tempBounds.x, tempBounds.y, tempBounds.width, tempBounds.height)
270
284
  }
271
285
 
272
286
  public clipWorld(bounds: IBoundsData, ceilPixel?: boolean): void {
273
287
  this.beginPath()
274
- tempBounds.set(bounds).scale(this.pixelRatio)
275
- if (ceilPixel) tempBounds.ceil()
288
+ this.setTempBounds(bounds, ceilPixel)
276
289
  this.rect(tempBounds.x, tempBounds.y, tempBounds.width, tempBounds.height)
277
290
  this.clip()
278
291
 
@@ -286,6 +299,13 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
286
299
 
287
300
  // other
288
301
 
302
+ protected setTempBounds(bounds: IBoundsData, ceil?: boolean, intersect?: boolean): void {
303
+ tempBounds.set(bounds)
304
+ if (intersect) tempBounds.intersect(this.bounds)
305
+ tempBounds.scale(this.pixelRatio)
306
+ if (ceil) tempBounds.ceil()
307
+ }
308
+
289
309
  public isSameSize(size: IScreenSizeData): boolean {
290
310
  return this.width === size.width && this.height === size.height && this.pixelRatio === size.pixelRatio
291
311
  }
package/types/index.d.ts CHANGED
@@ -130,6 +130,7 @@ declare class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
130
130
  copyWorld(canvas: ILeaferCanvas, from?: IBoundsData, to?: IBoundsData, blendMode?: IBlendMode): void;
131
131
  copyWorldToInner(canvas: ILeaferCanvas, fromWorld: IMatrixWithBoundsData, toInnerBounds: IBoundsData, blendMode?: IBlendMode): void;
132
132
  copyWorldByReset(canvas: ILeaferCanvas, from?: IBoundsData, to?: IBoundsData, blendMode?: IBlendMode, onlyResetTransform?: boolean): void;
133
+ useGrayscaleAlpha(bounds: IBoundsData): void;
133
134
  useMask(maskCanvas: ILeaferCanvas, fromBounds?: IBoundsData, toBounds?: IBoundsData): void;
134
135
  useEraser(eraserCanvas: ILeaferCanvas, fromBounds?: IBoundsData, toBounds?: IBoundsData): void;
135
136
  fillWorld(bounds: IBoundsData, color: string | object, blendMode?: IBlendMode): void;
@@ -137,6 +138,7 @@ declare class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
137
138
  clearWorld(bounds: IBoundsData, ceilPixel?: boolean): void;
138
139
  clipWorld(bounds: IBoundsData, ceilPixel?: boolean): void;
139
140
  clear(): void;
141
+ protected setTempBounds(bounds: IBoundsData, ceil?: boolean, intersect?: boolean): void;
140
142
  isSameSize(size: IScreenSizeData): boolean;
141
143
  getSameCanvas(useSameWorldTransform?: boolean, useSameSmooth?: boolean): ILeaferCanvas;
142
144
  recycle(clearBounds?: IBoundsData): void;