@leafer-ui/hit 1.0.0-rc.26 → 1.0.0-rc.28
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 +5 -5
- package/src/HitCanvasManager.ts +53 -0
- package/src/RectHit.ts +1 -1
- package/src/UIHit.ts +3 -3
- package/src/canvas.ts +24 -0
- package/src/find.ts +21 -0
- package/src/index.ts +4 -20
- package/types/index.d.ts +16 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leafer-ui/hit",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
3
|
+
"version": "1.0.0-rc.28",
|
|
4
4
|
"description": "@leafer-ui/hit",
|
|
5
5
|
"author": "Chao (Leafer) Wan",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,11 +22,11 @@
|
|
|
22
22
|
"leaferjs"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@leafer/core": "1.0.0-rc.
|
|
26
|
-
"@leafer-ui/draw": "1.0.0-rc.
|
|
25
|
+
"@leafer/core": "1.0.0-rc.28",
|
|
26
|
+
"@leafer-ui/draw": "1.0.0-rc.28"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@leafer/interface": "1.0.0-rc.
|
|
30
|
-
"@leafer-ui/interface": "1.0.0-rc.
|
|
29
|
+
"@leafer/interface": "1.0.0-rc.28",
|
|
30
|
+
"@leafer-ui/interface": "1.0.0-rc.28"
|
|
31
31
|
}
|
|
32
32
|
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { IHitCanvasManager, ILeaf, IHitCanvas, ILeafList, ILeaferCanvasConfig } from '@leafer/interface'
|
|
2
|
+
import { CanvasManager, LeafList, Creator } from '@leafer/core'
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
export class HitCanvasManager extends CanvasManager implements IHitCanvasManager {
|
|
6
|
+
|
|
7
|
+
public maxTotal = 1000 // 最多缓存多少张画布
|
|
8
|
+
|
|
9
|
+
protected pathList: ILeafList = new LeafList()
|
|
10
|
+
protected pixelList: ILeafList = new LeafList()
|
|
11
|
+
|
|
12
|
+
public getPixelType(leaf: ILeaf, config?: ILeaferCanvasConfig): IHitCanvas {
|
|
13
|
+
this.__autoClear()
|
|
14
|
+
this.pixelList.add(leaf)
|
|
15
|
+
return Creator.hitCanvas(config)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public getPathType(leaf: ILeaf): IHitCanvas {
|
|
19
|
+
this.__autoClear()
|
|
20
|
+
this.pathList.add(leaf)
|
|
21
|
+
return Creator.hitCanvas()
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public clearImageType(): void {
|
|
25
|
+
this.__clearLeafList(this.pixelList)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public clearPathType(): void {
|
|
29
|
+
this.__clearLeafList(this.pathList)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
protected __clearLeafList(leafList: ILeafList): void {
|
|
33
|
+
if (leafList.length) {
|
|
34
|
+
leafList.forEach(leaf => {
|
|
35
|
+
if (leaf.__hitCanvas) {
|
|
36
|
+
leaf.__hitCanvas.destroy()
|
|
37
|
+
leaf.__hitCanvas = null
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
leafList.reset()
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
protected __autoClear(): void {
|
|
45
|
+
if (this.pathList.length + this.pixelList.length > this.maxTotal) this.clear()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public clear(): void {
|
|
49
|
+
this.clearPathType()
|
|
50
|
+
this.clearImageType()
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
}
|
package/src/RectHit.ts
CHANGED
|
@@ -9,7 +9,7 @@ const rect = Rect.prototype
|
|
|
9
9
|
// hit
|
|
10
10
|
|
|
11
11
|
rect.__updateHitCanvas = function () {
|
|
12
|
-
if (this.stroke || this.cornerRadius || (this.fill && this.hitFill === 'pixel') || this.hitStroke === 'all') ui.__updateHitCanvas.call(this)
|
|
12
|
+
if (this.stroke || this.cornerRadius || ((this.fill || this.__.__isCanvas) && this.hitFill === 'pixel') || this.hitStroke === 'all') ui.__updateHitCanvas.call(this)
|
|
13
13
|
else if (this.__hitCanvas) this.__hitCanvas = null
|
|
14
14
|
}
|
|
15
15
|
|
package/src/UIHit.ts
CHANGED
|
@@ -9,7 +9,7 @@ const ui = UI.prototype
|
|
|
9
9
|
ui.__updateHitCanvas = function (): void {
|
|
10
10
|
const data = this.__, { hitCanvasManager } = this.leafer
|
|
11
11
|
|
|
12
|
-
const isHitPixelFill = data.__pixelFill && data.hitFill === 'pixel'
|
|
12
|
+
const isHitPixelFill = (data.__pixelFill || data.__isCanvas) && data.hitFill === 'pixel'
|
|
13
13
|
const isHitPixelStroke = data.__pixelStroke && data.hitStroke === 'pixel'
|
|
14
14
|
const isHitPixel = isHitPixelFill || isHitPixelStroke
|
|
15
15
|
|
|
@@ -51,11 +51,11 @@ ui.__hit = function (inner: IRadiusPointData): boolean {
|
|
|
51
51
|
// hit path
|
|
52
52
|
|
|
53
53
|
const { hitFill } = data
|
|
54
|
-
const needHitFillPath = (data.fill && hitFill
|
|
54
|
+
const needHitFillPath = ((data.fill || data.__isCanvas) && (hitFill === 'path' || (hitFill === 'pixel' && !(data.__pixelFill || data.__isCanvas)))) || hitFill === 'all'
|
|
55
55
|
if (needHitFillPath && this.__hitFill(inner)) return true
|
|
56
56
|
|
|
57
57
|
const { hitStroke, __strokeWidth } = data
|
|
58
|
-
const needHitStrokePath = (data.stroke &&
|
|
58
|
+
const needHitStrokePath = (data.stroke && (hitStroke === 'path' || (hitStroke === 'pixel' && !data.__pixelStroke))) || hitStroke === 'all'
|
|
59
59
|
if (!needHitFillPath && !needHitStrokePath) return false
|
|
60
60
|
|
|
61
61
|
const radiusWidth = inner.radiusX * 2
|
package/src/canvas.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
|
|
2
|
+
import { IPointData, IRadiusPointData, IWindingRule } from '@leafer/interface'
|
|
3
|
+
import { LeaferCanvasBase, tempBounds } from '@leafer/core'
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
const canvas = LeaferCanvasBase.prototype
|
|
7
|
+
|
|
8
|
+
canvas.hitFill = function (point: IPointData, fillRule?: IWindingRule): boolean {
|
|
9
|
+
return fillRule ? this.context.isPointInPath(point.x, point.y, fillRule) : this.context.isPointInPath(point.x, point.y)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
canvas.hitStroke = function (point: IPointData, strokeWidth?: number): boolean {
|
|
13
|
+
this.strokeWidth = strokeWidth
|
|
14
|
+
return this.context.isPointInStroke(point.x, point.y)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
canvas.hitPixel = function (radiusPoint: IRadiusPointData, offset?: IPointData, scale = 1): boolean { // 画布必须有alpha通道
|
|
18
|
+
let { x, y, radiusX, radiusY } = radiusPoint
|
|
19
|
+
if (offset) x -= offset.x, y -= offset.y
|
|
20
|
+
tempBounds.set(x - radiusX, y - radiusY, radiusX * 2, radiusY * 2).scale(scale).ceil()
|
|
21
|
+
const { data } = this.context.getImageData(tempBounds.x, tempBounds.y, tempBounds.width || 1, tempBounds.height || 1)
|
|
22
|
+
for (let i = 0, len = data.length; i < len; i += 4) { if (data[i + 3] > 0) return true }
|
|
23
|
+
return data[3] > 0
|
|
24
|
+
}
|
package/src/find.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
|
|
2
|
+
import { IFindMethod, IPointData, IPickOptions, IPickResult } from '@leafer/interface'
|
|
3
|
+
import { IFindUIMethod, IUI } from '@leafer-ui/interface'
|
|
4
|
+
import { UI, Group } from '@leafer-ui/draw'
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
const ui = UI.prototype, group = Group.prototype
|
|
8
|
+
|
|
9
|
+
ui.find = function (condition: number | string | IFindUIMethod, options?: any): IUI[] {
|
|
10
|
+
return this.leafer ? this.leafer.selector.getBy(condition as IFindMethod, this, false, options) as IUI[] : []
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
ui.findOne = function (condition: number | string | IFindUIMethod, options?: any): IUI | undefined {
|
|
14
|
+
return this.leafer ? this.leafer.selector.getBy(condition as IFindMethod, this, true, options) as IUI : null
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
group.pick = function (hitPoint: IPointData, options?: IPickOptions): IPickResult {
|
|
18
|
+
this.__layout.update()
|
|
19
|
+
if (!options) options = {}
|
|
20
|
+
return this.leafer ? this.leafer.selector.getByPoint(hitPoint, options.hitRadius || 0, { ...options, target: this }) : null
|
|
21
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,24 +1,8 @@
|
|
|
1
|
+
export { HitCanvasManager } from './HitCanvasManager'
|
|
2
|
+
|
|
1
3
|
export * from './LeafHit'
|
|
2
4
|
export * from './UIHit'
|
|
3
5
|
export * from './RectHit'
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import { UI, Group } from '@leafer-ui/draw'
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const ui = UI.prototype, group = Group.prototype
|
|
11
|
-
|
|
12
|
-
ui.find = function (condition: number | string | IFindUIMethod, options?: any): IUI[] {
|
|
13
|
-
return this.leafer ? this.leafer.selector.getBy(condition as IFindMethod, this, false, options) as IUI[] : []
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
ui.findOne = function (condition: number | string | IFindUIMethod, options?: any): IUI | undefined {
|
|
17
|
-
return this.leafer ? this.leafer.selector.getBy(condition as IFindMethod, this, true, options) as IUI : null
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
group.pick = function (hitPoint: IPointData, options?: IPickOptions): IPickResult {
|
|
21
|
-
this.__layout.update()
|
|
22
|
-
if (!options) options = {}
|
|
23
|
-
return this.leafer ? this.leafer.selector.getByPoint(hitPoint, options.hitRadius || 0, { ...options, target: this }) : null
|
|
24
|
-
}
|
|
7
|
+
export * from './find'
|
|
8
|
+
export * from './canvas'
|
package/types/index.d.ts
CHANGED
|
@@ -1,2 +1,17 @@
|
|
|
1
|
+
import { IHitCanvasManager, ILeafList, ILeaf, ILeaferCanvasConfig, IHitCanvas } from '@leafer/interface';
|
|
2
|
+
import { CanvasManager } from '@leafer/core';
|
|
1
3
|
|
|
2
|
-
|
|
4
|
+
declare class HitCanvasManager extends CanvasManager implements IHitCanvasManager {
|
|
5
|
+
maxTotal: number;
|
|
6
|
+
protected pathList: ILeafList;
|
|
7
|
+
protected pixelList: ILeafList;
|
|
8
|
+
getPixelType(leaf: ILeaf, config?: ILeaferCanvasConfig): IHitCanvas;
|
|
9
|
+
getPathType(leaf: ILeaf): IHitCanvas;
|
|
10
|
+
clearImageType(): void;
|
|
11
|
+
clearPathType(): void;
|
|
12
|
+
protected __clearLeafList(leafList: ILeafList): void;
|
|
13
|
+
protected __autoClear(): void;
|
|
14
|
+
clear(): void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { HitCanvasManager };
|