@leafer-ui/hit 1.0.0-rc.2 → 1.0.0-rc.20

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-ui/hit",
3
- "version": "1.0.0-rc.2",
3
+ "version": "1.0.0-rc.20",
4
4
  "description": "@leafer-ui/hit",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -21,8 +21,12 @@
21
21
  "leafer-ui",
22
22
  "leaferjs"
23
23
  ],
24
+ "dependencies": {
25
+ "@leafer/core": "1.0.0-rc.20",
26
+ "@leafer-ui/draw": "1.0.0-rc.20"
27
+ },
24
28
  "devDependencies": {
25
- "@leafer/interface": "1.0.0-rc.2",
26
- "@leafer-ui/interface": "1.0.0-rc.2"
29
+ "@leafer/interface": "1.0.0-rc.20",
30
+ "@leafer-ui/interface": "1.0.0-rc.20"
27
31
  }
28
32
  }
package/src/LeafHit.ts ADDED
@@ -0,0 +1,40 @@
1
+ import { IRadiusPointData, ILeaferCanvas } from '@leafer/interface'
2
+ import { Leaf, PointHelper, BoundsHelper } from '@leafer/core'
3
+
4
+
5
+ const { toInnerRadiusPointOf, copy, setRadius } = PointHelper
6
+ const inner = {} as IRadiusPointData
7
+
8
+ const leaf = Leaf.prototype
9
+
10
+ leaf.__hitWorld = function (point: IRadiusPointData): boolean {
11
+ if (this.__.hitRadius) {
12
+ copy(inner, point), point = inner
13
+ setRadius(point, this.__.hitRadius)
14
+ }
15
+
16
+ toInnerRadiusPointOf(point, this.__world, inner)
17
+
18
+ const { width, height } = this.__world
19
+ const isSmall = width < 10 && height < 10
20
+
21
+ if (this.__.hitBox || isSmall) {
22
+ if (BoundsHelper.hitRadiusPoint(this.__layout.boxBounds, inner)) return true
23
+ if (isSmall) return false
24
+ }
25
+
26
+ if (this.__layout.hitCanvasChanged || !this.__hitCanvas) {
27
+ this.__updateHitCanvas()
28
+ if (!this.__layout.boundsChanged) this.__layout.hitCanvasChanged = false
29
+ }
30
+
31
+ return this.__hit(inner)
32
+ }
33
+
34
+ leaf.__hitFill = function (inner: IRadiusPointData): boolean { return this.__hitCanvas?.hitFill(inner, this.__.windingRule) }
35
+
36
+ leaf.__hitStroke = function (inner: IRadiusPointData, strokeWidth: number): boolean { return this.__hitCanvas?.hitStroke(inner, strokeWidth) }
37
+
38
+ leaf.__hitPixel = function (inner: IRadiusPointData): boolean { return this.__hitCanvas?.hitPixel(inner, this.__layout.renderBounds, this.__hitCanvas.hitScale) }
39
+
40
+ leaf.__drawHitPath = function (canvas: ILeaferCanvas): void { if (canvas) this.__drawRenderPath(canvas) }
package/src/RectHit.ts ADDED
@@ -0,0 +1,16 @@
1
+ import { IRadiusPointData } from '@leafer/interface'
2
+ import { BoundsHelper } from '@leafer/core'
3
+ import { Rect, UI } from '@leafer-ui/draw'
4
+
5
+
6
+ const ui = new UI()
7
+
8
+ // hit
9
+
10
+ Rect.prototype.__updateHitCanvas = function () {
11
+ if (this.stroke || this.cornerRadius) ui.__updateHitCanvas.call(this)
12
+ }
13
+
14
+ Rect.prototype.__hitFill = function (inner: IRadiusPointData): boolean {
15
+ return this.__hitCanvas ? ui.__hitFill.call(this, inner) : BoundsHelper.hitRadiusPoint(this.__layout.boxBounds, inner)
16
+ }
package/src/UIHit.ts CHANGED
@@ -1,54 +1,85 @@
1
1
  import { IRadiusPointData } from '@leafer/interface'
