@leafer-in/view 1.0.0-rc.22 → 1.0.0-rc.23

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/dist/view.cjs ADDED
@@ -0,0 +1,93 @@
1
+ 'use strict';
2
+
3
+ var draw = require('@leafer-ui/draw');
4
+
5
+ function getZoomScale(scaleX, type) {
6
+ let scale = 1;
7
+ const out = type === 'out', absScale = Math.abs(scaleX);
8
+ if (absScale > 1) {
9
+ while (out ? scale < absScale : scale <= absScale)
10
+ scale *= 2;
11
+ if (out)
12
+ scale /= 2;
13
+ }
14
+ else {
15
+ while (out ? scale >= absScale : scale > absScale)
16
+ scale /= 2;
17
+ if (!out)
18
+ scale *= 2;
19
+ }
20
+ return scale / scaleX;
21
+ }
22
+ function getFixBounds(bounds, scaleBounds) {
23
+ let { x, y, width, height } = bounds;
24
+ let fix;
25
+ if (!height)
26
+ height = width * (scaleBounds.height / scaleBounds.width), fix = true;
27
+ if (!width)
28
+ width = height * (scaleBounds.width / scaleBounds.height), fix = true;
29
+ return fix ? { x, y, width, height } : bounds;
30
+ }
31
+
32
+ draw.Leafer.prototype.zoom = function (zoomType, padding, fixed) {
33
+ const { zoomLayer } = this;
34
+ const limitBounds = this.canvas.bounds.clone().shrink(padding ? padding : 30), bounds = new draw.Bounds();
35
+ const center = { x: limitBounds.x + limitBounds.width / 2, y: limitBounds.y + limitBounds.height / 2 };
36
+ let changeScale;
37
+ const { scaleX } = this.__;
38
+ if (typeof zoomType === 'string') {
39
+ switch (zoomType) {
40
+ case 'in':
41
+ changeScale = getZoomScale(scaleX, 'in');
42
+ break;
43
+ case 'out':
44
+ changeScale = getZoomScale(scaleX, 'out');
45
+ break;
46
+ case 'fit':
47
+ zoomType = this.boxBounds;
48
+ break;
49
+ case 'fit-width':
50
+ zoomType = new draw.Bounds(this.boxBounds);
51
+ zoomType.height = 0;
52
+ break;
53
+ case 'fit-height':
54
+ zoomType = new draw.Bounds(this.boxBounds);
55
+ zoomType.width = 0;
56
+ break;
57
+ }
58
+ }
59
+ else if (typeof zoomType === 'number') {
60
+ changeScale = zoomType / scaleX;
61
+ }
62
+ if (changeScale) {
63
+ if (changeScale !== 1)
64
+ zoomLayer.scaleOfWorld(center, this.getValidScale(changeScale));
65
+ }
66
+ else if (typeof zoomType === 'object') {
67
+ const isArray = zoomType instanceof Array;
68
+ if (isArray || zoomType.tag) {
69
+ const list = isArray ? zoomType : [zoomType];
70
+ bounds.setListWithFn(list, draw.LeafBoundsHelper.worldBounds);
71
+ }
72
+ else {
73
+ const innerBounds = getFixBounds(zoomType, limitBounds);
74
+ bounds.set(zoomLayer.getWorldBounds(innerBounds));
75
+ }
76
+ const { x, y, width, height } = bounds;
77
+ let moveX = limitBounds.x - x, moveY = limitBounds.y - y;
78
+ if (fixed) {
79
+ moveX += Math.max((limitBounds.width - width) / 2, 0);
80
+ moveY += Math.max((limitBounds.height - height) / 2, 0);
81
+ }
82
+ else {
83
+ const fitScale = this.getValidScale(Math.min(limitBounds.width / width, limitBounds.height / height));
84
+ moveX += (limitBounds.width - width * fitScale) / 2;
85
+ moveY += (limitBounds.height - height * fitScale) / 2;
86
+ zoomLayer.scaleOfWorld(bounds, fitScale);
87
+ bounds.scaleOf(bounds, fitScale);
88
+ }
89
+ zoomLayer.move(moveX, moveY);
90
+ return bounds.move(moveX, moveY);
91
+ }
92
+ return zoomLayer.worldBoxBounds;
93
+ };
@@ -0,0 +1 @@
1
+ "use strict";var t=require("@leafer-ui/draw");function e(t,e){let i=1;const s="out"===e,o=Math.abs(t);if(o>1){for(;s?i<o:i<=o;)i*=2;s&&(i/=2)}else{for(;s?i>=o:i>o;)i/=2;s||(i*=2)}return i/t}t.Leafer.prototype.zoom=function(i,s,o){const{zoomLayer:h}=this,n=this.canvas.bounds.clone().shrink(s||30),a=new t.Bounds,r={x:n.x+n.width/2,y:n.y+n.height/2};let d;const{scaleX:c}=this.__;if("string"==typeof i)switch(i){case"in":d=e(c,"in");break;case"out":d=e(c,"out");break;case"fit":i=this.boxBounds;break;case"fit-width":(i=new t.Bounds(this.boxBounds)).height=0;break;case"fit-height":(i=new t.Bounds(this.boxBounds)).width=0}else"number"==typeof i&&(d=i/c);if(d)1!==d&&h.scaleOfWorld(r,this.getValidScale(d));else if("object"==typeof i){const e=i instanceof Array;if(e||i.tag){const s=e?i:[i];a.setListWithFn(s,t.LeafBoundsHelper.worldBounds)}else{const t=function(t,e){let i,{x:s,y:o,width:h,height:n}=t;return n||(n=h*(e.height/e.width),i=!0),h||(h=n*(e.width/e.height),i=!0),i?{x:s,y:o,width:h,height:n}:t}(i,n);a.set(h.getWorldBounds(t))}const{x:s,y:r,width:d,height:c}=a;let l=n.x-s,u=n.y-r;if(o)l+=Math.max((n.width-d)/2,0),u+=Math.max((n.height-c)/2,0);else{const t=this.getValidScale(Math.min(n.width/d,n.height/c));l+=(n.width-d*t)/2,u+=(n.height-c*t)/2,h.scaleOfWorld(a,t),a.scaleOf(a,t)}return h.move(l,u),a.move(l,u)}return h.worldBoxBounds};
package/package.json CHANGED
@@ -1,13 +1,19 @@
1
1
  {
2
2
  "name": "@leafer-in/view",
3
- "version": "1.0.0-rc.22",
3
+ "version": "1.0.0-rc.23",
4
4
  "description": "@leafer-in/view",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
7
+ "type": "module",
7
8
  "main": "dist/view.esm.js",
9
+ "exports": {
10
+ "import": "./dist/view.esm.js",
11
+ "require": "./dist/view.cjs",
12
+ "types": "./types/index.d.ts"
13
+ },
14
+ "types": "types/index.d.ts",
8
15
  "unpkg": "dist/view.js",
9
16
  "jsdelivr": "dist/view.js",
10
- "types": "types/index.d.ts",
11
17
  "files": [
12
18
  "src",
13
19
  "types",
@@ -28,8 +34,8 @@
28
34
  "leaferjs"
29
35
  ],
30
36
  "dependencies": {
31
- "@leafer-ui/draw": "1.0.0-rc.22",
32
- "@leafer-ui/interface": "1.0.0-rc.22",
33
- "@leafer-in/interface": "1.0.0-rc.22"
37
+ "@leafer-ui/draw": "1.0.0-rc.23",
38
+ "@leafer-ui/interface": "1.0.0-rc.23",
39
+ "@leafer-in/interface": "1.0.0-rc.23"
34
40
  }
35
41
  }