@leafer/canvas 1.9.0 → 1.9.2

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.9.0",
3
+ "version": "1.9.2",
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.9.0",
26
- "@leafer/list": "1.9.0",
27
- "@leafer/math": "1.9.0",
28
- "@leafer/data": "1.9.0",
29
- "@leafer/path": "1.9.0",
30
- "@leafer/debug": "1.9.0",
31
- "@leafer/platform": "1.9.0"
25
+ "@leafer/file": "1.9.2",
26
+ "@leafer/list": "1.9.2",
27
+ "@leafer/math": "1.9.2",
28
+ "@leafer/data": "1.9.2",
29
+ "@leafer/path": "1.9.2",
30
+ "@leafer/debug": "1.9.2",
31
+ "@leafer/platform": "1.9.2"
32
32
  },
33
33
  "devDependencies": {
34
- "@leafer/interface": "1.9.0"
34
+ "@leafer/interface": "1.9.2"
35
35
  }
36
36
  }
@@ -1,12 +1,12 @@
1
1
  import { IBounds, ILeaferCanvas, ICanvasStrokeOptions, ILeaferCanvasConfig, IWindingRuleData, IExportOptions, IMatrixData, IBoundsData, IAutoBounds, IScreenSizeData, IResizeEventListener, IMatrixWithBoundsData, IPointData, InnerId, ICanvasManager, IWindingRule, IBlendMode, IExportImageType, IExportFileType, IBlob, ICursorType, ILeaferCanvasView, IRadiusPointData, IObject, IMatrixWithOptionHalfData } from '@leafer/interface'
2
- import { Bounds, tempBounds, BoundsHelper, MatrixHelper, IncrementId } from '@leafer/math'
2
+ import { Bounds, BoundsHelper, MatrixHelper, IncrementId } from '@leafer/math'
3
3
  import { Creator, Platform } from '@leafer/platform'
4
4
  import { DataHelper, isUndefined } from '@leafer/data'
5
5
 
6
6
  import { Canvas } from './Canvas'
7
7
 
8
8
 
9
- const { copy, multiplyParent } = MatrixHelper, { round } = Math
9
+ const { copy, multiplyParent } = MatrixHelper, { round } = Math, tempPixelBounds = new Bounds(), tempPixelBounds2 = new Bounds()
10
10
  const minSize: IScreenSizeData = { width: 1, height: 1, pixelRatio: 1 }
11
11
 
12
12
  export const canvasSizeAttrs = ['width', 'height', 'pixelRatio']
@@ -61,7 +61,7 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
61
61
  const { width, height, pixelRatio } = config
62
62
  this.autoLayout = !width || !height
63
63
 
64
- this.size.pixelRatio = pixelRatio | Platform.devicePixelRatio
64
+ this.size.pixelRatio = pixelRatio || Platform.devicePixelRatio
65
65
  this.config = config
66
66
 
67
67
  this.init()
@@ -210,50 +210,52 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
210
210
  }
211
211
 
212
212
 
