vue3-auto-scale 1.0.2 → 1.0.3
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 +90 -0
- package/dist/index.js +1 -0
- package/dist/index.umd.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { shallowRef as w, onMounted as _, onBeforeUnmount as h } from "vue";
|
|
2
|
+
function A(t = {}) {
|
|
3
|
+
const {
|
|
4
|
+
designWidth: e = 1920,
|
|
5
|
+
designHeight: o = 1080,
|
|
6
|
+
mode: n = "contain",
|
|
7
|
+
rootId: u = "app",
|
|
8
|
+
delay: $ = 200,
|
|
9
|
+
debug: y = !1
|
|
10
|
+
} = t;
|
|
11
|
+
let a = null, r = 1;
|
|
12
|
+
const l = (...s) => {
|
|
13
|
+
y && console.log("[vue3-auto-scale]", ...s);
|
|
14
|
+
}, i = () => {
|
|
15
|
+
const s = document.getElementById(u);
|
|
16
|
+
if (!s) {
|
|
17
|
+
l(`❌ 未找到 id 为 "${u}" 的容器`);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const f = window.innerWidth, m = window.innerHeight, v = f / e, g = m / o, c = n === "contain" ? Math.min(v, g) : Math.max(v, g);
|
|
21
|
+
r = c, s.style.transform = `scale(${c})`, s.style.transformOrigin = "top left";
|
|
22
|
+
const S = (f - e * c) / 2, p = (m - o * c) / 2;
|
|
23
|
+
s.style.marginLeft = `${S}px`, s.style.marginTop = `${p}px`, l(`✅ 缩放比例: ${c.toFixed(4)}, 偏移: (${S.toFixed(0)}, ${p.toFixed(0)})`);
|
|
24
|
+
}, d = () => {
|
|
25
|
+
a && (clearTimeout(a), a = null), a = setTimeout(i, $);
|
|
26
|
+
}, x = () => {
|
|
27
|
+
i(), window.addEventListener("resize", d), window.addEventListener("orientationchange", () => {
|
|
28
|
+
setTimeout(i, 300);
|
|
29
|
+
}), l("🚀 自适应缩放已启动");
|
|
30
|
+
}, I = () => {
|
|
31
|
+
window.removeEventListener("resize", d), a && (clearTimeout(a), a = null), l("🛑 自适应缩放已销毁");
|
|
32
|
+
}, T = () => r;
|
|
33
|
+
return x(), {
|
|
34
|
+
update: i,
|
|
35
|
+
destroy: I,
|
|
36
|
+
getScale: T
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function z(t = {}) {
|
|
40
|
+
const e = w(null), o = w(1);
|
|
41
|
+
return _(() => {
|
|
42
|
+
requestAnimationFrame(() => {
|
|
43
|
+
e.value = A({
|
|
44
|
+
...t,
|
|
45
|
+
debug: t.debug ?? !1
|
|
46
|
+
});
|
|
47
|
+
const n = setInterval(() => {
|
|
48
|
+
e.value && (o.value = e.value.getScale());
|
|
49
|
+
}, 500);
|
|
50
|
+
h(() => {
|
|
51
|
+
clearInterval(n);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
}), h(() => {
|
|
55
|
+
e.value && (e.value.destroy(), e.value = null);
|
|
56
|
+
}), {
|
|
57
|
+
/** 当前缩放比例 */
|
|
58
|
+
scale: o,
|
|
59
|
+
/** 手动更新缩放 */
|
|
60
|
+
update: () => {
|
|
61
|
+
var n;
|
|
62
|
+
return (n = e.value) == null ? void 0 : n.update();
|
|
63
|
+
},
|
|
64
|
+
/** 获取缩放实例(高级用法) */
|
|
65
|
+
instance: e
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
const E = {
|
|
69
|
+
mounted(t, e) {
|
|
70
|
+
const o = e.value || {}, n = A({
|
|
71
|
+
...o,
|
|
72
|
+
rootId: t.id || void 0
|
|
73
|
+
});
|
|
74
|
+
t.__autoScaleInstance = n;
|
|
75
|
+
},
|
|
76
|
+
beforeUnmount(t) {
|
|
77
|
+
const e = t.__autoScaleInstance;
|
|
78
|
+
e && (e.destroy(), delete t.__autoScaleInstance);
|
|
79
|
+
}
|
|
80
|
+
}, F = {
|
|
81
|
+
useAutoScale,
|
|
82
|
+
vAutoScale,
|
|
83
|
+
createAutoScale
|
|
84
|
+
};
|
|
85
|
+
export {
|
|
86
|
+
A as createAutoScale,
|
|
87
|
+
F as default,
|
|
88
|
+
z as useAutoScale,
|
|
89
|
+
E as vAutoScale
|
|
90
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const c=require("vue");function r(t={}){const{designWidth:e=1920,designHeight:o=1080,mode:n="contain",rootId:d="app",delay:y=200,debug:A=!1}=t;let a=null,f=1;const i=(...s)=>{A&&console.log("[vue3-auto-scale]",...s)},u=()=>{const s=document.getElementById(d);if(!s){i(`❌ 未找到 id 为 "${d}" 的容器`);return}const m=window.innerWidth,g=window.innerHeight,S=m/e,w=g/o,l=n==="contain"?Math.min(S,w):Math.max(S,w);f=l,s.style.transform=`scale(${l})`,s.style.transformOrigin="top left";const p=(m-e*l)/2,h=(g-o*l)/2;s.style.marginLeft=`${p}px`,s.style.marginTop=`${h}px`,i(`✅ 缩放比例: ${l.toFixed(4)}, 偏移: (${p.toFixed(0)}, ${h.toFixed(0)})`)},v=()=>{a&&(clearTimeout(a),a=null),a=setTimeout(u,y)},$=()=>{u(),window.addEventListener("resize",v),window.addEventListener("orientationchange",()=>{setTimeout(u,300)}),i("🚀 自适应缩放已启动")},I=()=>{window.removeEventListener("resize",v),a&&(clearTimeout(a),a=null),i("🛑 自适应缩放已销毁")},_=()=>f;return $(),{update:u,destroy:I,getScale:_}}function x(t={}){const e=c.shallowRef(null),o=c.shallowRef(1);return c.onMounted(()=>{requestAnimationFrame(()=>{e.value=r({...t,debug:t.debug??!1});const n=setInterval(()=>{e.value&&(o.value=e.value.getScale())},500);c.onBeforeUnmount(()=>{clearInterval(n)})})}),c.onBeforeUnmount(()=>{e.value&&(e.value.destroy(),e.value=null)}),{scale:o,update:()=>{var n;return(n=e.value)==null?void 0:n.update()},instance:e}}const T={mounted(t,e){const o=e.value||{},n=r({...o,rootId:t.id||void 0});t.__autoScaleInstance=n},beforeUnmount(t){const e=t.__autoScaleInstance;e&&(e.destroy(),delete t.__autoScaleInstance)}},b={useAutoScale,vAutoScale,createAutoScale};exports.createAutoScale=r;exports.default=b;exports.useAutoScale=x;exports.vAutoScale=T;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(t,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],o):(t=typeof globalThis<"u"?globalThis:t||self,o(t.Vue3AutoScale={},t.Vue))})(this,function(t,o){"use strict";function r(n={}){const{designWidth:e=1920,designHeight:i=1080,mode:a="contain",rootId:f="app",delay:I=200,debug:_=!1}=n;let s=null,m=1;const u=(...l)=>{_&&console.log("[vue3-auto-scale]",...l)},d=()=>{const l=document.getElementById(f);if(!l){u(`❌ 未找到 id 为 "${f}" 的容器`);return}const S=window.innerWidth,g=window.innerHeight,p=S/e,h=g/i,c=a==="contain"?Math.min(p,h):Math.max(p,h);m=c,l.style.transform=`scale(${c})`,l.style.transformOrigin="top left";const w=(S-e*c)/2,y=(g-i*c)/2;l.style.marginLeft=`${w}px`,l.style.marginTop=`${y}px`,u(`✅ 缩放比例: ${c.toFixed(4)}, 偏移: (${w.toFixed(0)}, ${y.toFixed(0)})`)},v=()=>{s&&(clearTimeout(s),s=null),s=setTimeout(d,I)},b=()=>{d(),window.addEventListener("resize",v),window.addEventListener("orientationchange",()=>{setTimeout(d,300)}),u("🚀 自适应缩放已启动")},x=()=>{window.removeEventListener("resize",v),s&&(clearTimeout(s),s=null),u("🛑 自适应缩放已销毁")},M=()=>m;return b(),{update:d,destroy:x,getScale:M}}function A(n={}){const e=o.shallowRef(null),i=o.shallowRef(1);return o.onMounted(()=>{requestAnimationFrame(()=>{e.value=r({...n,debug:n.debug??!1});const a=setInterval(()=>{e.value&&(i.value=e.value.getScale())},500);o.onBeforeUnmount(()=>{clearInterval(a)})})}),o.onBeforeUnmount(()=>{e.value&&(e.value.destroy(),e.value=null)}),{scale:i,update:()=>{var a;return(a=e.value)==null?void 0:a.update()},instance:e}}const $={mounted(n,e){const i=e.value||{},a=r({...i,rootId:n.id||void 0});n.__autoScaleInstance=a},beforeUnmount(n){const e=n.__autoScaleInstance;e&&(e.destroy(),delete n.__autoScaleInstance)}},T={useAutoScale,vAutoScale,createAutoScale};t.createAutoScale=r,t.default=T,t.useAutoScale=A,t.vAutoScale=$,Object.defineProperties(t,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|