@idraw/renderer 0.4.2 → 1.0.0-alpha.1

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.
Files changed (68) hide show
  1. package/dist/esm/calculator.d.ts +17 -12
  2. package/dist/esm/calculator.js +103 -88
  3. package/dist/esm/draw/base.d.ts +19 -0
  4. package/dist/esm/draw/base.js +141 -0
  5. package/dist/esm/draw/box.d.ts +2 -28
  6. package/dist/esm/draw/box.js +3 -335
  7. package/dist/esm/draw/circle.d.ts +2 -2
  8. package/dist/esm/draw/circle.js +4 -72
  9. package/dist/esm/draw/color.d.ts +3 -3
  10. package/dist/esm/draw/color.js +10 -8
  11. package/dist/esm/draw/ellipse.d.ts +2 -0
  12. package/dist/esm/draw/ellipse.js +6 -0
  13. package/dist/esm/draw/foreign-object.d.ts +2 -0
  14. package/dist/esm/draw/foreign-object.js +15 -0
  15. package/dist/esm/draw/global.d.ts +2 -2
  16. package/dist/esm/draw/global.js +2 -2
  17. package/dist/esm/draw/group.d.ts +3 -3
  18. package/dist/esm/draw/group.js +60 -94
  19. package/dist/esm/draw/image.d.ts +2 -2
  20. package/dist/esm/draw/image.js +70 -86
  21. package/dist/esm/draw/index.d.ts +6 -5
  22. package/dist/esm/draw/index.js +6 -5
  23. package/dist/esm/draw/layout.d.ts +2 -2
  24. package/dist/esm/draw/layout.js +21 -35
  25. package/dist/esm/draw/materials.d.ts +2 -0
  26. package/dist/esm/draw/materials.js +27 -0
  27. package/dist/esm/draw/path.d.ts +2 -2
  28. package/dist/esm/draw/path.js +4 -89
  29. package/dist/esm/draw/rect.d.ts +2 -2
  30. package/dist/esm/draw/rect.js +4 -22
  31. package/dist/esm/draw/svg-code.d.ts +2 -0
  32. package/dist/esm/draw/svg-code.js +15 -0
  33. package/dist/esm/draw/text.d.ts +2 -2
  34. package/dist/esm/draw/text.js +60 -50
  35. package/dist/esm/index.d.ts +1 -1
  36. package/dist/esm/index.js +15 -15
  37. package/dist/esm/loader.d.ts +4 -4
  38. package/dist/esm/loader.js +68 -70
  39. package/dist/esm/virtual/base.d.ts +2 -0
  40. package/dist/esm/virtual/base.js +52 -0
  41. package/dist/esm/virtual/circle.d.ts +2 -0
  42. package/dist/esm/virtual/circle.js +38 -0
  43. package/dist/esm/virtual/ellipse.d.ts +2 -0
  44. package/dist/esm/virtual/ellipse.js +40 -0
  45. package/dist/esm/virtual/index.d.ts +6 -0
  46. package/dist/esm/virtual/index.js +77 -0
  47. package/dist/esm/virtual/path.d.ts +2 -0
  48. package/dist/esm/virtual/path.js +10 -0
  49. package/dist/esm/virtual/rect.d.ts +2 -0
  50. package/dist/esm/virtual/rect.js +146 -0
  51. package/dist/esm/virtual/text.d.ts +2 -0
  52. package/dist/esm/virtual/text.js +165 -0
  53. package/dist/esm/visible/index.d.ts +22 -0
  54. package/dist/esm/{view-visible → visible}/index.js +29 -23
  55. package/dist/index.global.js +1794 -1578
  56. package/dist/index.global.min.js +1 -1
  57. package/package.json +3 -3
  58. package/dist/esm/draw/elements.d.ts +0 -2
  59. package/dist/esm/draw/elements.js +0 -27
  60. package/dist/esm/draw/html.d.ts +0 -2
  61. package/dist/esm/draw/html.js +0 -17
  62. package/dist/esm/draw/svg.d.ts +0 -2
  63. package/dist/esm/draw/svg.js +0 -17
  64. package/dist/esm/view-visible/index.d.ts +0 -22
  65. package/dist/esm/virtual-flat/index.d.ts +0 -7
  66. package/dist/esm/virtual-flat/index.js +0 -45
  67. package/dist/esm/virtual-flat/text.d.ts +0 -2
  68. package/dist/esm/virtual-flat/text.js +0 -151