213
- public copyWorld(canvas: ILeaferCanvas, from?: IBoundsData, to?: IBoundsData, blendMode?: IBlendMode): void {
213
+ public copyWorld(canvas: ILeaferCanvas, from?: IBoundsData, to?: IBoundsData, blendMode?: IBlendMode, ceilPixel: boolean = true): void {
214
214
  if (blendMode) this.blendMode = blendMode
215
215
  if (from) {
216
- const { pixelRatio } = this
217
- if (!to) to = from
218
- this.drawImage(canvas.view as HTMLCanvasElement, from.x * pixelRatio, from.y * pixelRatio, from.width * pixelRatio, from.height * pixelRatio, to.x * pixelRatio, to.y * pixelRatio, to.width * pixelRatio, to.height * pixelRatio)
216
+ this.setTempPixelBounds(from, ceilPixel)
217
+ if (!to) to = tempPixelBounds // to = from
218
+ else this.setTempPixelBounds2(to, ceilPixel), to = tempPixelBounds2
219
+
220
+ this.drawImage(canvas.view as HTMLCanvasElement, tempPixelBounds.x, tempPixelBounds.y, tempPixelBounds.width, tempPixelBounds.height, to.x, to.y, to.width, to.height)
219
221
  } else {
220
222
  this.drawImage(canvas.view as HTMLCanvasElement, 0, 0)
221
223
  }
222
224
  if (blendMode) this.blendMode = 'source-over'
223
225
  }
224
226
 
225
- public copyWorldToInner(canvas: ILeaferCanvas, fromWorld: IMatrixWithBoundsData, toInnerBounds: IBoundsData, blendMode?: IBlendMode): void {
226
- if (blendMode) this.blendMode = blendMode
227
+ public copyWorldToInner(canvas: ILeaferCanvas, fromWorld: IMatrixWithBoundsData, toInnerBounds: IBoundsData, blendMode?: IBlendMode, ceilPixel: boolean = true): void {
227
228
  if (fromWorld.b || fromWorld.c) {
228
229
  this.save()
229
230
  this.resetTransform()
230
- this.copyWorld(canvas, fromWorld, BoundsHelper.tempToOuterOf(toInnerBounds, fromWorld))
231
+ this.copyWorld(canvas, fromWorld, BoundsHelper.tempToOuterOf(toInnerBounds, fromWorld), blendMode, ceilPixel)
231
232
  this.restore()
232
233
  } else {
233
- const { pixelRatio } = this
234
- this.drawImage(canvas.view as HTMLCanvasElement, fromWorld.x * pixelRatio, fromWorld.y * pixelRatio, fromWorld.width * pixelRatio, fromWorld.height * pixelRatio, toInnerBounds.x, toInnerBounds.y, toInnerBounds.width, toInnerBounds.height)
234
+ if (blendMode) this.blendMode = blendMode
235
+ this.setTempPixelBounds(fromWorld, ceilPixel)
236
+ this.drawImage(canvas.view as HTMLCanvasElement, tempPixelBounds.x, tempPixelBounds.y, tempPixelBounds.width, tempPixelBounds.height, toInnerBounds.x, toInnerBounds.y, toInnerBounds.width, toInnerBounds.height)
237
+ if (blendMode) this.blendMode = 'source-over'
235
238
  }
236
- if (blendMode) this.blendMode = 'source-over'
237
239
  }
238
240
 
239
- public copyWorldByReset(canvas: ILeaferCanvas, from?: IBoundsData, to?: IBoundsData, blendMode?: IBlendMode, onlyResetTransform?: boolean): void {
241
+ public copyWorldByReset(canvas: ILeaferCanvas, from?: IBoundsData, to?: IBoundsData, blendMode?: IBlendMode, onlyResetTransform?: boolean, ceilPixel: boolean = true): void {
240
242
  this.resetTransform()
241
- this.copyWorld(canvas, from, to, blendMode)
243
+ this.copyWorld(canvas, from, to, blendMode, ceilPixel)
242
244
  if (!onlyResetTransform) this.useWorldTransform() // restore world transform
243
245
  }
244
246
 
245
247
  public useGrayscaleAlpha(bounds: IBoundsData): void { // 先实现功能,后期需通过 shader 优化性能
246
- this.setTempBounds(bounds, true, true)
248
+ this.setTempPixelBounds(bounds, true, true)
247
249
 
248
250
  let alpha: number, pixel: number
249
- const { context } = this, imageData = context.getImageData(tempBounds.x, tempBounds.y, tempBounds.width, tempBounds.height), { data } = imageData
251
+ const { context } = this, imageData = context.getImageData(tempPixelBounds.x, tempPixelBounds.y, tempPixelBounds.width, tempPixelBounds.height), { data } = imageData
250
252
 
251
253
  for (let i = 0, len = data.length; i < len; i += 4) {
252
254
  pixel = data[i] * 0.299 + data[i + 1] * 0.587 + data[i + 2] * 0.114
253
255
  if (alpha = data[i + 3]) data[i + 3] = alpha === 255 ? pixel : alpha * (pixel / 255)
254
256
  }
255
257
 
256
- context.putImageData(imageData, tempBounds.x, tempBounds.y)
258
+ context.putImageData(imageData, tempPixelBounds.x, tempPixelBounds.y)
257
259
  }
258
260
 
259
261
  public useMask(maskCanvas: ILeaferCanvas, fromBounds?: IBoundsData, toBounds?: IBoundsData): void {
@@ -264,26 +266,26 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
264
266
  this.copyWorld(eraserCanvas, fromBounds, toBounds, 'destination-out')
265
267
  }
266
268
 
267
- public fillWorld(bounds: IBoundsData, color: string | object, blendMode?: IBlendMode): void {
269
+ public fillWorld(bounds: IBoundsData, color: string | object, blendMode?: IBlendMode, ceilPixel?: boolean): void {
268
270
  if (blendMode) this.blendMode = blendMode
269
271
  this.fillStyle = color
270
- this.setTempBounds(bounds)
271
- this.fillRect(tempBounds.x, tempBounds.y, tempBounds.width, tempBounds.height)
272
+ this.setTempPixelBounds(bounds, ceilPixel)
273
+ this.fillRect(tempPixelBounds.x, tempPixelBounds.y, tempPixelBounds.width, tempPixelBounds.height)
272
274
  if (blendMode) this.blendMode = 'source-over'
273
275
  }
274
276
 
275
- public strokeWorld(bounds: IBoundsData, color: string | object, blendMode?: IBlendMode): void {
277
+ public strokeWorld(bounds: IBoundsData, color: string | object, blendMode?: IBlendMode, ceilPixel?: boolean): void {
276
278
  if (blendMode) this.blendMode = blendMode
277
279
  this.strokeStyle = color
278
- this.setTempBounds(bounds)
279
- this.strokeRect(tempBounds.x, tempBounds.y, tempBounds.width, tempBounds.height)
280
+ this.setTempPixelBounds(bounds, ceilPixel)
281
+ this.strokeRect(tempPixelBounds.x, tempPixelBounds.y, tempPixelBounds.width, tempPixelBounds.height)
280
282
  if (blendMode) this.blendMode = 'source-over'
281
283
  }
282
284
 
283
- public clipWorld(bounds: IBoundsData, ceilPixel?: boolean): void {
285
+ public clipWorld(bounds: IBoundsData, ceilPixel: boolean = true): void {
284
286
  this.beginPath()
285
- this.setTempBounds(bounds, ceilPixel)
286
- this.rect(tempBounds.x, tempBounds.y, tempBounds.width, tempBounds.height)
287
+ this.setTempPixelBounds(bounds, ceilPixel)
288
+ this.rect(tempPixelBounds.x, tempPixelBounds.y, tempPixelBounds.width, tempPixelBounds.height)
287
289
  this.clip()
288
290
 
289
291
  }
@@ -292,9 +294,9 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
292
294
  ruleData.windingRule ? this.clip(ruleData.windingRule) : this.clip()
293
295
  }
294
296
 
295
- public clearWorld(bounds: IBoundsData, ceilPixel?: boolean): void {
296
- this.setTempBounds(bounds, ceilPixel)
297
- this.clearRect(tempBounds.x, tempBounds.y, tempBounds.width, tempBounds.height)
297
+ public clearWorld(bounds: IBoundsData, ceilPixel: boolean = true): void {
298
+ this.setTempPixelBounds(bounds, ceilPixel)
299
+ this.clearRect(tempPixelBounds.x, tempPixelBounds.y, tempPixelBounds.width, tempPixelBounds.height)
298
300
  }
299
301
 
300
302
  public clear(): void {
@@ -305,11 +307,19 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
305
307
 
306
308
  // other
307
309
 
308
- protected setTempBounds(bounds: IBoundsData, ceil?: boolean, intersect?: boolean): void {
309
- tempBounds.set(bounds)
310
- if (intersect) tempBounds.intersect(this.bounds)
311
- tempBounds.scale(this.pixelRatio)
312
- if (ceil) tempBounds.ceil()
310
+ protected setTempPixelBounds(bounds: IBoundsData, ceil?: boolean, intersect?: boolean): void {
311
+ this.copyToPixelBounds(tempPixelBounds, bounds, ceil, intersect)
312
+ }
313
+
314
+ protected setTempPixelBounds2(bounds: IBoundsData, ceil?: boolean, intersect?: boolean): void {
315
+ this.copyToPixelBounds(tempPixelBounds2, bounds, ceil, intersect)
316
+ }
317
+
318
+ protected copyToPixelBounds(pixelBounds: IBounds, bounds: IBoundsData, ceil?: boolean, intersect?: boolean): void {
319
+ pixelBounds.set(bounds)
320
+ if (intersect) pixelBounds.intersect(this.bounds)
321
+ pixelBounds.scale(this.pixelRatio)
322
+ if (ceil) pixelBounds.ceil()
313
323
  }
314
324
 
315
325
  public isSameSize(size: IScreenSizeData): boolean {
@@ -331,7 +341,7 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
331
341
  public recycle(clearBounds?: IBoundsData): void {
332
342
  if (!this.recycled) {
333
343
  this.restore()
334
- clearBounds ? this.clearWorld(clearBounds, true) : this.clear()
344
+ clearBounds ? this.clearWorld(clearBounds) : this.clear()
335
345
  this.manager ? this.manager.recycle(this) : this.destroy()
336
346
  }
337
347
  }
package/types/index.d.ts CHANGED
@@ -129,19 +129,21 @@ declare class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
129
129
  hitPixel(_radiusPoint: IRadiusPointData, _offset?: IPointData, _scale?: number): boolean;
130
130
  setWorldShadow(x: number, y: number, blur: number, color?: string): void;
131
131
  setWorldBlur(blur: number): void;
132
- copyWorld(canvas: ILeaferCanvas, from?: IBoundsData, to?: IBoundsData, blendMode?: IBlendMode): void;
133
- copyWorldToInner(canvas: ILeaferCanvas, fromWorld: IMatrixWithBoundsData, toInnerBounds: IBoundsData, blendMode?: IBlendMode): void;
134
- copyWorldByReset(canvas: ILeaferCanvas, from?: IBoundsData, to?: IBoundsData, blendMode?: IBlendMode, onlyResetTransform?: boolean): void;
132
+ copyWorld(canvas: ILeaferCanvas, from?: IBoundsData, to?: IBoundsData, blendMode?: IBlendMode, ceilPixel?: boolean): void;
133
+ copyWorldToInner(canvas: ILeaferCanvas, fromWorld: IMatrixWithBoundsData, toInnerBounds: IBoundsData, blendMode?: IBlendMode, ceilPixel?: boolean): void;
134
+ copyWorldByReset(canvas: ILeaferCanvas, from?: IBoundsData, to?: IBoundsData, blendMode?: IBlendMode, onlyResetTransform?: boolean, ceilPixel?: boolean): void;
135
135
  useGrayscaleAlpha(bounds: IBoundsData): void;
136
136
  useMask(maskCanvas: ILeaferCanvas, fromBounds?: IBoundsData, toBounds?: IBoundsData): void;
137
137
  useEraser(eraserCanvas: ILeaferCanvas, fromBounds?: IBoundsData, toBounds?: IBoundsData): void;
138
- fillWorld(bounds: IBoundsData, color: string | object, blendMode?: IBlendMode): void;
139
- strokeWorld(bounds: IBoundsData, color: string | object, blendMode?: IBlendMode): void;
138
+ fillWorld(bounds: IBoundsData, color: string | object, blendMode?: IBlendMode, ceilPixel?: boolean): void;
139
+ strokeWorld(bounds: IBoundsData, color: string | object, blendMode?: IBlendMode, ceilPixel?: boolean): void;
140
140
  clipWorld(bounds: IBoundsData, ceilPixel?: boolean): void;
141
141
  clipUI(ruleData: IWindingRuleData): void;
142
142
  clearWorld(bounds: IBoundsData, ceilPixel?: boolean): void;
143
143
  clear(): void;
144
- protected setTempBounds(bounds: IBoundsData, ceil?: boolean, intersect?: boolean): void;
144
+ protected setTempPixelBounds(bounds: IBoundsData, ceil?: boolean, intersect?: boolean): void;
145
+ protected setTempPixelBounds2(bounds: IBoundsData, ceil?: boolean, intersect?: boolean): void;
146
+ protected copyToPixelBounds(pixelBounds: IBounds, bounds: IBoundsData, ceil?: boolean, intersect?: boolean): void;
145
147
  isSameSize(size: IScreenSizeData): boolean;
146
148
  getSameCanvas(useSameWorldTransform?: boolean, useSameSmooth?: boolean): ILeaferCanvas;
147
149
  recycle(clearBounds?: IBoundsData): void;