@leafer/canvas 1.9.1 → 1.9.3
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 +9 -9
- package/src/LeaferCanvasBase.ts +45 -35
- package/types/index.d.ts +8 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leafer/canvas",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.3",
|
|
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.
|
|
26
|
-
"@leafer/list": "1.9.
|
|
27
|
-
"@leafer/math": "1.9.
|
|
28
|
-
"@leafer/data": "1.9.
|
|
29
|
-
"@leafer/path": "1.9.
|
|
30
|
-
"@leafer/debug": "1.9.
|
|
31
|
-
"@leafer/platform": "1.9.
|
|
25
|
+
"@leafer/file": "1.9.3",
|
|
26
|
+
"@leafer/list": "1.9.3",
|
|
27
|
+
"@leafer/math": "1.9.3",
|
|
28
|
+
"@leafer/data": "1.9.3",
|
|
29
|
+
"@leafer/path": "1.9.3",
|
|
30
|
+
"@leafer/debug": "1.9.3",
|
|
31
|
+
"@leafer/platform": "1.9.3"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@leafer/interface": "1.9.
|
|
34
|
+
"@leafer/interface": "1.9.3"
|
|
35
35
|
}
|
|
36
36
|
}
|
package/src/LeaferCanvasBase.ts
CHANGED
|
@@ -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,
|
|
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']
|
|
@@ -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
|
-
|
|
217
|
-
if (!to) to = from
|
|
218
|
-
this.
|
|
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
|
-
|
|
234
|
-
this.
|
|
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.
|
|
248
|
+
this.setTempPixelBounds(bounds, true, true)
|
|
247
249
|
|
|
248
250
|
let alpha: number, pixel: number
|
|
249
|
-
const { context } = this, imageData = context.getImageData(
|
|
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,
|
|
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.
|
|
271
|
-
this.fillRect(
|
|
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.
|
|
279
|
-
this.strokeRect(
|
|
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
|
|
285
|
+
public clipWorld(bounds: IBoundsData, ceilPixel: boolean = true): void {
|
|
284
286
|
this.beginPath()
|
|
285
|
-
this.
|
|
286
|
-
this.rect(
|
|
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
|
|
296
|
-
this.
|
|
297
|
-
this.clearRect(
|
|
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
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
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
|
|
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
|
|
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;
|