three-base 1.0.74 → 1.0.76
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/index.js +2 -0
- package/index.ts +4 -1
- package/lib/PlaneWithHtml.d.ts +10 -0
- package/lib/PlaneWithHtml.js +62 -0
- package/lib/PlaneWithHtml.ts +75 -0
- package/lib/ThreeBase.js +1 -4
- package/lib/ThreeBase.ts +1 -3
- package/lib/threeUtils.js +3 -4
- package/lib/threeUtils.ts +4 -4
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -32,3 +32,5 @@ export { default as SpriteModal1 } from "./lib/spriteModal1/SpriteModal1";
|
|
|
32
32
|
export { default as PlaneModal1 } from "./lib/planeModal1/PlaneModal1";
|
|
33
33
|
// 3D文字
|
|
34
34
|
export { default as Text3D } from "./lib/Text3D/Text3D";
|
|
35
|
+
// html面板
|
|
36
|
+
export { default as PlaneWithHtml } from "./lib/PlaneWithHtml";
|
package/index.ts
CHANGED
|
@@ -53,4 +53,7 @@ export { default as PlaneModal1 } from "./lib/planeModal1/PlaneModal1";
|
|
|
53
53
|
|
|
54
54
|
// 3D文字
|
|
55
55
|
export { default as Text3D } from "./lib/Text3D/Text3D";
|
|
56
|
-
export type { Text3DProps } from "./lib/Text3D/Text3D";
|
|
56
|
+
export type { Text3DProps } from "./lib/Text3D/Text3D";
|
|
57
|
+
|
|
58
|
+
// html面板
|
|
59
|
+
export { default as PlaneWithHtml } from "./lib/PlaneWithHtml";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Mesh, MeshBasicMaterial, PlaneGeometry, Vector2 } from "three";
|
|
2
|
+
export default class PlaneWithHtml extends Mesh {
|
|
3
|
+
constructor(html?: string);
|
|
4
|
+
size: Vector2;
|
|
5
|
+
geometry: PlaneGeometry;
|
|
6
|
+
material: MeshBasicMaterial;
|
|
7
|
+
div: HTMLDivElement;
|
|
8
|
+
setGeoSize(width: number, height: number): void;
|
|
9
|
+
setHtml(html?: string): Promise<void>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @Author: xiaosihan
|
|
3
|
+
* @Date: 2023-04-15 11:26:32
|
|
4
|
+
* @Last Modified by: xiaosihan
|
|
5
|
+
* @Last Modified time: 2023-04-15 12:38:09
|
|
6
|
+
*/
|
|
7
|
+
import html2canvas from "html2canvas";
|
|
8
|
+
import { CanvasTexture, DoubleSide, Mesh, MeshBasicMaterial, PlaneGeometry, Vector2 } from "three";
|
|
9
|
+
import { degToRad } from "three/src/math/MathUtils";
|
|
10
|
+
import threeLoader from "./threeLoader";
|
|
11
|
+
// html 文本面板
|
|
12
|
+
export default class PlaneWithHtml extends Mesh {
|
|
13
|
+
constructor(html = "") {
|
|
14
|
+
super();
|
|
15
|
+
this.setHtml(html);
|
|
16
|
+
}
|
|
17
|
+
size = new Vector2(10, 10);
|
|
18
|
+
geometry = (() => {
|
|
19
|
+
const geometry = new PlaneGeometry(10, 10, 1, 1);
|
|
20
|
+
geometry.rotateX(degToRad(-90));
|
|
21
|
+
return geometry;
|
|
22
|
+
})();
|
|
23
|
+
material = new MeshBasicMaterial({
|
|
24
|
+
side: DoubleSide,
|
|
25
|
+
transparent: true
|
|
26
|
+
});
|
|
27
|
+
div = (() => {
|
|
28
|
+
const div = document.createElement("div");
|
|
29
|
+
div.style.position = "fixed";
|
|
30
|
+
div.style.top = "100%";
|
|
31
|
+
div.style.left = "100%";
|
|
32
|
+
div.style.pointerEvents = "none";
|
|
33
|
+
// div.style.border = "1px dashed #ff0000";
|
|
34
|
+
div.style.userSelect = "none";
|
|
35
|
+
div.style.lineHeight = "1";
|
|
36
|
+
div.style.whiteSpace = "nowrap";
|
|
37
|
+
return div;
|
|
38
|
+
})();
|
|
39
|
+
// 设置网格尺寸
|
|
40
|
+
setGeoSize(width, height) {
|
|
41
|
+
this.size.set(width, height);
|
|
42
|
+
this.geometry.dispose();
|
|
43
|
+
const geometry = new PlaneGeometry(width, height, 1, 1);
|
|
44
|
+
geometry.rotateX(degToRad(-90));
|
|
45
|
+
this.geometry = geometry;
|
|
46
|
+
}
|
|
47
|
+
async setHtml(html = "") {
|
|
48
|
+
this.div.innerHTML = html;
|
|
49
|
+
document.body.appendChild(this.div);
|
|
50
|
+
html2canvas(this.div, {
|
|
51
|
+
backgroundColor: "#00000000",
|
|
52
|
+
removeContainer: true
|
|
53
|
+
}).then(async (canvas) => {
|
|
54
|
+
this.setGeoSize(canvas.width, canvas.height);
|
|
55
|
+
this.material.map = new CanvasTexture(canvas);
|
|
56
|
+
this.material.map.anisotropy = threeLoader.anisotropy;
|
|
57
|
+
this.material.needsUpdate = true;
|
|
58
|
+
await new Promise(resolve => requestAnimationFrame(resolve));
|
|
59
|
+
this.div.remove();
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @Author: xiaosihan
|
|
3
|
+
* @Date: 2023-04-15 11:26:32
|
|
4
|
+
* @Last Modified by: xiaosihan
|
|
5
|
+
* @Last Modified time: 2023-04-15 12:38:09
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import html2canvas from "html2canvas";
|
|
9
|
+
import { CanvasTexture, DoubleSide, Mesh, MeshBasicMaterial, PlaneGeometry, Vector2 } from "three";
|
|
10
|
+
import { degToRad } from "three/src/math/MathUtils";
|
|
11
|
+
import threeLoader from "./threeLoader";
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
// html 文本面板
|
|
15
|
+
export default class PlaneWithHtml extends Mesh {
|
|
16
|
+
|
|
17
|
+
constructor(html: string = "") {
|
|
18
|
+
super();
|
|
19
|
+
this.setHtml(html);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
size = new Vector2(10, 10);
|
|
23
|
+
|
|
24
|
+
geometry = (() => {
|
|
25
|
+
const geometry = new PlaneGeometry(10, 10, 1, 1);
|
|
26
|
+
geometry.rotateX(degToRad(-90));
|
|
27
|
+
return geometry;
|
|
28
|
+
})();
|
|
29
|
+
|
|
30
|
+
material = new MeshBasicMaterial({
|
|
31
|
+
side: DoubleSide,
|
|
32
|
+
transparent: true
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
div = (() => {
|
|
36
|
+
const div = document.createElement("div");
|
|
37
|
+
div.style.position = "fixed";
|
|
38
|
+
div.style.top = "100%";
|
|
39
|
+
div.style.left = "100%";
|
|
40
|
+
div.style.pointerEvents = "none";
|
|
41
|
+
// div.style.border = "1px dashed #ff0000";
|
|
42
|
+
div.style.userSelect = "none";
|
|
43
|
+
div.style.lineHeight = "1";
|
|
44
|
+
div.style.whiteSpace = "nowrap";
|
|
45
|
+
return div;
|
|
46
|
+
})();
|
|
47
|
+
|
|
48
|
+
// 设置网格尺寸
|
|
49
|
+
setGeoSize(width: number, height: number) {
|
|
50
|
+
this.size.set(width, height);
|
|
51
|
+
this.geometry.dispose();
|
|
52
|
+
const geometry = new PlaneGeometry(width, height, 1, 1);
|
|
53
|
+
geometry.rotateX(degToRad(-90));
|
|
54
|
+
this.geometry = geometry;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async setHtml(html: string = "") {
|
|
58
|
+
this.div.innerHTML = html;
|
|
59
|
+
document.body.appendChild(this.div);
|
|
60
|
+
html2canvas(this.div, {
|
|
61
|
+
backgroundColor: "#00000000",
|
|
62
|
+
removeContainer: true
|
|
63
|
+
}).then(async (canvas) => {
|
|
64
|
+
this.setGeoSize(canvas.width, canvas.height);
|
|
65
|
+
this.material.map = new CanvasTexture(canvas);
|
|
66
|
+
this.material.map.anisotropy = threeLoader.anisotropy;
|
|
67
|
+
this.material.needsUpdate = true;
|
|
68
|
+
await new Promise(resolve => requestAnimationFrame(resolve));
|
|
69
|
+
this.div.remove();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
}
|
package/lib/ThreeBase.js
CHANGED
|
@@ -70,10 +70,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
70
70
|
// 设置 dom 容器
|
|
71
71
|
async setContainer(div) {
|
|
72
72
|
if (div) {
|
|
73
|
-
|
|
74
|
-
const canvas = div.getElementsByTagName("canvas");
|
|
75
|
-
for (let c of canvas)
|
|
76
|
-
c.remove();
|
|
73
|
+
div.innerHTML = "";
|
|
77
74
|
div.append(this.renderer.domElement);
|
|
78
75
|
// 如果上下文被销毁了, 就恢复上下文
|
|
79
76
|
if (this.isRendererContextLost) {
|
package/lib/ThreeBase.ts
CHANGED
|
@@ -87,9 +87,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
87
87
|
// 设置 dom 容器
|
|
88
88
|
async setContainer(div: HTMLDivElement | null) {
|
|
89
89
|
if (div) {
|
|
90
|
-
|
|
91
|
-
const canvas = div.getElementsByTagName("canvas");
|
|
92
|
-
for (let c of canvas) c.remove();
|
|
90
|
+
div.innerHTML = "";
|
|
93
91
|
div.append(this.renderer.domElement);
|
|
94
92
|
|
|
95
93
|
// 如果上下文被销毁了, 就恢复上下文
|
package/lib/threeUtils.js
CHANGED
|
@@ -46,10 +46,8 @@ class ThreeUtils extends EventDispatcher {
|
|
|
46
46
|
this._keepZoomDoms = this._keepZoomDoms.filter(l => this.inDocument(l.dom));
|
|
47
47
|
//2.设置dom的缩放值样式
|
|
48
48
|
this._keepZoomDoms.map((item, i) => {
|
|
49
|
-
let
|
|
50
|
-
let
|
|
51
|
-
let height = parseInt(parentRact.height);
|
|
52
|
-
let scale = Math.min(width / item.width, height / item.height);
|
|
49
|
+
let parentDom = item.dom.parentNode;
|
|
50
|
+
let scale = Math.min(parentDom.clientWidth / item.width, parentDom.clientHeight / item.height);
|
|
53
51
|
Object.assign(item.dom.style, {
|
|
54
52
|
position: "absolute",
|
|
55
53
|
top: "50%",
|
|
@@ -70,6 +68,7 @@ class ThreeUtils extends EventDispatcher {
|
|
|
70
68
|
// 把配置加入到列表中
|
|
71
69
|
this._keepZoomDoms.push({ dom, width, height });
|
|
72
70
|
dom.dataset.a = "这个元素缩放比例由 threeUtils.keepZoom 方法设置";
|
|
71
|
+
this._computerDomZoom();
|
|
73
72
|
}
|
|
74
73
|
}
|
|
75
74
|
workList = [];
|
package/lib/threeUtils.ts
CHANGED
|
@@ -63,10 +63,8 @@ class ThreeUtils extends EventDispatcher {
|
|
|
63
63
|
|
|
64
64
|
//2.设置dom的缩放值样式
|
|
65
65
|
this._keepZoomDoms.map((item, i) => {
|
|
66
|
-
let
|
|
67
|
-
let
|
|
68
|
-
let height = parseInt(parentRact.height);
|
|
69
|
-
let scale = Math.min(width / item.width, height / item.height);
|
|
66
|
+
let parentDom = item.dom.parentNode as HTMLDivElement;
|
|
67
|
+
let scale = Math.min(parentDom.clientWidth / item.width, parentDom.clientHeight / item.height);
|
|
70
68
|
Object.assign(item.dom.style, {
|
|
71
69
|
position: "absolute",
|
|
72
70
|
top: "50%",
|
|
@@ -91,6 +89,8 @@ class ThreeUtils extends EventDispatcher {
|
|
|
91
89
|
this._keepZoomDoms.push({ dom, width, height });
|
|
92
90
|
|
|
93
91
|
dom.dataset.a = "这个元素缩放比例由 threeUtils.keepZoom 方法设置";
|
|
92
|
+
|
|
93
|
+
this._computerDomZoom();
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
|