vue3-auto-scale 1.0.4 → 1.0.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/dist/index.esm.js +107 -64
- package/dist/index.js +133 -1
- package/dist/index.umd.js +136 -1
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
|
@@ -1,90 +1,133 @@
|
|
|
1
|
-
import { shallowRef
|
|
2
|
-
function
|
|
1
|
+
import { shallowRef, onMounted, onBeforeUnmount } from "vue";
|
|
2
|
+
function createAutoScale$1(options = {}) {
|
|
3
3
|
const {
|
|
4
|
-
designWidth
|
|
5
|
-
designHeight
|
|
6
|
-
mode
|
|
7
|
-
rootId
|
|
8
|
-
delay
|
|
9
|
-
debug
|
|
10
|
-
} =
|
|
11
|
-
let
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
4
|
+
designWidth = 1920,
|
|
5
|
+
designHeight = 1080,
|
|
6
|
+
mode = "contain",
|
|
7
|
+
rootId = "app",
|
|
8
|
+
delay = 200,
|
|
9
|
+
debug = false
|
|
10
|
+
} = options;
|
|
11
|
+
let resizeTimer = null;
|
|
12
|
+
let currentScale = 1;
|
|
13
|
+
const log = (...args) => {
|
|
14
|
+
if (debug) {
|
|
15
|
+
console.log("[vue3-auto-scale]", ...args);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
const update = () => {
|
|
19
|
+
const app = document.getElementById(rootId);
|
|
20
|
+
if (!app) {
|
|
21
|
+
log(`❌ 未找到 id 为 "${rootId}" 的容器`);
|
|
18
22
|
return;
|
|
19
23
|
}
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
24
|
+
const winWidth = window.innerWidth;
|
|
25
|
+
const winHeight = window.innerHeight;
|
|
26
|
+
const scaleX = winWidth / designWidth;
|
|
27
|
+
const scaleY = winHeight / designHeight;
|
|
28
|
+
const scale = mode === "contain" ? Math.min(scaleX, scaleY) : Math.max(scaleX, scaleY);
|
|
29
|
+
currentScale = scale;
|
|
30
|
+
app.style.transform = `scale(${scale})`;
|
|
31
|
+
app.style.transformOrigin = "top left";
|
|
32
|
+
const offsetX = (winWidth - designWidth * scale) / 2;
|
|
33
|
+
const offsetY = (winHeight - designHeight * scale) / 2;
|
|
34
|
+
app.style.marginLeft = `${offsetX}px`;
|
|
35
|
+
app.style.marginTop = `${offsetY}px`;
|
|
36
|
+
log(`✅ 缩放比例: ${scale.toFixed(4)}, 偏移: (${offsetX.toFixed(0)}, ${offsetY.toFixed(0)})`);
|
|
37
|
+
};
|
|
38
|
+
const handleResize = () => {
|
|
39
|
+
if (resizeTimer) {
|
|
40
|
+
clearTimeout(resizeTimer);
|
|
41
|
+
resizeTimer = null;
|
|
42
|
+
}
|
|
43
|
+
resizeTimer = setTimeout(update, delay);
|
|
44
|
+
};
|
|
45
|
+
const start = () => {
|
|
46
|
+
update();
|
|
47
|
+
window.addEventListener("resize", handleResize);
|
|
48
|
+
window.addEventListener("orientationchange", () => {
|
|
49
|
+
setTimeout(update, 300);
|
|
50
|
+
});
|
|
51
|
+
log("🚀 自适应缩放已启动");
|
|
52
|
+
};
|
|
53
|
+
const destroy = () => {
|
|
54
|
+
window.removeEventListener("resize", handleResize);
|
|
55
|
+
if (resizeTimer) {
|
|
56
|
+
clearTimeout(resizeTimer);
|
|
57
|
+
resizeTimer = null;
|
|
58
|
+
}
|
|
59
|
+
log("🛑 自适应缩放已销毁");
|
|
60
|
+
};
|
|
61
|
+
const getScale = () => currentScale;
|
|
62
|
+
start();
|
|
63
|
+
return {
|
|
64
|
+
update,
|
|
65
|
+
destroy,
|
|
66
|
+
getScale
|
|
37
67
|
};
|
|
38
68
|
}
|
|
39
|
-
function
|
|
40
|
-
const
|
|
41
|
-
|
|
69
|
+
function useAutoScale$1(options = {}) {
|
|
70
|
+
const instance = shallowRef(null);
|
|
71
|
+
const scale = shallowRef(1);
|
|
72
|
+
onMounted(() => {
|
|
42
73
|
requestAnimationFrame(() => {
|
|
43
|
-
|
|
44
|
-
...
|
|
45
|
-
debug:
|
|
74
|
+
instance.value = createAutoScale$1({
|
|
75
|
+
...options,
|
|
76
|
+
debug: options.debug ?? false
|
|
46
77
|
});
|
|
47
|
-
const
|
|
48
|
-
|
|
78
|
+
const pollScale = setInterval(() => {
|
|
79
|
+
if (instance.value) {
|
|
80
|
+
scale.value = instance.value.getScale();
|
|
81
|
+
}
|
|
49
82
|
}, 500);
|
|
50
|
-
|
|
51
|
-
clearInterval(
|
|
83
|
+
onBeforeUnmount(() => {
|
|
84
|
+
clearInterval(pollScale);
|
|
52
85
|
});
|
|
53
86
|
});
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
|
|
87
|
+
});
|
|
88
|
+
onBeforeUnmount(() => {
|
|
89
|
+
if (instance.value) {
|
|
90
|
+
instance.value.destroy();
|
|
91
|
+
instance.value = null;
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
return {
|
|
57
95
|
/** 当前缩放比例 */
|
|
58
|
-
scale
|
|
96
|
+
scale,
|
|
59
97
|
/** 手动更新缩放 */
|
|
60
98
|
update: () => {
|
|
61
|
-
var
|
|
62
|
-
return (
|
|
99
|
+
var _a;
|
|
100
|
+
return (_a = instance.value) == null ? void 0 : _a.update();
|
|
63
101
|
},
|
|
64
102
|
/** 获取缩放实例(高级用法) */
|
|
65
|
-
instance
|
|
103
|
+
instance
|
|
66
104
|
};
|
|
67
105
|
}
|
|
68
|
-
const
|
|
69
|
-
mounted(
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
106
|
+
const vAutoScale$1 = {
|
|
107
|
+
mounted(el, binding) {
|
|
108
|
+
const options = binding.value || {};
|
|
109
|
+
const instance = createAutoScale$1({
|
|
110
|
+
...options,
|
|
111
|
+
rootId: el.id || void 0
|
|
73
112
|
});
|
|
74
|
-
|
|
113
|
+
el.__autoScaleInstance = instance;
|
|
75
114
|
},
|
|
76
|
-
beforeUnmount(
|
|
77
|
-
const
|
|
78
|
-
|
|
115
|
+
beforeUnmount(el) {
|
|
116
|
+
const instance = el.__autoScaleInstance;
|
|
117
|
+
if (instance) {
|
|
118
|
+
instance.destroy();
|
|
119
|
+
delete el.__autoScaleInstance;
|
|
120
|
+
}
|
|
79
121
|
}
|
|
80
|
-
}
|
|
122
|
+
};
|
|
123
|
+
const index = {
|
|
81
124
|
useAutoScale,
|
|
82
125
|
vAutoScale,
|
|
83
126
|
createAutoScale
|
|
84
127
|
};
|
|
85
128
|
export {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
129
|
+
createAutoScale$1 as createAutoScale,
|
|
130
|
+
index as default,
|
|
131
|
+
useAutoScale$1 as useAutoScale,
|
|
132
|
+
vAutoScale$1 as vAutoScale
|
|
90
133
|
};
|
package/dist/index.js
CHANGED
|
@@ -1 +1,133 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const vue = require("vue");
|
|
4
|
+
function createAutoScale$1(options = {}) {
|
|
5
|
+
const {
|
|
6
|
+
designWidth = 1920,
|
|
7
|
+
designHeight = 1080,
|
|
8
|
+
mode = "contain",
|
|
9
|
+
rootId = "app",
|
|
10
|
+
delay = 200,
|
|
11
|
+
debug = false
|
|
12
|
+
} = options;
|
|
13
|
+
let resizeTimer = null;
|
|
14
|
+
let currentScale = 1;
|
|
15
|
+
const log = (...args) => {
|
|
16
|
+
if (debug) {
|
|
17
|
+
console.log("[vue3-auto-scale]", ...args);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const update = () => {
|
|
21
|
+
const app = document.getElementById(rootId);
|
|
22
|
+
if (!app) {
|
|
23
|
+
log(`❌ 未找到 id 为 "${rootId}" 的容器`);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const winWidth = window.innerWidth;
|
|
27
|
+
const winHeight = window.innerHeight;
|
|
28
|
+
const scaleX = winWidth / designWidth;
|
|
29
|
+
const scaleY = winHeight / designHeight;
|
|
30
|
+
const scale = mode === "contain" ? Math.min(scaleX, scaleY) : Math.max(scaleX, scaleY);
|
|
31
|
+
currentScale = scale;
|
|
32
|
+
app.style.transform = `scale(${scale})`;
|
|
33
|
+
app.style.transformOrigin = "top left";
|
|
34
|
+
const offsetX = (winWidth - designWidth * scale) / 2;
|
|
35
|
+
const offsetY = (winHeight - designHeight * scale) / 2;
|
|
36
|
+
app.style.marginLeft = `${offsetX}px`;
|
|
37
|
+
app.style.marginTop = `${offsetY}px`;
|
|
38
|
+
log(`✅ 缩放比例: ${scale.toFixed(4)}, 偏移: (${offsetX.toFixed(0)}, ${offsetY.toFixed(0)})`);
|
|
39
|
+
};
|
|
40
|
+
const handleResize = () => {
|
|
41
|
+
if (resizeTimer) {
|
|
42
|
+
clearTimeout(resizeTimer);
|
|
43
|
+
resizeTimer = null;
|
|
44
|
+
}
|
|
45
|
+
resizeTimer = setTimeout(update, delay);
|
|
46
|
+
};
|
|
47
|
+
const start = () => {
|
|
48
|
+
update();
|
|
49
|
+
window.addEventListener("resize", handleResize);
|
|
50
|
+
window.addEventListener("orientationchange", () => {
|
|
51
|
+
setTimeout(update, 300);
|
|
52
|
+
});
|
|
53
|
+
log("🚀 自适应缩放已启动");
|
|
54
|
+
};
|
|
55
|
+
const destroy = () => {
|
|
56
|
+
window.removeEventListener("resize", handleResize);
|
|
57
|
+
if (resizeTimer) {
|
|
58
|
+
clearTimeout(resizeTimer);
|
|
59
|
+
resizeTimer = null;
|
|
60
|
+
}
|
|
61
|
+
log("🛑 自适应缩放已销毁");
|
|
62
|
+
};
|
|
63
|
+
const getScale = () => currentScale;
|
|
64
|
+
start();
|
|
65
|
+
return {
|
|
66
|
+
update,
|
|
67
|
+
destroy,
|
|
68
|
+
getScale
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function useAutoScale$1(options = {}) {
|
|
72
|
+
const instance = vue.shallowRef(null);
|
|
73
|
+
const scale = vue.shallowRef(1);
|
|
74
|
+
vue.onMounted(() => {
|
|
75
|
+
requestAnimationFrame(() => {
|
|
76
|
+
instance.value = createAutoScale$1({
|
|
77
|
+
...options,
|
|
78
|
+
debug: options.debug ?? false
|
|
79
|
+
});
|
|
80
|
+
const pollScale = setInterval(() => {
|
|
81
|
+
if (instance.value) {
|
|
82
|
+
scale.value = instance.value.getScale();
|
|
83
|
+
}
|
|
84
|
+
}, 500);
|
|
85
|
+
vue.onBeforeUnmount(() => {
|
|
86
|
+
clearInterval(pollScale);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
vue.onBeforeUnmount(() => {
|
|
91
|
+
if (instance.value) {
|
|
92
|
+
instance.value.destroy();
|
|
93
|
+
instance.value = null;
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
return {
|
|
97
|
+
/** 当前缩放比例 */
|
|
98
|
+
scale,
|
|
99
|
+
/** 手动更新缩放 */
|
|
100
|
+
update: () => {
|
|
101
|
+
var _a;
|
|
102
|
+
return (_a = instance.value) == null ? void 0 : _a.update();
|
|
103
|
+
},
|
|
104
|
+
/** 获取缩放实例(高级用法) */
|
|
105
|
+
instance
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
const vAutoScale$1 = {
|
|
109
|
+
mounted(el, binding) {
|
|
110
|
+
const options = binding.value || {};
|
|
111
|
+
const instance = createAutoScale$1({
|
|
112
|
+
...options,
|
|
113
|
+
rootId: el.id || void 0
|
|
114
|
+
});
|
|
115
|
+
el.__autoScaleInstance = instance;
|
|
116
|
+
},
|
|
117
|
+
beforeUnmount(el) {
|
|
118
|
+
const instance = el.__autoScaleInstance;
|
|
119
|
+
if (instance) {
|
|
120
|
+
instance.destroy();
|
|
121
|
+
delete el.__autoScaleInstance;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
const index = {
|
|
126
|
+
useAutoScale,
|
|
127
|
+
vAutoScale,
|
|
128
|
+
createAutoScale
|
|
129
|
+
};
|
|
130
|
+
exports.createAutoScale = createAutoScale$1;
|
|
131
|
+
exports.default = index;
|
|
132
|
+
exports.useAutoScale = useAutoScale$1;
|
|
133
|
+
exports.vAutoScale = vAutoScale$1;
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1,136 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(global, factory) {
|
|
2
|
+
typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("vue")) : typeof define === "function" && define.amd ? define(["exports", "vue"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.Vue3AutoScale = {}, global.Vue));
|
|
3
|
+
})(this, function(exports2, vue) {
|
|
4
|
+
"use strict";
|
|
5
|
+
function createAutoScale$1(options = {}) {
|
|
6
|
+
const {
|
|
7
|
+
designWidth = 1920,
|
|
8
|
+
designHeight = 1080,
|
|
9
|
+
mode = "contain",
|
|
10
|
+
rootId = "app",
|
|
11
|
+
delay = 200,
|
|
12
|
+
debug = false
|
|
13
|
+
} = options;
|
|
14
|
+
let resizeTimer = null;
|
|
15
|
+
let currentScale = 1;
|
|
16
|
+
const log = (...args) => {
|
|
17
|
+
if (debug) {
|
|
18
|
+
console.log("[vue3-auto-scale]", ...args);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
const update = () => {
|
|
22
|
+
const app = document.getElementById(rootId);
|
|
23
|
+
if (!app) {
|
|
24
|
+
log(`❌ 未找到 id 为 "${rootId}" 的容器`);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const winWidth = window.innerWidth;
|
|
28
|
+
const winHeight = window.innerHeight;
|
|
29
|
+
const scaleX = winWidth / designWidth;
|
|
30
|
+
const scaleY = winHeight / designHeight;
|
|
31
|
+
const scale = mode === "contain" ? Math.min(scaleX, scaleY) : Math.max(scaleX, scaleY);
|
|
32
|
+
currentScale = scale;
|
|
33
|
+
app.style.transform = `scale(${scale})`;
|
|
34
|
+
app.style.transformOrigin = "top left";
|
|
35
|
+
const offsetX = (winWidth - designWidth * scale) / 2;
|
|
36
|
+
const offsetY = (winHeight - designHeight * scale) / 2;
|
|
37
|
+
app.style.marginLeft = `${offsetX}px`;
|
|
38
|
+
app.style.marginTop = `${offsetY}px`;
|
|
39
|
+
log(`✅ 缩放比例: ${scale.toFixed(4)}, 偏移: (${offsetX.toFixed(0)}, ${offsetY.toFixed(0)})`);
|
|
40
|
+
};
|
|
41
|
+
const handleResize = () => {
|
|
42
|
+
if (resizeTimer) {
|
|
43
|
+
clearTimeout(resizeTimer);
|
|
44
|
+
resizeTimer = null;
|
|
45
|
+
}
|
|
46
|
+
resizeTimer = setTimeout(update, delay);
|
|
47
|
+
};
|
|
48
|
+
const start = () => {
|
|
49
|
+
update();
|
|
50
|
+
window.addEventListener("resize", handleResize);
|
|
51
|
+
window.addEventListener("orientationchange", () => {
|
|
52
|
+
setTimeout(update, 300);
|
|
53
|
+
});
|
|
54
|
+
log("🚀 自适应缩放已启动");
|
|
55
|
+
};
|
|
56
|
+
const destroy = () => {
|
|
57
|
+
window.removeEventListener("resize", handleResize);
|
|
58
|
+
if (resizeTimer) {
|
|
59
|
+
clearTimeout(resizeTimer);
|
|
60
|
+
resizeTimer = null;
|
|
61
|
+
}
|
|
62
|
+
log("🛑 自适应缩放已销毁");
|
|
63
|
+
};
|
|
64
|
+
const getScale = () => currentScale;
|
|
65
|
+
start();
|
|
66
|
+
return {
|
|
67
|
+
update,
|
|
68
|
+
destroy,
|
|
69
|
+
getScale
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function useAutoScale$1(options = {}) {
|
|
73
|
+
const instance = vue.shallowRef(null);
|
|
74
|
+
const scale = vue.shallowRef(1);
|
|
75
|
+
vue.onMounted(() => {
|
|
76
|
+
requestAnimationFrame(() => {
|
|
77
|
+
instance.value = createAutoScale$1({
|
|
78
|
+
...options,
|
|
79
|
+
debug: options.debug ?? false
|
|
80
|
+
});
|
|
81
|
+
const pollScale = setInterval(() => {
|
|
82
|
+
if (instance.value) {
|
|
83
|
+
scale.value = instance.value.getScale();
|
|
84
|
+
}
|
|
85
|
+
}, 500);
|
|
86
|
+
vue.onBeforeUnmount(() => {
|
|
87
|
+
clearInterval(pollScale);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
vue.onBeforeUnmount(() => {
|
|
92
|
+
if (instance.value) {
|
|
93
|
+
instance.value.destroy();
|
|
94
|
+
instance.value = null;
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
return {
|
|
98
|
+
/** 当前缩放比例 */
|
|
99
|
+
scale,
|
|
100
|
+
/** 手动更新缩放 */
|
|
101
|
+
update: () => {
|
|
102
|
+
var _a;
|
|
103
|
+
return (_a = instance.value) == null ? void 0 : _a.update();
|
|
104
|
+
},
|
|
105
|
+
/** 获取缩放实例(高级用法) */
|
|
106
|
+
instance
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
const vAutoScale$1 = {
|
|
110
|
+
mounted(el, binding) {
|
|
111
|
+
const options = binding.value || {};
|
|
112
|
+
const instance = createAutoScale$1({
|
|
113
|
+
...options,
|
|
114
|
+
rootId: el.id || void 0
|
|
115
|
+
});
|
|
116
|
+
el.__autoScaleInstance = instance;
|
|
117
|
+
},
|
|
118
|
+
beforeUnmount(el) {
|
|
119
|
+
const instance = el.__autoScaleInstance;
|
|
120
|
+
if (instance) {
|
|
121
|
+
instance.destroy();
|
|
122
|
+
delete el.__autoScaleInstance;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
const index = {
|
|
127
|
+
useAutoScale,
|
|
128
|
+
vAutoScale,
|
|
129
|
+
createAutoScale
|
|
130
|
+
};
|
|
131
|
+
exports2.createAutoScale = createAutoScale$1;
|
|
132
|
+
exports2.default = index;
|
|
133
|
+
exports2.useAutoScale = useAutoScale$1;
|
|
134
|
+
exports2.vAutoScale = vAutoScale$1;
|
|
135
|
+
Object.defineProperties(exports2, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
136
|
+
});
|