mirage-engine 0.1.1 → 0.2.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 siwoo lee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # MirageEngine
2
+
3
+ > **An engine that mirrors HTML DOM elements to a WebGL scene in real-time.**
4
+
5
+ MirageEngine directly mirrors HTML DOM elements to WebGL objects. It observes DOM mutations and synchronizes position, style, and content in real-time, allowing standard HTML elements to exist within a WebGL context.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install mirage-engine
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ import { Mirage } from 'mirage-engine';
17
+
18
+ const engine = new Mirage("#target");
19
+ engine.start();
20
+ ```
@@ -0,0 +1 @@
1
+ export {};
package/dist/index.d.ts CHANGED
@@ -1 +1,2 @@
1
- export { Mirage } from './core/Mirage';
1
+ export * from './src/index'
2
+ export {}
@@ -1,24 +1,26 @@
1
- var p = Object.defineProperty;
2
- var v = (r, e, t) => e in r ? p(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
3
- var i = (r, e, t) => (v(r, typeof e != "symbol" ? e + "" : e, t), t);
4
- import * as h from "three";
5
- class S {
1
+ var D = Object.defineProperty;
2
+ var E = (n, e, t) => e in n ? D(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
3
+ var s = (n, e, t) => (E(n, typeof e != "symbol" ? e + "" : e, t), t);
4
+ import * as a from "three";
5
+ const m = 0, u = 1, p = 2, y = 8;
6
+ class R {
6
7
  constructor() {
7
- i(this, "canvas");
8
- i(this, "scene");
9
- i(this, "camera");
10
- i(this, "renderer");
11
- i(this, "renderOrder", 0);
12
- this.canvas = document.createElement("canvas"), this.scene = new h.Scene();
8
+ s(this, "canvas");
9
+ s(this, "scene");
10
+ s(this, "camera");
11
+ s(this, "renderer");
12
+ s(this, "renderOrder", 0);
13
+ s(this, "meshMap", /* @__PURE__ */ new Map());
14
+ this.canvas = document.createElement("canvas"), this.scene = new a.Scene();
13
15
  const e = window.innerWidth, t = window.innerHeight;
14
- this.camera = new h.OrthographicCamera(
16
+ this.camera = new a.OrthographicCamera(
15
17
  e / -2,
16
18
  e / 2,
17
19
  t / 2,
18
20
  t / -2,
19
21
  1,
20
22
  1e3
21
- ), this.camera.position.z = 100, this.renderer = new h.WebGLRenderer({
23
+ ), this.camera.position.z = 100, this.renderer = new a.WebGLRenderer({
22
24
  canvas: this.canvas,
23
25
  alpha: !0
24
26
  }), this.renderer.setSize(e, t);
@@ -36,102 +38,119 @@ class S {
36
38
  setSize(e, t) {
37
39
  this.renderer.setSize(e, t), this.camera.left = e / -2, this.camera.right = e / 2, this.camera.top = t / 2, this.camera.bottom = t / -2, this.camera.updateProjectionMatrix();
38
40
  }
39
- clearScene() {
40
- for (; this.scene.children.length > 0; )
41
- this.scene.remove(this.scene.children[0]);
41
+ syncScene(e) {
42
+ this.renderOrder = 0;
43
+ const t = /* @__PURE__ */ new Set();
44
+ this.reconcileNode(e, t);
45
+ for (const [r, i] of this.meshMap.entries())
46
+ t.has(r) || (this.scene.remove(i), i.geometry.dispose(), i.material instanceof a.Material && i.material.dispose(), this.meshMap.delete(r));
42
47
  }
43
- buildScene(e) {
48
+ reconcileNode(e, t) {
44
49
  if (e.type === "BOX") {
45
- const t = new h.PlaneGeometry(
46
- e.rect.width,
47
- e.rect.height
48
- ), s = e.styles.backgroundColor;
49
- let o = s, n = 1;
50
- if (s === "transparent" || s === "rgba(0, 0, 0, 0)")
51
- o = "#ffffff", n = 0;
52
- else if (s.startsWith("rgba")) {
53
- const a = s.match(/[\d.]+/g);
54
- if (a && a.length >= 4) {
55
- const g = a[0], y = a[1], T = a[2];
56
- n = parseFloat(a[3]), o = `rgb(${g}, ${y}, ${T})`;
57
- }
50
+ t.add(e.element);
51
+ let r = this.meshMap.get(e.element);
52
+ if (r)
53
+ console.log("[V2] 기존 매쉬 재사용:", e.element);
54
+ else {
55
+ console.log("[V2] 매쉬 신규 생성:", e.element);
56
+ const i = new a.PlaneGeometry(1, 1), o = new a.MeshBasicMaterial({ transparent: !0 });
57
+ r = new a.Mesh(i, o), this.scene.add(r), this.meshMap.set(e.element, r);
58
58
  }
59
- const c = e.styles.opacity * n, d = new h.MeshBasicMaterial({
60
- color: o,
61
- opacity: c,
62
- transparent: c < 1
63
- }), l = new h.Mesh(t, d), u = this.renderer.domElement.width, f = this.renderer.domElement.height, w = 1e-3;
64
- this.renderOrder++, l.position.set(
65
- e.rect.x - u / 2 + e.rect.width / 2,
66
- -e.rect.y + f / 2 - e.rect.height / 2,
67
- e.styles.zIndex + this.renderOrder * w
68
- ), this.scene.add(l);
69
- for (const a of e.children)
70
- this.buildScene(a);
59
+ this.updateMeshProperties(r, e);
60
+ for (const i of e.children)
61
+ this.reconcileNode(i, t);
71
62
  }
72
63
  }
73
- syncScene(e) {
74
- this.clearScene(), this.renderOrder = 0, this.buildScene(e);
64
+ updateMeshProperties(e, t) {
65
+ console.log(
66
+ `[V2] 업데이트 중인 매쉬 ID: ${e.uuid}, 마스크: ${t.dirtyMask}`
67
+ );
68
+ const { rect: r, styles: i } = t, o = this.renderer.domElement.width, l = this.renderer.domElement.height;
69
+ e.scale.set(r.width, r.height, 1);
70
+ const g = 1e-3;
71
+ this.renderOrder++, e.position.set(
72
+ r.x - o / 2 + r.width / 2,
73
+ -r.y + l / 2 - r.height / 2,
74
+ i.zIndex + this.renderOrder * g
75
+ );
76
+ const c = e.material, d = i.backgroundColor;
77
+ let f = d, w = 1;
78
+ if (d === "transparent" || d === "rgba(0, 0, 0, 0)")
79
+ f = "#ffffff", w = 0;
80
+ else if (d.startsWith("rgba")) {
81
+ const h = d.match(/[\d.]+/g);
82
+ if (h && h.length >= 4) {
83
+ const M = h[0], b = h[1], S = h[2];
84
+ w = parseFloat(h[3]), f = `rgb(${M}, ${b}, ${S})`;
85
+ }
86
+ }
87
+ const T = i.opacity * w;
88
+ c.color.set(f), c.opacity = T, c.transparent = T < 1, t.dirtyMask & u && console.log(" -> 위치/크기만 업데이트됨"), t.dirtyMask & p && console.log(" -> 스타일만 업데이트됨");
75
89
  }
76
90
  render() {
77
91
  this.renderer.render(this.scene, this.camera);
78
92
  }
79
93
  }
80
- function m(r) {
81
- if (r.tagName === "SCRIPT" || r.tagName === "STYLE")
94
+ function v(n, e = u | p) {
95
+ if (n.tagName === "SCRIPT" || n.tagName === "STYLE")
82
96
  return null;
83
- const e = r.getBoundingClientRect(), t = {
84
- x: e.x + window.scrollX,
85
- y: e.y + window.scrollY,
86
- width: e.width,
87
- height: e.height
88
- }, s = window.getComputedStyle(r), o = {
89
- backgroundColor: s.backgroundColor,
90
- opacity: parseFloat(s.opacity),
91
- zIndex: parseInt(s.zIndex, 10) || 0
92
- }, n = [];
93
- for (const c of r.children) {
94
- const d = m(c);
95
- d && n.push(d);
97
+ const t = n.getBoundingClientRect(), r = {
98
+ x: t.x + window.scrollX,
99
+ y: t.y + window.scrollY,
100
+ width: t.width,
101
+ height: t.height
102
+ }, i = window.getComputedStyle(n), o = {
103
+ backgroundColor: i.backgroundColor,
104
+ opacity: parseFloat(i.opacity),
105
+ zIndex: parseInt(i.zIndex, 10) || 0
106
+ }, l = [];
107
+ for (const g of n.children) {
108
+ const c = v(g);
109
+ c && l.push(c);
96
110
  }
97
111
  return {
98
112
  type: "BOX",
99
- rect: t,
113
+ element: n,
114
+ rect: r,
100
115
  styles: o,
101
- children: n
116
+ dirtyMask: e,
117
+ children: l
102
118
  };
103
119
  }
104
- class b {
120
+ class C {
105
121
  constructor(e, t) {
106
- i(this, "target");
107
- i(this, "renderer");
108
- i(this, "observer");
109
- i(this, "isDomDirty", !1);
110
- i(this, "isRunning", !1);
111
- i(this, "mutationTimer", null);
112
- i(this, "cssTimer", null);
113
- i(this, "onTransitionFinished", (e) => {
114
- this.target.contains(e.target) && this.mutationTimer === null && (this.cssTimer && clearTimeout(this.cssTimer), this.cssTimer = window.setTimeout(() => {
122
+ s(this, "target");
123
+ s(this, "renderer");
124
+ s(this, "observer");
125
+ s(this, "isDomDirty", !1);
126
+ s(this, "isRunning", !1);
127
+ s(this, "pendingMask", m);
128
+ s(this, "mutationTimer", null);
129
+ s(this, "cssTimer", null);
130
+ s(this, "onTransitionFinished", (e) => {
131
+ this.target.contains(e.target) && this.mutationTimer === null && (this.cssTimer && clearTimeout(this.cssTimer), this.pendingMask |= u | p, this.cssTimer = window.setTimeout(() => {
115
132
  this.isDomDirty = !0, this.cssTimer = null;
116
133
  }, 50));
117
134
  });
118
- i(this, "onWindowResize", () => {
135
+ s(this, "onWindowResize", () => {
119
136
  this.renderer.setSize(window.innerWidth, window.innerHeight), this.isDomDirty = !0;
120
137
  });
121
- i(this, "renderLoop", () => {
138
+ s(this, "renderLoop", () => {
122
139
  this.isRunning && (this.isDomDirty && this.forceUpdateScene(), this.renderer.render(), requestAnimationFrame(this.renderLoop));
123
140
  });
124
- this.target = e, this.renderer = t, this.observer = new MutationObserver((s) => {
125
- let o = !1, n = !1;
126
- for (const c of s)
127
- c.type === "childList" ? o = !0 : c.type === "attributes" && (n = !0);
128
- if (o) {
129
- this.clearTimers(), console.log("Structural Changed"), this.isDomDirty = !0;
130
- return;
141
+ this.target = e, this.renderer = t, this.observer = new MutationObserver((r) => {
142
+ let i = m;
143
+ for (const o of r)
144
+ o.type === "childList" ? i |= y : o.type === "attributes" && (o.attributeName === "style" || o.attributeName === "class") && (i |= u | p);
145
+ if (i !== m) {
146
+ if (this.pendingMask |= i, i & y) {
147
+ this.clearTimers(), console.log("Structural Change detected"), this.isDomDirty = !0;
148
+ return;
149
+ }
150
+ this.mutationTimer && clearTimeout(this.mutationTimer), this.mutationTimer = window.setTimeout(() => {
151
+ this.mutationTimer = null, this.isDomDirty = !0;
152
+ }, 200);
131
153
  }
132
- n && (this.mutationTimer && clearTimeout(this.mutationTimer), this.mutationTimer = window.setTimeout(() => {
133
- this.mutationTimer = null, this.isDomDirty = !0;
134
- }, 200));
135
154
  });
136
155
  }
137
156
  start() {
@@ -150,19 +169,19 @@ class b {
150
169
  }
151
170
  forceUpdateScene() {
152
171
  this.isDomDirty = !1;
153
- const e = m(this.target);
154
- e && this.renderer.syncScene(e);
172
+ const e = v(this.target, this.pendingMask);
173
+ e && this.renderer.syncScene(e), this.pendingMask = m;
155
174
  }
156
175
  }
157
- class C {
176
+ class k {
158
177
  constructor(e) {
159
- i(this, "renderer");
160
- i(this, "syncer");
161
- i(this, "target");
178
+ s(this, "renderer");
179
+ s(this, "syncer");
180
+ s(this, "target");
162
181
  const t = document.querySelector(e);
163
182
  if (!t)
164
183
  throw new Error(`[Mirage] Element not found: ${e}`);
165
- this.target = t, this.renderer = new S(), this.renderer.mount(document.body), this.syncer = new b(this.target, this.renderer);
184
+ this.target = t, this.renderer = new R(), this.renderer.mount(document.body), this.syncer = new C(this.target, this.renderer);
166
185
  }
167
186
  start() {
168
187
  this.syncer.start();
@@ -172,5 +191,5 @@ class C {
172
191
  }
173
192
  }
174
193
  export {
175
- C as Mirage
194
+ k as Mirage
176
195
  };
@@ -1 +1 @@
1
- (function(s,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("three")):typeof define=="function"&&define.amd?define(["exports","three"],o):(s=typeof globalThis<"u"?globalThis:s||self,o(s.MirageEngine={},s.THREE))})(this,function(s,o){"use strict";var O=Object.defineProperty;var C=(s,o,d)=>o in s?O(s,o,{enumerable:!0,configurable:!0,writable:!0,value:d}):s[o]=d;var i=(s,o,d)=>(C(s,typeof o!="symbol"?o+"":o,d),d);function d(r){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(r){for(const t in r)if(t!=="default"){const n=Object.getOwnPropertyDescriptor(r,t);Object.defineProperty(e,t,n.get?n:{enumerable:!0,get:()=>r[t]})}}return e.default=r,Object.freeze(e)}const u=d(o);class y{constructor(){i(this,"canvas");i(this,"scene");i(this,"camera");i(this,"renderer");i(this,"renderOrder",0);this.canvas=document.createElement("canvas"),this.scene=new u.Scene;const e=window.innerWidth,t=window.innerHeight;this.camera=new u.OrthographicCamera(e/-2,e/2,t/2,t/-2,1,1e3),this.camera.position.z=100,this.renderer=new u.WebGLRenderer({canvas:this.canvas,alpha:!0}),this.renderer.setSize(e,t)}mount(e){e.appendChild(this.canvas)}dispose(){try{this.renderer.dispose()}catch{}this.canvas.parentElement&&this.canvas.parentElement.removeChild(this.canvas)}setSize(e,t){this.renderer.setSize(e,t),this.camera.left=e/-2,this.camera.right=e/2,this.camera.top=t/2,this.camera.bottom=t/-2,this.camera.updateProjectionMatrix()}clearScene(){for(;this.scene.children.length>0;)this.scene.remove(this.scene.children[0])}buildScene(e){if(e.type==="BOX"){const t=new u.PlaneGeometry(e.rect.width,e.rect.height),n=e.styles.backgroundColor;let c=n,a=1;if(n==="transparent"||n==="rgba(0, 0, 0, 0)")c="#ffffff",a=0;else if(n.startsWith("rgba")){const h=n.match(/[\d.]+/g);if(h&&h.length>=4){const S=h[0],E=h[1],D=h[2];a=parseFloat(h[3]),c=`rgb(${S}, ${E}, ${D})`}}const l=e.styles.opacity*a,m=new u.MeshBasicMaterial({color:c,opacity:l,transparent:l<1}),g=new u.Mesh(t,m),T=this.renderer.domElement.width,b=this.renderer.domElement.height,v=.001;this.renderOrder++,g.position.set(e.rect.x-T/2+e.rect.width/2,-e.rect.y+b/2-e.rect.height/2,e.styles.zIndex+this.renderOrder*v),this.scene.add(g);for(const h of e.children)this.buildScene(h)}}syncScene(e){this.clearScene(),this.renderOrder=0,this.buildScene(e)}render(){this.renderer.render(this.scene,this.camera)}}function f(r){if(r.tagName==="SCRIPT"||r.tagName==="STYLE")return null;const e=r.getBoundingClientRect(),t={x:e.x+window.scrollX,y:e.y+window.scrollY,width:e.width,height:e.height},n=window.getComputedStyle(r),c={backgroundColor:n.backgroundColor,opacity:parseFloat(n.opacity),zIndex:parseInt(n.zIndex,10)||0},a=[];for(const l of r.children){const m=f(l);m&&a.push(m)}return{type:"BOX",rect:t,styles:c,children:a}}class p{constructor(e,t){i(this,"target");i(this,"renderer");i(this,"observer");i(this,"isDomDirty",!1);i(this,"isRunning",!1);i(this,"mutationTimer",null);i(this,"cssTimer",null);i(this,"onTransitionFinished",e=>{this.target.contains(e.target)&&this.mutationTimer===null&&(this.cssTimer&&clearTimeout(this.cssTimer),this.cssTimer=window.setTimeout(()=>{this.isDomDirty=!0,this.cssTimer=null},50))});i(this,"onWindowResize",()=>{this.renderer.setSize(window.innerWidth,window.innerHeight),this.isDomDirty=!0});i(this,"renderLoop",()=>{this.isRunning&&(this.isDomDirty&&this.forceUpdateScene(),this.renderer.render(),requestAnimationFrame(this.renderLoop))});this.target=e,this.renderer=t,this.observer=new MutationObserver(n=>{let c=!1,a=!1;for(const l of n)l.type==="childList"?c=!0:l.type==="attributes"&&(a=!0);if(c){this.clearTimers(),console.log("Structural Changed"),this.isDomDirty=!0;return}a&&(this.mutationTimer&&clearTimeout(this.mutationTimer),this.mutationTimer=window.setTimeout(()=>{this.mutationTimer=null,this.isDomDirty=!0},200))})}start(){this.isRunning||(this.isRunning=!0,this.observer.observe(this.target,{childList:!0,subtree:!0,attributes:!0,characterData:!0}),this.target.addEventListener("transitionend",this.onTransitionFinished),this.target.addEventListener("animationend",this.onTransitionFinished),window.addEventListener("resize",this.onWindowResize),this.forceUpdateScene(),this.renderLoop())}stop(){this.isRunning=!1,this.observer.disconnect(),this.clearTimers(),this.target.removeEventListener("transitionend",this.onTransitionFinished),this.target.removeEventListener("animationend",this.onTransitionFinished),window.removeEventListener("resize",this.onWindowResize)}clearTimers(){this.mutationTimer&&(clearTimeout(this.mutationTimer),this.mutationTimer=null),this.cssTimer&&(clearTimeout(this.cssTimer),this.cssTimer=null)}forceUpdateScene(){this.isDomDirty=!1;const e=f(this.target);e&&this.renderer.syncScene(e)}}class w{constructor(e){i(this,"renderer");i(this,"syncer");i(this,"target");const t=document.querySelector(e);if(!t)throw new Error(`[Mirage] Element not found: ${e}`);this.target=t,this.renderer=new y,this.renderer.mount(document.body),this.syncer=new p(this.target,this.renderer)}start(){this.syncer.start()}stop(){this.syncer.stop(),this.renderer.dispose()}}s.Mirage=w,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
1
+ (function(o,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("three")):typeof define=="function"&&define.amd?define(["exports","three"],a):(o=typeof globalThis<"u"?globalThis:o||self,a(o.MirageEngine={},o.THREE))})(this,function(o,a){"use strict";var z=Object.defineProperty;var L=(o,a,h)=>a in o?z(o,a,{enumerable:!0,configurable:!0,writable:!0,value:h}):o[a]=h;var s=(o,a,h)=>(L(o,typeof a!="symbol"?a+"":a,h),h);function h(n){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(n){for(const t in n)if(t!=="default"){const i=Object.getOwnPropertyDescriptor(n,t);Object.defineProperty(e,t,i.get?i:{enumerable:!0,get:()=>n[t]})}}return e.default=n,Object.freeze(e)}const d=h(a),f=0,p=1,g=2,b=8;class E{constructor(){s(this,"canvas");s(this,"scene");s(this,"camera");s(this,"renderer");s(this,"renderOrder",0);s(this,"meshMap",new Map);this.canvas=document.createElement("canvas"),this.scene=new d.Scene;const e=window.innerWidth,t=window.innerHeight;this.camera=new d.OrthographicCamera(e/-2,e/2,t/2,t/-2,1,1e3),this.camera.position.z=100,this.renderer=new d.WebGLRenderer({canvas:this.canvas,alpha:!0}),this.renderer.setSize(e,t)}mount(e){e.appendChild(this.canvas)}dispose(){try{this.renderer.dispose()}catch{}this.canvas.parentElement&&this.canvas.parentElement.removeChild(this.canvas)}setSize(e,t){this.renderer.setSize(e,t),this.camera.left=e/-2,this.camera.right=e/2,this.camera.top=t/2,this.camera.bottom=t/-2,this.camera.updateProjectionMatrix()}syncScene(e){this.renderOrder=0;const t=new Set;this.reconcileNode(e,t);for(const[i,r]of this.meshMap.entries())t.has(i)||(this.scene.remove(r),r.geometry.dispose(),r.material instanceof d.Material&&r.material.dispose(),this.meshMap.delete(i))}reconcileNode(e,t){if(e.type==="BOX"){t.add(e.element);let i=this.meshMap.get(e.element);if(i)console.log("[V2] 기존 매쉬 재사용:",e.element);else{console.log("[V2] 매쉬 신규 생성:",e.element);const r=new d.PlaneGeometry(1,1),c=new d.MeshBasicMaterial({transparent:!0});i=new d.Mesh(r,c),this.scene.add(i),this.meshMap.set(e.element,i)}this.updateMeshProperties(i,e);for(const r of e.children)this.reconcileNode(r,t)}}updateMeshProperties(e,t){console.log(`[V2] 업데이트 중인 매쉬 ID: ${e.uuid}, 마스크: ${t.dirtyMask}`);const{rect:i,styles:r}=t,c=this.renderer.domElement.width,y=this.renderer.domElement.height;e.scale.set(i.width,i.height,1);const T=.001;this.renderOrder++,e.position.set(i.x-c/2+i.width/2,-i.y+y/2-i.height/2,r.zIndex+this.renderOrder*T);const l=e.material,u=r.backgroundColor;let w=u,M=1;if(u==="transparent"||u==="rgba(0, 0, 0, 0)")w="#ffffff",M=0;else if(u.startsWith("rgba")){const m=u.match(/[\d.]+/g);if(m&&m.length>=4){const O=m[0],C=m[1],k=m[2];M=parseFloat(m[3]),w=`rgb(${O}, ${C}, ${k})`}}const S=r.opacity*M;l.color.set(w),l.opacity=S,l.transparent=S<1,t.dirtyMask&p&&console.log(" -> 위치/크기만 업데이트됨"),t.dirtyMask&g&&console.log(" -> 스타일만 업데이트됨")}render(){this.renderer.render(this.scene,this.camera)}}function v(n,e=p|g){if(n.tagName==="SCRIPT"||n.tagName==="STYLE")return null;const t=n.getBoundingClientRect(),i={x:t.x+window.scrollX,y:t.y+window.scrollY,width:t.width,height:t.height},r=window.getComputedStyle(n),c={backgroundColor:r.backgroundColor,opacity:parseFloat(r.opacity),zIndex:parseInt(r.zIndex,10)||0},y=[];for(const T of n.children){const l=v(T);l&&y.push(l)}return{type:"BOX",element:n,rect:i,styles:c,dirtyMask:e,children:y}}class D{constructor(e,t){s(this,"target");s(this,"renderer");s(this,"observer");s(this,"isDomDirty",!1);s(this,"isRunning",!1);s(this,"pendingMask",f);s(this,"mutationTimer",null);s(this,"cssTimer",null);s(this,"onTransitionFinished",e=>{this.target.contains(e.target)&&this.mutationTimer===null&&(this.cssTimer&&clearTimeout(this.cssTimer),this.pendingMask|=p|g,this.cssTimer=window.setTimeout(()=>{this.isDomDirty=!0,this.cssTimer=null},50))});s(this,"onWindowResize",()=>{this.renderer.setSize(window.innerWidth,window.innerHeight),this.isDomDirty=!0});s(this,"renderLoop",()=>{this.isRunning&&(this.isDomDirty&&this.forceUpdateScene(),this.renderer.render(),requestAnimationFrame(this.renderLoop))});this.target=e,this.renderer=t,this.observer=new MutationObserver(i=>{let r=f;for(const c of i)c.type==="childList"?r|=b:c.type==="attributes"&&(c.attributeName==="style"||c.attributeName==="class")&&(r|=p|g);if(r!==f){if(this.pendingMask|=r,r&b){this.clearTimers(),console.log("Structural Change detected"),this.isDomDirty=!0;return}this.mutationTimer&&clearTimeout(this.mutationTimer),this.mutationTimer=window.setTimeout(()=>{this.mutationTimer=null,this.isDomDirty=!0},200)}})}start(){this.isRunning||(this.isRunning=!0,this.observer.observe(this.target,{childList:!0,subtree:!0,attributes:!0,characterData:!0}),this.target.addEventListener("transitionend",this.onTransitionFinished),this.target.addEventListener("animationend",this.onTransitionFinished),window.addEventListener("resize",this.onWindowResize),this.forceUpdateScene(),this.renderLoop())}stop(){this.isRunning=!1,this.observer.disconnect(),this.clearTimers(),this.target.removeEventListener("transitionend",this.onTransitionFinished),this.target.removeEventListener("animationend",this.onTransitionFinished),window.removeEventListener("resize",this.onWindowResize)}clearTimers(){this.mutationTimer&&(clearTimeout(this.mutationTimer),this.mutationTimer=null),this.cssTimer&&(clearTimeout(this.cssTimer),this.cssTimer=null)}forceUpdateScene(){this.isDomDirty=!1;const e=v(this.target,this.pendingMask);e&&this.renderer.syncScene(e),this.pendingMask=f}}class R{constructor(e){s(this,"renderer");s(this,"syncer");s(this,"target");const t=document.querySelector(e);if(!t)throw new Error(`[Mirage] Element not found: ${e}`);this.target=t,this.renderer=new E,this.renderer.mount(document.body),this.syncer=new D(this.target,this.renderer)}start(){this.syncer.start()}stop(){this.syncer.stop(),this.renderer.dispose()}}o.Mirage=R,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
@@ -5,6 +5,7 @@ export declare class Syncer {
5
5
  private observer;
6
6
  private isDomDirty;
7
7
  private isRunning;
8
+ private pendingMask;
8
9
  private mutationTimer;
9
10
  private cssTimer;
10
11
  constructor(target: HTMLElement, renderer: Renderer);
@@ -0,0 +1,2 @@
1
+ import { SceneNode } from '../types';
2
+ export declare function extractSceneGraph(element: HTMLElement, initialMask?: number): SceneNode | null;
@@ -0,0 +1 @@
1
+ export { Mirage } from './core/Mirage';
@@ -5,12 +5,13 @@ export declare class Renderer {
5
5
  private readonly camera;
6
6
  private readonly renderer;
7
7
  private renderOrder;
8
+ private meshMap;
8
9
  constructor();
9
10
  mount(parent: HTMLElement): void;
10
11
  dispose(): void;
11
12
  setSize(width: number, height: number): void;
12
- private clearScene;
13
- private buildScene;
14
13
  syncScene(graphNode: SceneNode): void;
14
+ private reconcileNode;
15
+ private updateMeshProperties;
15
16
  render(): void;
16
17
  }
@@ -11,7 +11,14 @@ export interface BoxStyles {
11
11
  }
12
12
  export interface SceneNode {
13
13
  type: "BOX";
14
+ element: HTMLElement;
14
15
  rect: NodeRect;
15
16
  styles: BoxStyles;
17
+ dirtyMask: number;
16
18
  children: SceneNode[];
17
19
  }
20
+ export declare const DIRTY_NONE = 0;
21
+ export declare const DIRTY_RECT: number;
22
+ export declare const DIRTY_STYLE: number;
23
+ export declare const DIRTY_ZINDEX: number;
24
+ export declare const DIRTY_STRUCTURE: number;
package/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "name": "mirage-engine",
3
- "version": "0.1.1",
3
+ "version": "0.2.3",
4
4
  "type": "module",
5
5
  "types": "./dist/index.d.ts",
6
6
  "main": "./dist/mirage-engine.umd.js",
7
7
  "module": "./dist/mirage-engine.js",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/dltldn333/MirageEngine"
11
+ },
8
12
  "exports": {
9
13
  ".": {
10
14
  "types": "./dist/index.d.ts",
@@ -1,2 +0,0 @@
1
- import { SceneNode } from '../types';
2
- export declare function extractSceneGraph(element: HTMLElement): SceneNode | null;
File without changes