@leafer/canvas-web 1.0.0-alpha.21 → 1.0.0-alpha.23

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-web",
3
- "version": "1.0.0-alpha.21",
3
+ "version": "1.0.0-alpha.23",
4
4
  "description": "@leafer/canvas-web",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -19,11 +19,11 @@
19
19
  "leaferjs"
20
20
  ],
21
21
  "dependencies": {
22
- "@leafer/math": "1.0.0-alpha.21",
23
- "@leafer/event": "1.0.0-alpha.21",
24
- "@leafer/debug": "1.0.0-alpha.21"
22
+ "@leafer/math": "1.0.0-alpha.23",
23
+ "@leafer/event": "1.0.0-alpha.23",
24
+ "@leafer/debug": "1.0.0-alpha.23"
25
25
  },
26
26
  "devDependencies": {
27
- "@leafer/interface": "1.0.0-alpha.21"
27
+ "@leafer/interface": "1.0.0-alpha.23"
28
28
  }
29
29
  }
package/src/CanvasBase.ts CHANGED
@@ -19,6 +19,7 @@ function contextMethod() {
19
19
  }
20
20
  }
21
21
 
22
+ const emptyArray: number[] = []
22
23
 
23
24
  export class CanvasBase {
24
25
 
@@ -62,8 +63,12 @@ export class CanvasBase {
62
63
  @contextAttr('lineJoin')
63
64
  public strokeJoin: string
64
65
 
65
- @contextAttr('lineDash')
66
- public dashPattern: Array<number>
66
+ public set dashPattern(value: number[]) {
67
+ this.context.setLineDash(value || emptyArray)
68
+ }
69
+ public get dashPattern(): number[] {
70
+ return this.context.getLineDash()
71
+ }
67
72
 
68
73
  @contextAttr('lineDashOffset')
69
74
  public dashOffset: number
@@ -147,6 +152,9 @@ export class CanvasBase {
147
152
  @contextMethod()
148
153
  public restore(): void { }
149
154
 
155
+ @contextMethod()
156
+ public transform(_a: number, _b: number, _c: number, _d: number, _e: number, _f: number): void { }
157
+
150
158
  @contextMethod()
151
159
  public translate(_x: number, _y: number): void { }
152
160
 
@@ -29,21 +29,26 @@ export class LeaferCanvas extends CanvasBase implements ILeaferCanvas {
29
29
  public bounds: IBounds
30
30
 
31
31
  public view: HTMLCanvasElement | OffscreenCanvas
32
+ public parentView: HTMLElement
33
+
32
34
  public offscreen: boolean
33
35
 
34
36
  public recycled?: boolean
35
37
 
36
38
  protected resizeObserver: ResizeObserver
37
39
 
40
+ protected savedblendMode: string
41
+
38
42
  constructor(config?: ILeaferCanvasConfig, manager?: ICanvasManager) {
39
43
  super()
40
44
 
41
45
  if (!config) config = minSize
46
+ if (!config.pixelRatio) config.pixelRatio = devicePixelRatio
42
47
 
43
48
  this.manager = manager
44
49
  this.innerId = IncrementId.create(IncrementId.CNAVAS)
45
50
 
46
- const { view, width, height, pixelRatio, fill, hittable: hitable } = config
51
+ const { view, width, height, pixelRatio, fill, hittable } = config
47
52
  const autoLayout = !width || !height
48
53
 
49
54
  this.pixelRatio = pixelRatio
@@ -55,8 +60,9 @@ export class LeaferCanvas extends CanvasBase implements ILeaferCanvas {
55
60
  view ? this.__createViewFrom(view) : this.__createView()
56
61
  const { style } = this.view as HTMLCanvasElement
57
62
  if (fill) style.backgroundColor = fill
58
- if (!hitable) style.pointerEvents = 'none'
63
+ if (!hittable) style.pointerEvents = 'none'
59
64
  if (autoLayout) style.display || (style.display = 'block')
65
+ this.parentView = (this.view as HTMLCanvasElement).parentElement
60
66
  }
61
67
 
62
68
  this.__init()
@@ -65,7 +71,6 @@ export class LeaferCanvas extends CanvasBase implements ILeaferCanvas {
65
71
 
66
72
  protected __init(): void {
67
73
  this.context = this.view.getContext('2d') as ICanvasContext2D
68
- this.smooth = true
69
74
  this.__bindContext()
70
75
  }
71
76
 
@@ -88,7 +93,6 @@ export class LeaferCanvas extends CanvasBase implements ILeaferCanvas {
88
93
  const { style } = div
89
94
  style.position = 'absolute'
90
95
  style.top = style.bottom = style.left = style.right = '0px'
91
- style.overflow = 'hidden'
92
96
  document.body.appendChild(div)
93
97
  parent = div
94
98
  }
@@ -111,6 +115,19 @@ export class LeaferCanvas extends CanvasBase implements ILeaferCanvas {
111
115
  }
112
116
  }
113
117
 
118
+ public debug(): void {
119
+ const panel = document.createElement('div')
120
+ panel.style.position = 'absolute'
121
+ panel.style.top = '10px'
122
+ panel.style.left = '10px'
123
+ panel.style.transform = 'scale(0.5)'
124
+ panel.style.transformOrigin = '0px 0px'
125
+ panel.style.pointerEvents = 'none'
126
+ panel.style.zIndex = '100'
127
+ document.body.appendChild(panel)
128
+ panel.appendChild(this.view as HTMLCanvasElement)
129
+ }
130
+
114
131
  public pixel(num: number): number { return num * this.pixelRatio }
115
132
 
116
133
  public startAutoLayout(autoBounds: IAutoBounds, listener: IResizeEventListener): void {
@@ -137,7 +154,7 @@ export class LeaferCanvas extends CanvasBase implements ILeaferCanvas {
137
154
  }
138
155
  })
139
156
 
140
- const parent = view.parentElement
157
+ const parent = this.parentView
141
158
  if (parent) {
142
159
  this.resizeObserver.observe(parent)
143
160
  check(parent.getBoundingClientRect())
@@ -173,6 +190,7 @@ export class LeaferCanvas extends CanvasBase implements ILeaferCanvas {
173
190
 
174
191
  this.view.width = width * pixelRatio
175
192
  this.view.height = height * pixelRatio
193
+ this.smooth = false
176
194
 
177
195
  if (this.context && takeCanvas) {
178
196
  this.copyWorld(takeCanvas)
@@ -180,6 +198,15 @@ export class LeaferCanvas extends CanvasBase implements ILeaferCanvas {
180
198
  }
181
199
  }
182
200
 
201
+ public saveBlendMode(blendMode: string): void {
202
+ this.savedblendMode = this.blendMode
203
+ this.blendMode = blendMode
204
+ }
205
+
206
+ public restoreBlendMode(): void {
207
+ this.blendMode = this.savedblendMode
208
+ }
209
+
183
210
  public setWorld(matrix: IMatrixData, parentMatrix?: IMatrixData, onlyTranslate?: boolean): void {
184
211
  const { pixelRatio } = this
185
212
  if (parentMatrix) {
@@ -231,11 +258,12 @@ export class LeaferCanvas extends CanvasBase implements ILeaferCanvas {
231
258
  this.strokeCap = options.strokeCap
232
259
  this.strokeJoin = options.strokeJoin
233
260
  this.dashPattern = options.dashPattern
261
+ this.dashOffset = options.dashOffset
234
262
  this.miterLimit = options.miterLimit
235
263
  }
236
264
  }
237
265
 
238
- public hitPath(point: IPointData, fillRule?: IWindingRule): boolean {
266
+ public hitFill(point: IPointData, fillRule?: IWindingRule): boolean {
239
267
  return this.context.isPointInPath(point.x, point.y, fillRule)
240
268
  }
241
269
 
@@ -259,59 +287,56 @@ export class LeaferCanvas extends CanvasBase implements ILeaferCanvas {
259
287
 
260
288
 
261
289
  public copyWorld(canvas: ILeaferCanvas, from?: IBoundsData, to?: IBoundsData, blendMode?: string): void {
290
+ if (blendMode) this.blendMode = blendMode
262
291
  if (from) {
263
- if (!to) to = from
264
292
  const { pixelRatio } = this
265
- if (blendMode) this.blendMode = blendMode
293
+ if (!to) to = from
266
294
  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)
267
- if (blendMode) this.blendMode = 'normal'
268
295
  } else {
269
296
  this.drawImage(canvas.view as HTMLCanvasElement, 0, 0)
270
297
  }
271
-
298
+ if (blendMode) this.blendMode = 'normal'
272
299
  }
273
300
 
274
- public copyWorldToLocal(canvas: ILeaferCanvas, fromWorld: IMatrixWithBoundsData, toLocalBounds: IBoundsData, blendMode?: string): void {
275
- const { pixelRatio } = this
301
+ public copyWorldToInner(canvas: ILeaferCanvas, fromWorld: IMatrixWithBoundsData, toInnerBounds: IBoundsData, blendMode?: string): void {
276
302
  if (blendMode) this.blendMode = blendMode
277
303
  if (fromWorld.b || fromWorld.c) {
278
304
  this.save()
279
305
  this.resetTransform()
280
- this.copyWorld(canvas, fromWorld, BoundsHelper.tempToWorld(toLocalBounds, fromWorld))
306
+ this.copyWorld(canvas, fromWorld, BoundsHelper.tempToOuterOf(toInnerBounds, fromWorld))
281
307
  this.restore()
282
308
  } else {
283
- this.drawImage(canvas.view as HTMLCanvasElement, fromWorld.x * pixelRatio, fromWorld.y * pixelRatio, fromWorld.width * pixelRatio, fromWorld.height * pixelRatio, toLocalBounds.x, toLocalBounds.y, toLocalBounds.width, toLocalBounds.height)
309
+ const { pixelRatio } = this
310
+ 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)
284
311
  }
285
312
  if (blendMode) this.blendMode = 'normal'
286
313
  }
287
314
 
288
315
  public fillWorld(bounds: IBoundsData, color: string | object, blendMode?: string): void {
289
- const { pixelRatio } = this
290
316
  if (blendMode) this.blendMode = blendMode
291
317
  this.fillStyle = color
292
- this.fillRect(bounds.x * pixelRatio, bounds.y * pixelRatio, bounds.width * pixelRatio, bounds.height * pixelRatio)
318
+ temp.copy(bounds).scale(this.pixelRatio)
319
+ this.fillRect(temp.x, temp.y, temp.width, temp.height)
293
320
  if (blendMode) this.blendMode = 'normal'
294
321
  }
295
322
 
296
323
  public strokeWorld(bounds: IBoundsData, color: string | object, blendMode?: string): void {
297
- const { pixelRatio } = this
298
324
  if (blendMode) this.blendMode = blendMode
299
325
  this.strokeStyle = color
300
- this.strokeRect(bounds.x * pixelRatio, bounds.y * pixelRatio, bounds.width * pixelRatio, bounds.height * pixelRatio)
326
+ temp.copy(bounds).scale(this.pixelRatio)
327
+ this.strokeRect(temp.x, temp.y, temp.width, temp.height)
301
328
  if (blendMode) this.blendMode = 'normal'
302
329
  }
303
330
 
304
331
  public clearWorld(bounds: IBoundsData, ceilPixel?: boolean): void {
305
- const { pixelRatio } = this
306
- temp.copy(bounds).scale(pixelRatio)
332
+ temp.copy(bounds).scale(this.pixelRatio)
307
333
  if (ceilPixel) temp.ceil()
308
334
  this.clearRect(temp.x, temp.y, temp.width, temp.height)
309
335
  }
310
336
 
311
337
  public clipWorld(bounds: IBoundsData, ceilPixel?: boolean): void {
312
- const { pixelRatio } = this
313
338
  this.beginPath()
314
- temp.copy(bounds).scale(pixelRatio)
339
+ temp.copy(bounds).scale(this.pixelRatio)
315
340
  if (ceilPixel) temp.ceil()
316
341
  this.rect(temp.x, temp.y, temp.width, temp.height)
317
342
  this.clip()
@@ -355,6 +380,14 @@ export class LeaferCanvas extends CanvasBase implements ILeaferCanvas {
355
380
  this.manager.recycle(this)
356
381
  }
357
382
 
383
+ public unloadView(): void {
384
+ if (this.parentView) {
385
+ const view = this.view as HTMLCanvasElement
386
+ const fill = view.style.backgroundColor
387
+ if (fill) this.parentView.style.backgroundColor = fill
388
+ view.remove() // App needs to use
389
+ }
390
+ }
358
391
 
359
392
  public destroy(): void {
360
393
  if (this.view) {
@@ -366,6 +399,7 @@ export class LeaferCanvas extends CanvasBase implements ILeaferCanvas {
366
399
  }
367
400
  this.manager = null
368
401
  this.view = null
402
+ this.parentView = null
369
403
  this.context = null
370
404
  }
371
405
  }