2
+ import { Platform, Matrix, tempBounds } from '@leafer/core'
3
+ import { UI, ImageManager } from '@leafer-ui/draw'
2
4
 
3
- import { IUIHitModule } from '@leafer-ui/interface'
4
- import { Platform } from '@leafer/core'
5
5
 
6
+ const matrix = new Matrix()
7
+ const ui = UI.prototype
6
8
 
7
- export const UIHit: IUIHitModule = {
9
+ ui.__updateHitCanvas = function (): void {
10
+ const data = this.__, { hitCanvasManager } = this.leafer
8
11
 
9
- __updateHitCanvas(): void {
10
- if (!this.__hitCanvas) this.__hitCanvas = this.leafer.hitCanvasManager.getPathType(this)
11
- const h = this.__hitCanvas
12
- this.__drawHitPath(h)
13
- h.setStrokeOptions(this.__)
14
- },
12
+ const isHitPixelFill = data.__pixelFill && data.hitFill === 'pixel'
13
+ const isHitPixelStroke = data.__pixelStroke && data.hitStroke === 'pixel'
14
+ const isHitPixel = isHitPixelFill || isHitPixelStroke
15
15
 
16
- __hit(inner: IRadiusPointData): boolean {
17
- const { __hitCanvas: h } = this
18
- if (Platform.name === 'miniapp') this.__drawHitPath(h) // fix: 小程序需要实时更新
16
+ if (!this.__hitCanvas) this.__hitCanvas = isHitPixel ? hitCanvasManager.getPixelType(this, { contextSettings: { willReadFrequently: true } }) : hitCanvasManager.getPathType(this)
19
17
 
20
- const { fill, hitFill, windingRule } = this.__
21
- const needHitFill = (fill && hitFill === 'path') || hitFill === 'all'
22
- const isHitFill = h.hitFill(inner, windingRule)
23
- if (needHitFill && isHitFill) return true
18
+ const h = this.__hitCanvas
24
19
 
25
- const { stroke, hitStroke, strokeWidth, strokeAlign } = this.__
26
- const needHitStroke = (stroke && hitStroke === 'path') || hitStroke === 'all'
27
- const radiusWidth = inner.radiusX * 2
20
+ if (isHitPixel) {
21
+ const { renderBounds } = this.__layout
22
+ const size = Platform.image.hitCanvasSize
23
+ const scale = h.hitScale = tempBounds.set(0, 0, size, size).getFitMatrix(renderBounds, 0.5).a
24
+ const { x, y, width, height } = tempBounds.set(renderBounds).scale(scale)
25
+ h.resize({ width, height, pixelRatio: 1 })
26
+ h.clear()
28
27
 
29
- let hitWidth = radiusWidth
28
+ ImageManager.patternLocked = true
29
+ this.__renderShape(h, { matrix: matrix.setWith(this.__world).scaleWith(1 / scale).invertWith().translate(-x, -y) }, !isHitPixelFill, !isHitPixelStroke) // 矩阵
30
+ ImageManager.patternLocked = false
31
+ h.resetTransform()
30
32
 
31
- if (needHitStroke) {
32
- switch (strokeAlign) {
33
- case 'inside':
34
- hitWidth += strokeWidth * 2
35
- if (!needHitFill && (isHitFill && h.hitStroke(inner, hitWidth))) return true
33
+ data.__isHitPixel = true
34
+ } else {
35
+ data.__isHitPixel && (data.__isHitPixel = false)
36
+ }
37
+
38
+ this.__drawHitPath(h)
39
+ h.setStrokeOptions(data)
40
+
41
+ }
42
+
43
+ ui.__hit = function (inner: IRadiusPointData): boolean {
44
+ if (Platform.name === 'miniapp') this.__drawHitPath(this.__hitCanvas) // fix: 小程序需要实时更新
45
+
46
+ // hit pixel
47
+
48
+ const data = this.__
49
+ if (data.__isHitPixel && this.__hitPixel(inner)) return true
50
+
51
+ // hit path
52
+
53
+ const { hitFill } = data
54
+ const needHitFillPath = ((data.fill && hitFill == 'path') || hitFill === 'all')
55
+ if (needHitFillPath && this.__hitFill(inner)) return true
56
+
57
+ const { hitStroke, __strokeWidth } = data
58
+ const needHitStrokePath = ((data.stroke && hitStroke == 'path') || hitStroke === 'all')
59
+ if (!needHitFillPath && !needHitStrokePath) return false
60
+
61
+ const radiusWidth = inner.radiusX * 2
62
+ let hitWidth = radiusWidth
63
+
64
+ if (needHitStrokePath) {
65
+ switch (data.strokeAlign) {
66
+ case 'inside':
67
+ hitWidth += __strokeWidth * 2
68
+ if (!needHitFillPath && this.__hitFill(inner) && this.__hitStroke(inner, hitWidth)) return true
69
+ hitWidth = radiusWidth
70
+ break
71
+ case 'center':
72
+ hitWidth += __strokeWidth
73
+ break
74
+ case 'outside':
75
+ hitWidth += __strokeWidth * 2
76
+ if (!needHitFillPath) {
77
+ if (!this.__hitFill(inner) && this.__hitStroke(inner, hitWidth)) return true
36
78
  hitWidth = radiusWidth
37
- break
38
- case 'center':
39
- hitWidth += strokeWidth
40
- break
41
- case 'outside':
42
- hitWidth += strokeWidth * 2
43
- if (!needHitFill) {
44
- if (!isHitFill && h.hitStroke(inner, hitWidth)) return true
45
- hitWidth = radiusWidth
46
- }
47
- break
48
- }
79
+ }
80
+ break
49
81
  }
50
-
51
- return hitWidth ? h.hitStroke(inner, hitWidth) : false
52
82
  }
53
83
 
84
+ return hitWidth ? this.__hitStroke(inner, hitWidth) : false
54
85
  }
package/src/index.ts CHANGED
@@ -1 +1,22 @@
1
- export { UIHit } from "./UIHit"
1
+ export * from './LeafHit'
2
+ export * from './UIHit'
3
+ export * from './RectHit'
4
+
5
+ import { IFindMethod, IPointData, IPickOptions, IPickResult } from '@leafer/interface'
6
+ import { IFindUIMethod, IUI } from '@leafer-ui/interface'
7
+ import { UI, Group } from '@leafer-ui/draw'
8
+
9
+
10
+ UI.prototype.find = function (condition: number | string | IFindUIMethod, options?: any): IUI[] {
11
+ return this.leafer ? this.leafer.selector.getBy(condition as IFindMethod, this, false, options) as IUI[] : []
12
+ }
13
+
14
+ UI.prototype.findOne = function (condition: number | string | IFindUIMethod, options?: any): IUI {
15
+ return this.leafer ? this.leafer.selector.getBy(condition as IFindMethod, this, true, options) as IUI : null
16
+ }
17
+
18
+ Group.prototype.pick = function (hitPoint: IPointData, options?: IPickOptions): IPickResult {
19
+ this.__layout.update()
20
+ if (!options) options = {}
21
+ return this.leafer ? this.leafer.selector.getByPoint(hitPoint, options.hitRadius || 0, { ...options, target: this }) : null
22
+ }
package/types/index.d.ts CHANGED
@@ -1,5 +1,2 @@
1
- import { IUIHitModule } from '@leafer-ui/interface';
2
1
 
3
- declare const UIHit: IUIHitModule;
4
-
5
- export { UIHit };
2
+ export { }