@leafer-ui/paint 1.0.0-rc.3 → 1.0.0-rc.5

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/paint",
3
- "version": "1.0.0-rc.3",
3
+ "version": "1.0.0-rc.5",
4
4
  "description": "@leafer-ui/paint",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -22,10 +22,10 @@
22
22
  "leaferjs"
23
23
  ],
24
24
  "dependencies": {
25
- "@leafer/core": "1.0.0-rc.3"
25
+ "@leafer/core": "1.0.0-rc.5"
26
26
  },
27
27
  "devDependencies": {
28
- "@leafer/interface": "1.0.0-rc.3",
29
- "@leafer-ui/interface": "1.0.0-rc.3"
28
+ "@leafer/interface": "1.0.0-rc.5",
29
+ "@leafer-ui/interface": "1.0.0-rc.5"
30
30
  }
31
31
  }
package/src/Compute.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { IUI, IPaint, ILeafPaint, IRGB, IBooleanMap } from '@leafer-ui/interface'
2
- import { ColorConvert } from '@leafer-ui/core'
1
+ import { IUI, IPaint, ILeafPaint, IRGB, IBooleanMap, IImagePaint } from '@leafer-ui/interface'
2
+ import { ColorConvert, ImageManager } from '@leafer-ui/core'
3
3
 
4
4
  import { image } from "./paint/image/image"
5
5
  import { linearGradient } from './paint/linear'
@@ -10,25 +10,41 @@ import { recycleImage } from './paint/image'
10
10
 
11
11
  let recycleMap: IBooleanMap
12
12
 