@@ -0,0 +1,38 @@
1
+ import { calcVirtualBaseAttributes } from './base';
2
+ export function calcVirtualCircleAttributes(mtrl, opts) {
3
+ const { dpr } = opts;
4
+ const { width, height } = mtrl;
5
+ let { r } = mtrl;
6
+ const cx = (width / 2) * dpr;
7
+ const cy = (height / 2) * dpr;
8
+ r = r * dpr;
9
+ const c = r * 0.55228475;
10
+ const commands = [
11
+ {
12
+ type: 'M',
13
+ params: [cx + r, cy],
14
+ },
15
+ {
16
+ type: 'C',
17
+ params: [cx + r, cy - c, cx + c, cy - r, cx, cy - r],
18
+ },
19
+ {
20
+ type: 'C',
21
+ params: [cx - c, cy - r, cx - r, cy - c, cx - r, cy],
22
+ },
23
+ {
24
+ type: 'C',
25
+ params: [cx - r, cy + c, cx - c, cy + r, cx, cy + r],
26
+ },
27
+ {
28
+ type: 'C',
29
+ params: [cx + c, cy + r, cx + r, cy + c, cx + r, cy],
30
+ },
31
+ {
32
+ type: 'Z',
33
+ params: [],
34
+ },
35
+ ];
36
+ const attributes = Object.assign(Object.assign({}, calcVirtualBaseAttributes(mtrl, opts)), { commands });
37
+ return attributes;
38
+ }
@@ -0,0 +1,2 @@
1
+ import type { StrictMaterial, VirtualRectAttributes, CalcVirtualAttributesOptions } from '@idraw/types';
2
+ export declare function calcVirtualEllipseAttributes(mtrl: StrictMaterial<'ellipse'>, opts: CalcVirtualAttributesOptions): VirtualRectAttributes;
@@ -0,0 +1,40 @@
1
+ import { calcVirtualBaseAttributes } from './base';
2
+ export function calcVirtualEllipseAttributes(mtrl, opts) {
3
+ const { dpr } = opts;
4
+ const { width, height } = mtrl;
5
+ let { rx, ry } = mtrl;
6
+ const cx = (width / 2) * dpr;
7
+ const cy = (height / 2) * dpr;
8
+ rx = rx * dpr;
9
+ ry = ry * dpr;
10
+ const kx = 0.55228475 * rx;
11
+ const ky = 0.55228475 * ry;
12
+ const commands = [
13
+ {
14
+ type: 'M',
15
+ params: [cx + rx, cy],
16
+ },
17
+ {
18
+ type: 'C',
19
+ params: [cx + rx, cy - ky, cx + kx, cy - ry, cx, cy - ry],
20
+ },
21
+ {
22
+ type: 'C',
23
+ params: [cx - kx, cy - ry, cx - rx, cy - ky, cx - rx, cy],
24
+ },
25
+ {
26
+ type: 'C',
27
+ params: [cx - rx, cy + ky, cx - kx, cy + ry, cx, cy + ry],
28
+ },
29
+ {
30
+ type: 'C',
31
+ params: [cx + kx, cy + ry, cx + rx, cy + ky, cx + rx, cy],
32
+ },
33
+ {
34
+ type: 'Z',
35
+ params: [],
36
+ },
37
+ ];
38
+ const attributes = Object.assign(Object.assign({}, calcVirtualBaseAttributes(mtrl, opts)), { commands });
39
+ return attributes;
40
+ }
@@ -0,0 +1,6 @@
1
+ import { StrictMaterial, VirtualItemMap, VirtualAttributes, ViewContext2D, CalcVirtualAttributesOptions } from '@idraw/types';
2
+ export declare function calcVirtualAttributes(mtrl: StrictMaterial, opts: CalcVirtualAttributesOptions): VirtualAttributes | null;
3
+ export declare function materialsToVirtualFlatMap(materials: StrictMaterial[], opts: {
4
+ tempContext: ViewContext2D;
5
+ dpr: number;
6
+ }): VirtualItemMap;
@@ -0,0 +1,77 @@
1
+ import { is, getGroupQueueByMaterialPosition, calcMaterialBoundingInfo, boundingInfoToRangeBoundingInfo, } from '@idraw/util';
2
+ import { calcVirtualBaseAttributes } from './base';
3
+ import { calcVirtualRectAttributes } from './rect';
4
+ import { calcVirtualCircleAttributes } from './circle';
5
+ import { calcVirtualEllipseAttributes } from './ellipse';
6
+ import { calcVirtualTextAttributes } from './text';
7
+ import { calcVirtualPathAttributes } from './path';
8
+ export function calcVirtualAttributes(mtrl, opts) {
9
+ let attributes = null;
10
+ switch (mtrl.type) {
11
+ case 'rect': {
12
+ attributes = calcVirtualRectAttributes(mtrl, opts);
13
+ break;
14
+ }
15
+ case 'circle': {
16
+ attributes = calcVirtualCircleAttributes(mtrl, opts);
17
+ break;
18
+ }
19
+ case 'ellipse': {
20
+ attributes = calcVirtualEllipseAttributes(mtrl, opts);
21
+ break;
22
+ }
23
+ case 'text': {
24
+ attributes = calcVirtualTextAttributes(mtrl, opts);
25
+ break;
26
+ }
27
+ case 'group': {
28
+ attributes = calcVirtualRectAttributes(mtrl, opts);
29
+ break;
30
+ }
31
+ case 'path': {
32
+ attributes = calcVirtualPathAttributes(mtrl, opts);
33
+ break;
34
+ }
35
+ default: {
36
+ attributes = calcVirtualBaseAttributes(mtrl, opts);
37
+ break;
38
+ }
39
+ }
40
+ return attributes;
41
+ }
42
+ export function materialsToVirtualFlatMap(materials, opts) {
43
+ const virtualFlatMap = {};
44
+ const currentPosition = [];
45
+ const _walk = (mtrl) => {
46
+ const baseInfo = {
47
+ type: mtrl.type,
48
+ isVisibleInView: true,
49
+ position: [...currentPosition],
50
+ };
51
+ let boundingInfo = null;
52
+ const groupQueue = getGroupQueueByMaterialPosition(materials, currentPosition);
53
+ boundingInfo = calcMaterialBoundingInfo(mtrl, {
54
+ groupQueue: groupQueue || [],
55
+ });
56
+ const virtualItem = Object.assign(Object.assign(Object.assign({}, baseInfo), {
57
+ boundingInfo: boundingInfo,
58
+ rangeBoundingInfo: is.angle(mtrl.angle)
59
+ ? boundingInfoToRangeBoundingInfo(boundingInfo)
60
+ : boundingInfo,
61
+ }), calcVirtualAttributes(mtrl, Object.assign(Object.assign({}, opts), { groupQueue: groupQueue || [] })));
62
+ virtualFlatMap[mtrl.id] = virtualItem;
63
+ if (mtrl.type === 'group') {
64
+ mtrl.children.forEach((ele, i) => {
65
+ currentPosition.push(i);
66
+ _walk(ele);
67
+ currentPosition.pop();
68
+ });
69
+ }
70
+ };
71
+ materials.forEach((mtrl, index) => {
72
+ currentPosition.push(index);
73
+ _walk(mtrl);
74
+ currentPosition.pop();
75
+ });
76
+ return virtualFlatMap;
77
+ }
@@ -0,0 +1,2 @@
1
+ import { StrictMaterial, VirtualPathAttributes, CalcVirtualAttributesOptions } from '@idraw/types';
2
+ export declare function calcVirtualPathAttributes(mtrl: StrictMaterial<'path'>, opts: CalcVirtualAttributesOptions): VirtualPathAttributes;
@@ -0,0 +1,10 @@
1
+ import { convertPathCommandsToACLMZ, scalePathCommands } from '@idraw/util';
2
+ import { calcVirtualBaseAttributes } from './base';
3
+ export function calcVirtualPathAttributes(mtrl, opts) {
4
+ const { dpr } = opts;
5
+ const attributes = Object.assign(Object.assign({}, calcVirtualBaseAttributes(mtrl, opts)), { anchorCommands: [] });
6
+ const anchorCommands = convertPathCommandsToACLMZ(mtrl.commands || []);
7
+ attributes.anchorCommands = anchorCommands;
8
+ attributes.commands = scalePathCommands(mtrl.commands, dpr, dpr);
9
+ return attributes;
10
+ }
@@ -0,0 +1,2 @@
1
+ import type { StrictMaterial, VirtualRectAttributes, CalcVirtualAttributesOptions } from '@idraw/types';
2
+ export declare function calcVirtualRectAttributes(mtrl: StrictMaterial<'rect' | 'text'>, opts: CalcVirtualAttributesOptions): VirtualRectAttributes;
@@ -0,0 +1,146 @@
1
+ import { createId } from '@idraw/util';
2
+ import { calcVirtualBaseAttributes } from './base';
3
+ export function calcVirtualRectAttributes(mtrl, opts) {
4
+ const { dpr } = opts;
5
+ let { width, height, rx = 0, ry = 0, } = mtrl;
6
+ const { cornerRadius } = mtrl;
7
+ const x = 0;
8
+ const y = 0;
9
+ height = height * dpr;
10
+ width = width * dpr;
11
+ const commands = [];
12
+ let tlRX = 0;
13
+ let trRX = 0;
14
+ let brRX = 0;
15
+ let blRX = 0;
16
+ let tlRY = 0;
17
+ let trRY = 0;
18
+ let brRY = 0;
19
+ let blRY = 0;
20
+ if (typeof rx === 'number' && typeof ry === 'number') {
21
+ rx = rx * dpr;
22
+ ry = ry * dpr;
23
+ tlRX = rx;
24
+ trRX = rx;
25
+ brRX = rx;
26
+ blRX = rx;
27
+ tlRY = ry;
28
+ trRY = ry;
29
+ brRY = ry;
30
+ blRY = ry;
31
+ }
32
+ else if (Array.isArray(cornerRadius) && cornerRadius.length === 4) {
33
+ const crs = cornerRadius.map((r) => r * dpr);
34
+ tlRX = crs[0] || 0;
35
+ trRX = crs[1] || 0;
36
+ brRX = crs[2] || 0;
37
+ blRX = crs[3] || 0;
38
+ tlRY = crs[0] || 0;
39
+ trRY = crs[1] || 0;
40
+ brRY = crs[2] || 0;
41
+ blRY = crs[3] || 0;
42
+ }
43
+ else if (typeof cornerRadius === 'number') {
44
+ const cr = cornerRadius * dpr;
45
+ tlRX = cr;
46
+ trRX = cr;
47
+ brRX = cr;
48
+ blRX = cr;
49
+ tlRY = cr;
50
+ trRY = cr;
51
+ brRY = cr;
52
+ blRY = cr;
53
+ }
54
+ const x0 = x;
55
+ const y0 = y;
56
+ const x1 = x + width;
57
+ const y1 = y + height;
58
+ commands.push({
59
+ id: createId(),
60
+ type: 'M',
61
+ params: [x + tlRX, y],
62
+ });
63
+ if (trRX > 0 || trRY > 0) {
64
+ commands.push({
65
+ id: createId(),
66
+ type: 'L',
67
+ params: [x1 - trRX, y0],
68
+ });
69
+ commands.push({
70
+ id: createId(),
71
+ type: 'A',
72
+ params: [trRX, trRY, 0, 0, 1, x1, y0 + trRY],
73
+ });
74
+ }
75
+ else {
76
+ commands.push({
77
+ id: createId(),
78
+ type: 'L',
79
+ params: [x1, y0],
80
+ });
81
+ }
82
+ if (brRX > 0 || brRY > 0) {
83
+ commands.push({
84
+ id: createId(),
85
+ type: 'L',
86
+ params: [x1, y1 - brRY],
87
+ });
88
+ commands.push({
89
+ id: createId(),
90
+ type: 'A',
91
+ params: [brRX, brRY, 0, 0, 1, x1 - brRX, y1],
92
+ });
93
+ }
94
+ else {
95
+ commands.push({
96
+ id: createId(),
97
+ type: 'L',
98
+ params: [x1, y1],
99
+ });
100
+ }
101
+ if (blRX > 0 || blRY > 0) {
102
+ commands.push({
103
+ id: createId(),
104
+ type: 'L',
105
+ params: [x0 + blRX, y1],
106
+ });
107
+ commands.push({
108
+ id: createId(),
109
+ type: 'A',
110
+ params: [blRX, blRY, 0, 0, 1, x0, y1 - blRY],
111
+ });
112
+ }
113
+ else {
114
+ commands.push({
115
+ id: createId(),
116
+ type: 'L',
117
+ params: [x0, y1],
118
+ });
119
+ }
120
+ if (tlRX > 0 || tlRY > 0) {
121
+ commands.push({
122
+ id: createId(),
123
+ type: 'L',
124
+ params: [x0, y0 + tlRY],
125
+ });
126
+ commands.push({
127
+ id: createId(),
128
+ type: 'A',
129
+ params: [tlRX, tlRY, 0, 0, 1, x0 + tlRX, y0],
130
+ });
131
+ }
132
+ else {
133
+ commands.push({
134
+ id: createId(),
135
+ type: 'L',
136
+ params: [x0, y0],
137
+ });
138
+ }
139
+ commands.push({
140
+ id: createId(),
141
+ type: 'Z',
142
+ params: [],
143
+ });
144
+ const attributes = Object.assign(Object.assign({}, calcVirtualBaseAttributes(mtrl, opts)), { commands });
145
+ return attributes;
146
+ }
@@ -0,0 +1,2 @@
1
+ import type { StrictMaterial, CalcVirtualAttributesOptions, VirtualTextAttributes } from '@idraw/types';
2
+ export declare function calcVirtualTextAttributes(mtrl: StrictMaterial<'text'>, opts: CalcVirtualAttributesOptions): VirtualTextAttributes;
@@ -0,0 +1,165 @@
1
+ import { enhanceFontFamliy, getDefaultMaterialAttributes } from '@idraw/util';
2
+ import { calcVirtualRectAttributes } from './rect';
3
+ const attributesConfig = getDefaultMaterialAttributes();
4
+ function isTextWidthWithinErrorRange(w0, w1, scale) {
5
+ if (scale < 0.5) {
6
+ if (w0 < w1 && (w0 - w1) / w0 > -0.15) {
7
+ return true;
8
+ }
9
+ }
10
+ return w0 >= w1;
11
+ }
12
+ export function calcVirtualTextAttributes(mtrl, opts) {
13
+ const { width, height } = mtrl;
14
+ const x = 0;
15
+ const y = 0;
16
+ const ctx = opts.tempContext;
17
+ const lines = [];
18
+ const attributes = Object.assign(Object.assign({}, attributesConfig), mtrl);
19
+ const originFontSize = attributes.fontSize || attributesConfig.fontSize;
20
+ const fontSize = originFontSize;
21
+ const baseAttrs = calcVirtualRectAttributes(mtrl, opts);
22
+ if (fontSize < 2) {
23
+ return Object.assign(Object.assign({}, baseAttrs), { textLines: [] });
24
+ }
25
+ const originLineHeight = attributes.lineHeight || originFontSize;
26
+ const lineHeight = originLineHeight;
27
+ ctx.textBaseline = 'top';
28
+ ctx.$setFont({
29
+ fontWeight: attributes.fontWeight,
30
+ fontSize: fontSize,
31
+ fontFamily: enhanceFontFamliy(attributes.fontFamily),
32
+ });
33
+ let attributesText = attributes.text.replace(/\r\n/gi, '\n');
34
+ if (attributes.textTransform === 'lowercase') {
35
+ attributesText = attributesText.toLowerCase();
36
+ }
37
+ else if (attributes.textTransform === 'uppercase') {
38
+ attributesText = attributesText.toUpperCase();
39
+ }
40
+ const fontHeight = lineHeight;
41
+ const attributesTextList = attributesText.split('\n');
42
+ let lineNum = 0;
43
+ attributesTextList.forEach((itemText, idx) => {
44
+ if (attributes.minInlineSize === 'maxContent') {
45
+ const measureResult = ctx.measureText(itemText);
46
+ lines.push({
47
+ x,
48
+ y: 0,
49
+ text: itemText,
50
+ width: ctx.$undoPixelRatio(measureResult.width),
51
+ });
52
+ }
53
+ else {
54
+ let lineText = '';
55
+ let splitStr = '';
56
+ let tempCharList = itemText.split(splitStr);
57
+ if (attributes.wordBreak === 'normal') {
58
+ splitStr = ' ';
59
+ const wordList = itemText.split(splitStr);
60
+ tempCharList = [];
61
+ wordList.forEach((word, idx) => {
62
+ tempCharList.push(word);
63
+ if (idx < wordList.length - 1) {
64
+ tempCharList.push(splitStr);
65
+ }
66
+ });
67
+ }
68
+ if (tempCharList.length === 1 && attributes.overflow !== 'hidden') {
69
+ lines.push({
70
+ x,
71
+ y: 0,
72
+ text: tempCharList[0],
73
+ width: ctx.$undoPixelRatio(ctx.measureText(tempCharList[0]).width),
74
+ });
75
+ }
76
+ else if (tempCharList.length > 0) {
77
+ for (let i = 0; i < tempCharList.length; i++) {
78
+ if (isTextWidthWithinErrorRange(ctx.$doPixelRatio(width), ctx.measureText(lineText + tempCharList[i]).width, 1)) {
79
+ lineText += tempCharList[i] || '';
80
+ }
81
+ else {
82
+ lines.push({
83
+ x,
84
+ y: 0,
85
+ text: lineText,
86
+ width: ctx.$undoPixelRatio(ctx.measureText(lineText).width),
87
+ });
88
+ lineText = tempCharList[i] || '';
89
+ lineNum++;
90
+ }
91
+ if (lineNum * fontHeight >= height) {
92
+ if (attributes.overflow === 'hidden') {
93
+ lineText = '';
94
+ break;
95
+ }
96
+ }
97
+ if (tempCharList.length - 1 === i) {
98
+ if ((lineNum + 1) * fontHeight <= height) {
99
+ lines.push({
100
+ x,
101
+ y: 0,
102
+ text: lineText,
103
+ width: ctx.$undoPixelRatio(ctx.measureText(lineText).width),
104
+ });
105
+ lineText = '';
106
+ if (idx < attributesTextList.length - 1) {
107
+ lineNum++;
108
+ }
109
+ break;
110
+ }
111
+ }
112
+ }
113
+ if (lineText) {
114
+ lines.push({
115
+ x,
116
+ y: 0,
117
+ text: lineText,
118
+ width: ctx.$undoPixelRatio(ctx.measureText(lineText).width),
119
+ });
120
+ lineText = '';
121
+ }
122
+ }
123
+ else {
124
+ lines.push({
125
+ x,
126
+ y: 0,
127
+ text: '',
128
+ width: 0,
129
+ });
130
+ }
131
+ }
132
+ });
133
+ let startY = 0;
134
+ let eachLineStartY = 0;
135
+ if (fontHeight > fontSize) {
136
+ eachLineStartY = (fontHeight - fontSize) / 2;
137
+ }
138
+ if (lines.length * fontHeight < height) {
139
+ if (attributes.verticalAlign === 'top') {
140
+ startY = 0;
141
+ }
142
+ else if (attributes.verticalAlign === 'bottom') {
143
+ startY += height - lines.length * fontHeight;
144
+ }
145
+ else {
146
+ startY += (height - lines.length * fontHeight) / 2;
147
+ }
148
+ }
149
+ {
150
+ const _y = y + startY;
151
+ lines.forEach((line, i) => {
152
+ let _x = x;
153
+ if (attributes.textAlign === 'center') {
154
+ _x = x + (width - line.width) / 2;
155
+ }
156
+ else if (attributes.textAlign === 'right') {
157
+ _x = x + (width - line.width);
158
+ }
159
+ lines[i].x = _x;
160
+ lines[i].y = _y + fontHeight * i + eachLineStartY;
161
+ });
162
+ }
163
+ const virtualTextAttributes = Object.assign(Object.assign({}, baseAttrs), { textLines: lines });
164
+ return virtualTextAttributes;
165
+ }
@@ -0,0 +1,22 @@
1
+ import { StrictMaterial, ViewScaleInfo, ViewSizeInfo, BoundingInfo, VirtualItemMap, ViewContext2D } from '@idraw/types';
2
+ export declare function sortMaterialsViewVisiableInfoMap(materials: StrictMaterial[], opts: {
3
+ viewScaleInfo: ViewScaleInfo;
4
+ viewSizeInfo: ViewSizeInfo;
5
+ tempContext: ViewContext2D;
6
+ }): {
7
+ virtualItemMap: VirtualItemMap;
8
+ visibleCount: number;
9
+ invisibleCount: number;
10
+ };
11
+ export declare function updateVirtualItemMapStatus(virtualItemMap: VirtualItemMap, opts: {
12
+ viewScaleInfo: ViewScaleInfo;
13
+ viewSizeInfo: ViewSizeInfo;
14
+ }): {
15
+ virtualItemMap: VirtualItemMap;
16
+ visibleCount: number;
17
+ invisibleCount: number;
18
+ };
19
+ export declare function calcVisibleOriginCanvasBoundingBox(opts: {
20
+ viewScaleInfo: ViewScaleInfo;
21
+ viewSizeInfo: ViewSizeInfo;
22
+ }): BoundingInfo;
@@ -1,37 +1,43 @@
1
- import { calcElementCenter } from '@idraw/util';
2
- import { elementsToVirtualFlatMap } from '../virtual-flat';
3
- export function sortElementsViewVisiableInfoMap(elements, opts) {
1
+ import { calcMaterialCenter } from '@idraw/util';
2
+ import { materialsToVirtualFlatMap } from '../virtual';
3
+ export function sortMaterialsViewVisiableInfoMap(materials, opts) {
4
4
  const { viewScaleInfo, viewSizeInfo, tempContext } = opts;
5
- const visibleInfoMap = elementsToVirtualFlatMap(elements, { tempContext });
6
- return updateVirtualFlatItemMapStatus(visibleInfoMap, { viewScaleInfo, viewSizeInfo });
5
+ const visibleInfoMap = materialsToVirtualFlatMap(materials, {
6
+ tempContext,
7
+ dpr: viewSizeInfo.devicePixelRatio,
8
+ });
9
+ return updateVirtualItemMapStatus(visibleInfoMap, { viewScaleInfo, viewSizeInfo });
7
10
  }
8
- function isRangeRectInfoCollide(info1, info2) {
11
+ function isRangeBoundingBoxCollide(info1, info2) {
12
+ const centerX = info1.center.x;
13
+ const centerY = info1.center.y;
9
14
  const rect1MinX = Math.min(info1.topLeft.x, info1.topRight.x, info1.bottomLeft.x, info1.bottomRight.x);
10
15
  const rect1MaxX = Math.max(info1.topLeft.x, info1.topRight.x, info1.bottomLeft.x, info1.bottomRight.x);
11
16
  const rect1MinY = Math.min(info1.topLeft.y, info1.topRight.y, info1.bottomLeft.y, info1.bottomRight.y);
12
17
  const rect1MaxY = Math.max(info1.topLeft.y, info1.topRight.y, info1.bottomLeft.y, info1.bottomRight.y);
13
- const rect2MinX = Math.min(info2.topLeft.x, info2.topRight.x, info2.bottomLeft.x, info2.bottomRight.x);
14
- const rect2MaxX = Math.max(info2.topLeft.x, info2.topRight.x, info2.bottomLeft.x, info2.bottomRight.x);
15
- const rect2MinY = Math.min(info2.topLeft.y, info2.topRight.y, info2.bottomLeft.y, info2.bottomRight.y);
16
- const rect2MaxY = Math.max(info2.topLeft.y, info2.topRight.y, info2.bottomLeft.y, info2.bottomRight.y);
17
- if ((rect1MinX <= rect2MaxX && rect1MaxX >= rect2MinX && rect1MinY <= rect2MaxY && rect1MaxY >= rect2MinY) ||
18
- (rect2MaxX <= rect1MaxY && rect2MaxX >= rect1MaxY && rect2MaxX <= rect1MaxY && rect2MaxX >= rect1MaxY)) {
18
+ const w = Math.abs(rect1MaxX - rect1MinX);
19
+ const h = Math.abs(rect1MaxY - rect1MinY);
20
+ const rect2MinX = Math.min(info2.topLeft.x, info2.topRight.x, info2.bottomLeft.x, info2.bottomRight.x) - w;
21
+ const rect2MaxX = Math.max(info2.topLeft.x, info2.topRight.x, info2.bottomLeft.x, info2.bottomRight.x) + w;
22
+ const rect2MinY = Math.min(info2.topLeft.y, info2.topRight.y, info2.bottomLeft.y, info2.bottomRight.y) - h;
23
+ const rect2MaxY = Math.max(info2.topLeft.y, info2.topRight.y, info2.bottomLeft.y, info2.bottomRight.y) + h;
24
+ if (centerX >= rect2MinX && centerX <= rect2MaxX && centerY >= rect2MinY && centerY <= rect2MaxY) {
19
25
  return true;
20
26
  }
21
27
  return false;
22
28
  }
23
- export function updateVirtualFlatItemMapStatus(virtualFlatItemMap, opts) {
24
- const canvasRectInfo = calcVisibleOriginCanvasRectInfo(opts);
29
+ export function updateVirtualItemMapStatus(virtualItemMap, opts) {
30
+ const canvasBoundingBox = calcVisibleOriginCanvasBoundingBox(opts);
25
31
  let visibleCount = 0;
26
32
  let invisibleCount = 0;
27
- Object.keys(virtualFlatItemMap).forEach((uuid) => {
28
- const info = virtualFlatItemMap[uuid];
29
- info.isVisibleInView = isRangeRectInfoCollide(info.rangeRectInfo, canvasRectInfo);
33
+ Object.keys(virtualItemMap).forEach((id) => {
34
+ const info = virtualItemMap[id];
35
+ info.isVisibleInView = isRangeBoundingBoxCollide(info.rangeBoundingInfo, canvasBoundingBox);
30
36
  info.isVisibleInView ? visibleCount++ : invisibleCount++;
31
37
  });
32
- return { virtualFlatItemMap, visibleCount, invisibleCount };
38
+ return { virtualItemMap, visibleCount, invisibleCount };
33
39
  }
34
- export function calcVisibleOriginCanvasRectInfo(opts) {
40
+ export function calcVisibleOriginCanvasBoundingBox(opts) {
35
41
  const { viewScaleInfo, viewSizeInfo } = opts;
36
42
  const { scale, offsetTop, offsetLeft } = viewScaleInfo;
37
43
  const { width, height } = viewSizeInfo;
@@ -39,7 +45,7 @@ export function calcVisibleOriginCanvasRectInfo(opts) {
39
45
  const y = 0 - offsetTop / scale;
40
46
  const w = width / scale;
41
47
  const h = height / scale;
42
- const center = calcElementCenter({ x, y, w, h });
48
+ const center = calcMaterialCenter({ x, y, width, height });
43
49
  const topLeft = { x, y };
44
50
  const topRight = { x: x + w, y };
45
51
  const bottomLeft = { x, y: y + h };
@@ -48,7 +54,7 @@ export function calcVisibleOriginCanvasRectInfo(opts) {
48
54
  const top = { x: center.x, y };
49
55
  const right = { x: x + w, y: center.y };
50
56
  const bottom = { x: center.x, y: y + h };
51
- const rectInfo = {
57
+ const boundingBox = {
52
58
  center,
53
59
  topLeft,
54
60
  topRight,
@@ -57,7 +63,7 @@ export function calcVisibleOriginCanvasRectInfo(opts) {
57
63
  left,
58
64
  top,
59
65
  right,
60
- bottom
66
+ bottom,
61
67
  };
62
- return rectInfo;
68
+ return boundingBox;
63
69
  }