13
- export function compute(ui: IUI, attrName: string): void {
13
+ export function compute(attrName: 'fill' | 'stroke', ui: IUI): void {
14
14
  const value: ILeafPaint[] = []
15
+ const data = ui.__
16
+
15
17
  let item: ILeafPaint
16
- let paints = ui.__.__input[attrName] as IPaint[]
18
+ let paints = data.__input[attrName] as IPaint[]
17
19
 
18
20
  if (!(paints instanceof Array)) paints = [paints]
19
21
 
20
- recycleMap = recycleImage(ui.__, attrName)
22
+ recycleMap = recycleImage(attrName, data)
21
23
 
22
24
  for (let i = 0, len = paints.length; i < len; i++) {
23
- item = getLeafPaint(ui, paints[i], attrName)
25
+ item = getLeafPaint(attrName, paints[i], ui)
24
26
  if (item) value.push(item)
25
27
  }
26
28
 
27
- ui.__['_' + attrName] = value.length ? value : undefined
29
+ data['_' + attrName] = value.length ? value : undefined
30
+
31
+ // check png / svg / webp
32
+
33
+ let isPixel
34
+ if (paints.length === 1) {
35
+ const paint = paints[0] as IImagePaint
36
+ if (paint.type === 'image') isPixel = ImageManager.isPixel(paint)
37
+ }
38
+
39
+ if (attrName === 'fill') {
40
+ data.__pixelFill = isPixel
41
+ } else {
42
+ data.__pixelStroke = isPixel
43
+ }
28
44
  }
29
45
 
30
46
 
31
- function getLeafPaint(ui: IUI, paint: IPaint, attrName: string): ILeafPaint {
47
+ function getLeafPaint(attrName: string, paint: IPaint, ui: IUI,): ILeafPaint {
32
48
  if (typeof paint !== 'object' || paint.visible === false || paint.opacity === 0) return undefined
33
49
  const { boxBounds } = ui.__layout
34
50
 
package/src/Fill.ts CHANGED
@@ -6,13 +6,13 @@ import { checkImage } from './paint/image'
6
6
  import { fillText } from './FillText'
7
7
 
8
8
 
9
- export function fill(ui: IUI, canvas: ILeaferCanvas, fill: string): void {
9
+ export function fill(fill: string, ui: IUI, canvas: ILeaferCanvas): void {
10
10
  canvas.fillStyle = fill
11
11
  ui.__.__font ? fillText(ui, canvas) : (ui.__.windingRule ? canvas.fill(ui.__.windingRule) : canvas.fill())
12
12
  }
13
13
 
14
14
 
15
- export function fills(ui: IUI, canvas: ILeaferCanvas, fills: ILeafPaint[]): void {
15
+ export function fills(fills: ILeafPaint[], ui: IUI, canvas: ILeaferCanvas): void {
16
16
  let item: ILeafPaint
17
17
  const { windingRule, __font } = ui.__
18
18
  for (let i = 0, len = fills.length; i < len; i++) {
package/src/Stroke.ts CHANGED
@@ -1,18 +1,18 @@
1
- import { ILeaferCanvas } from '@leafer/interface'
1
+ import { ILeaferCanvas, IRenderOptions } from '@leafer/interface'
2
2
 
3
3
  import { IUI, ILeafPaint } from '@leafer-ui/interface'
4
4
 
5
5
  import { strokeText, drawStrokesStyle } from './StrokeText'
6
6
 
7
7
 
8
- export function stroke(ui: IUI, canvas: ILeaferCanvas, stroke: string): void {
8
+ export function stroke(stroke: string, ui: IUI, canvas: ILeaferCanvas, renderOptions: IRenderOptions): void {
9
9
  const options = ui.__
10
10
  const { strokeWidth, strokeAlign, __font } = options
11
11
  if (!strokeWidth) return
12
12
 
13
13
  if (__font) {
14
14
 
15
- strokeText(ui, canvas, stroke)
15
+ strokeText(stroke, ui, canvas, renderOptions)
16
16
 
17
17
  } else {
18
18
 
@@ -47,7 +47,12 @@ export function stroke(ui: IUI, canvas: ILeaferCanvas, stroke: string): void {
47
47
  options.windingRule ? out.clip(options.windingRule) : out.clip()
48
48
  out.clearWorld(ui.__layout.renderBounds)
49
49
 
50
- canvas.copyWorldToInner(out, ui.__world, ui.__layout.renderBounds)
50
+ if (ui.__hasMirror || renderOptions.matrix) {
51
+ canvas.copyWorldByReset(out)
52
+ } else {
53
+ canvas.copyWorldToInner(out, ui.__world, ui.__layout.renderBounds)
54
+ }
55
+
51
56
  out.recycle()
52
57
 
53
58
  break
@@ -57,14 +62,14 @@ export function stroke(ui: IUI, canvas: ILeaferCanvas, stroke: string): void {
57
62
  }
58
63
 
59
64
 
60
- export function strokes(ui: IUI, canvas: ILeaferCanvas, strokes: ILeafPaint[]): void {
65
+ export function strokes(strokes: ILeafPaint[], ui: IUI, canvas: ILeaferCanvas, renderOptions: IRenderOptions): void {
61
66
  const options = ui.__
62
67
  const { strokeWidth, strokeAlign, __font } = options
63
68
  if (!strokeWidth) return
64
69
 
65
70
  if (__font) {
66
71
 
67
- strokeText(ui, canvas, strokes)
72
+ strokeText(strokes, ui, canvas, renderOptions)
68
73
 
69
74
  } else {
70
75
 
@@ -72,7 +77,7 @@ export function strokes(ui: IUI, canvas: ILeaferCanvas, strokes: ILeafPaint[]):
72
77
 
73
78
  case 'center':
74
79
  canvas.setStroke(undefined, strokeWidth, options)
75
- drawStrokesStyle(ui, strokes, canvas)
80
+ drawStrokesStyle(strokes, false, ui, canvas)
76
81
  break
77
82
 
78
83
  case 'inside':
@@ -80,7 +85,7 @@ export function strokes(ui: IUI, canvas: ILeaferCanvas, strokes: ILeafPaint[]):
80
85
  canvas.setStroke(undefined, strokeWidth * 2, options)
81
86
  options.windingRule ? canvas.clip(options.windingRule) : canvas.clip()
82
87
 
83
- drawStrokesStyle(ui, strokes, canvas)
88
+ drawStrokesStyle(strokes, false, ui, canvas)
84
89
 
85
90
  canvas.restore()
86
91
  break
@@ -92,12 +97,17 @@ export function strokes(ui: IUI, canvas: ILeaferCanvas, strokes: ILeafPaint[]):
92
97
 
93
98
  out.setStroke(undefined, strokeWidth * 2, ui.__)
94
99
 
95
- drawStrokesStyle(ui, strokes, out)
100
+ drawStrokesStyle(strokes, false, ui, out)
96
101
 
97
102
  options.windingRule ? out.clip(options.windingRule) : out.clip()
98
103
  out.clearWorld(renderBounds)
99
104
 
100
- canvas.copyWorldToInner(out, ui.__world, renderBounds)
105
+ if (ui.__hasMirror || renderOptions.matrix) {
106
+ canvas.copyWorldByReset(out)
107
+ } else {
108
+ canvas.copyWorldToInner(out, ui.__world, renderBounds)
109
+ }
110
+
101
111
  out.recycle()
102
112
  break
103
113
  }
package/src/StrokeText.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ILeaferCanvas } from '@leafer/interface'
1
+ import { ILeaferCanvas, IRenderOptions } from '@leafer/interface'
2
2
 
3
3
  import { IUI, ITextRowData, ILeafPaint, IStrokeAlign, ILeafStrokePaint } from '@leafer-ui/interface'
4
4
 
@@ -6,37 +6,42 @@ import { fillText } from './FillText'
6
6
  import { checkImage } from './paint/image'
7
7
 
8
8
 
9
- export function strokeText(ui: IUI, canvas: ILeaferCanvas, stroke: string | ILeafPaint[]): void {
9
+ export function strokeText(stroke: string | ILeafPaint[], ui: IUI, canvas: ILeaferCanvas, renderOptions?: IRenderOptions): void {
10
10
  const { strokeAlign } = ui.__
11
11
  const isStrokes = typeof stroke !== 'string'
12
12
  switch (strokeAlign) {
13
13
  case 'center':
14
14
  canvas.setStroke(isStrokes ? undefined : stroke, ui.__.strokeWidth, ui.__)
15
- isStrokes ? drawStrokesStyle(ui, stroke as ILeafPaint[], canvas, true) : drawTextStroke(ui, canvas)
15
+ isStrokes ? drawStrokesStyle(stroke as ILeafPaint[], true, ui, canvas) : drawTextStroke(ui, canvas)
16
16
  break
17
17
  case 'inside':
18
- drawAlignStroke(ui, canvas, stroke, 'inside', isStrokes)
18
+ drawAlignStroke('inside', stroke, isStrokes, ui, canvas, renderOptions)
19
19
  break
20
20
  case 'outside':
21
- drawAlignStroke(ui, canvas, stroke, 'outside', isStrokes)
21
+ drawAlignStroke('outside', stroke, isStrokes, ui, canvas, renderOptions)
22
22
  break
23
23
  }
24
24
  }
25
25
 
26
- function drawAlignStroke(ui: IUI, canvas: ILeaferCanvas, stroke: string | ILeafPaint[], align: IStrokeAlign, isStrokes?: boolean): void {
26
+ function drawAlignStroke(align: IStrokeAlign, stroke: string | ILeafPaint[], isStrokes: boolean, ui: IUI, canvas: ILeaferCanvas, renderOptions: IRenderOptions): void {
27
27
  const { strokeWidth, __font } = ui.__
28
28
 
29
29
  const out = canvas.getSameCanvas(true)
30
30
  out.setStroke(isStrokes ? undefined : stroke, strokeWidth * 2, ui.__)
31
31
 
32
32
  out.font = __font
33
- isStrokes ? drawStrokesStyle(ui, stroke as ILeafPaint[], out, true) : drawTextStroke(ui, out)
33
+ isStrokes ? drawStrokesStyle(stroke as ILeafPaint[], true, ui, out) : drawTextStroke(ui, out)
34
34
 
35
35
  out.blendMode = align === 'outside' ? 'destination-out' : 'destination-in'
36
36
  fillText(ui, out)
37
37
  out.blendMode = 'normal'
38
38
 
39
- canvas.copyWorldToInner(out, ui.__world, ui.__layout.renderBounds)
39
+ if (ui.__hasMirror || renderOptions.matrix) {
40
+ canvas.copyWorldByReset(out)
41
+ } else {
42
+ canvas.copyWorldToInner(out, ui.__world, ui.__layout.renderBounds)
43
+ }
44
+
40
45
  out.recycle()
41
46
  }
42
47
 
@@ -61,7 +66,7 @@ export function drawTextStroke(ui: IUI, canvas: ILeaferCanvas): void {
61
66
 
62
67
  }
63
68
 
64
- export function drawStrokesStyle(ui: IUI, strokes: ILeafStrokePaint[], canvas: ILeaferCanvas, isText?: boolean): void {
69
+ export function drawStrokesStyle(strokes: ILeafStrokePaint[], isText: boolean, ui: IUI, canvas: ILeaferCanvas): void {
65
70
  let item: ILeafStrokePaint
66
71
  for (let i = 0, len = strokes.length; i < len; i++) {
67
72
  item = strokes[i]
@@ -39,9 +39,12 @@ export function checkImage(ui: IUI, canvas: ILeaferCanvas, paint: ILeafPaint, al
39
39
  if (!paint.style) {
40
40
  createPattern(ui, paint, canvas.pixelRatio)
41
41
  } else {
42
- ImageManager.patternTasker.add(async () => {
43
- if (canvas.bounds.hit(ui.__world) && createPattern(ui, paint, canvas.pixelRatio)) ui.forceUpdate('surface')
44
- }, 300)
42
+ if (!paint.patternTask) {
43
+ paint.patternTask = ImageManager.patternTasker.add(async () => {
44
+ paint.patternTask = null
45
+ if (canvas.bounds.hit(ui.__world) && createPattern(ui, paint, canvas.pixelRatio)) ui.forceUpdate('surface')
46
+ }, 300)
47
+ }
45
48
  }
46
49
  return false
47
50
  }
@@ -4,7 +4,7 @@ import { ImageManager } from '@leafer/core'
4
4
  import { IImagePaint, ILeafPaint, IUIData } from '@leafer-ui/interface'
5
5
 
6
6
 
7
- export function recycleImage(data: IUIData, attrName: string): IBooleanMap {
7
+ export function recycleImage(attrName: 'fill' | 'stroke', data: IUIData): IBooleanMap {
8
8
  const paints = (attrName === 'fill' ? data._fill : data._stroke) as ILeafPaint[]
9
9
 
10
10
  if (paints instanceof Array) {
package/types/index.d.ts CHANGED
@@ -1,21 +1,21 @@
1
1
  import { ILeaferCanvas, IRenderOptions, IBooleanMap } from '@leafer/interface';
2
2
  import { IUI, ILeafPaint, ICachedShape, IUIData } from '@leafer-ui/interface';
3
3
 
4
- declare function fill(ui: IUI, canvas: ILeaferCanvas, fill: string): void;
5
- declare function fills(ui: IUI, canvas: ILeaferCanvas, fills: ILeafPaint[]): void;
4
+ declare function fill(fill: string, ui: IUI, canvas: ILeaferCanvas): void;
5
+ declare function fills(fills: ILeafPaint[], ui: IUI, canvas: ILeaferCanvas): void;
6
6
 
7
7
  declare function fillText(ui: IUI, canvas: ILeaferCanvas): void;
8
8
 
9
- declare function stroke(ui: IUI, canvas: ILeaferCanvas, stroke: string): void;
10
- declare function strokes(ui: IUI, canvas: ILeaferCanvas, strokes: ILeafPaint[]): void;
9
+ declare function stroke(stroke: string, ui: IUI, canvas: ILeaferCanvas, renderOptions: IRenderOptions): void;
10
+ declare function strokes(strokes: ILeafPaint[], ui: IUI, canvas: ILeaferCanvas, renderOptions: IRenderOptions): void;
11
11
 
12
- declare function strokeText(ui: IUI, canvas: ILeaferCanvas, stroke: string | ILeafPaint[]): void;
12
+ declare function strokeText(stroke: string | ILeafPaint[], ui: IUI, canvas: ILeaferCanvas, renderOptions?: IRenderOptions): void;
13
13
  declare function drawTextStroke(ui: IUI, canvas: ILeaferCanvas): void;
14
14
 
15
15
  declare function shape(ui: IUI, current: ILeaferCanvas, options: IRenderOptions): ICachedShape;
16
16
 
17
- declare function compute(ui: IUI, attrName: string): void;
17
+ declare function compute(attrName: 'fill' | 'stroke', ui: IUI): void;
18
18
 
19
- declare function recycleImage(data: IUIData, attrName: string): IBooleanMap;
19
+ declare function recycleImage(attrName: 'fill' | 'stroke', data: IUIData): IBooleanMap;
20
20
 
21
21
  export { compute, drawTextStroke, fill, fillText, fills, recycleImage, shape, stroke, strokeText, strokes };