aframe-babia-components 1.2.7 → 1.2.9

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.
@@ -0,0 +1,255 @@
1
+ AFRAME.registerComponent('babia-html', {
2
+ schema: {
3
+ html: { type: 'string' },
4
+ distanceLevels: { type: 'float', default: 0.7 },
5
+ renderHTML: { type: 'boolean', default: false },
6
+ renderHTMLOnlyLeafs: { type: 'boolean', default: false },
7
+ },
8
+
9
+ babiaDiv: null,
10
+ clickedBoxes: new Map(),
11
+
12
+ init: function () {
13
+ const self = this
14
+ let el = this.el;
15
+ let data = this.data;
16
+
17
+ // Crear un observador para detectar la adición del div al DOM
18
+ this.observer = new MutationObserver(function (mutations) {
19
+ mutations.forEach(function (mutation) {
20
+ if (mutation.addedNodes) {
21
+ mutation.addedNodes.forEach(function (node) {
22
+ if (node.id === 'babiaHtmlDiv') {
23
+ console.log('El div con id "babiaHtmlDiv" ha sido renderizado en el DOM');
24
+
25
+ self.processNodeNoOffset(node, null, 0);
26
+
27
+ self.observer.disconnect(); // Desconectar el observador si ya no se necesita
28
+ }
29
+ });
30
+ }
31
+ });
32
+ });
33
+
34
+ // Configuración del observador para observar cambios en el cuerpo del documento
35
+ this.observer.observe(document.body, { childList: true, subtree: true });
36
+
37
+ // Insert the HTML in order to render it
38
+ babiaDiv = document.createElement('div');
39
+ babiaDiv.setAttribute('id', 'babiaHtmlDiv');
40
+ babiaDiv.innerHTML = data.html;
41
+ document.body.appendChild(babiaDiv);
42
+
43
+
44
+ },
45
+
46
+ processNodeNoOffset: function (node, firstOffestToDelete, childrenLevel) {
47
+ let el = this.el
48
+ const children = node.children;
49
+
50
+ for (let i = 0; i < children.length; i++) {
51
+ const child = children[i];
52
+ const rect = child.getBoundingClientRect();
53
+
54
+ // Remove the first Y and X offset, since the HTML is rendered below the scene
55
+ if (!firstOffestToDelete) {
56
+ firstOffestToDelete = { x: rect.left / 100, y: rect.top / 100 }
57
+ }
58
+
59
+ // Calculate positions, adjusting the center of the box and removing the firstOffset
60
+ const offsetX = rect.right / 100 - (rect.width / 100) / 2 - firstOffestToDelete.x;
61
+ const offsetY = -rect.bottom / 100 + (rect.height / 100) / 2 + firstOffestToDelete.y;
62
+
63
+ // Create box
64
+ const box = document.createElement('a-box');
65
+ box.setAttribute('position', `${offsetX} ${offsetY} ${childrenLevel * this.data.distanceLevels}`);
66
+ box.setAttribute('width', rect.width / 100);
67
+ box.setAttribute('height', rect.height / 100);
68
+ box.setAttribute('depth', 0.01);
69
+
70
+
71
+ // Clickable
72
+ box.classList.add("babiaxraycasterclass")
73
+
74
+ // Store the HTML content in the box for later use
75
+ box.setAttribute('html-content', child.outerHTML);
76
+
77
+ // Add mouseenter and mouseleave events
78
+ box.addEventListener('mouseenter', () => {
79
+ if (!box.classList.contains('clicked')) {
80
+ this.showHtmlContent(box, child.outerHTML, 'temp');
81
+ }
82
+ });
83
+
84
+ box.addEventListener('mouseleave', () => {
85
+ if (!box.classList.contains('clicked')) {
86
+ this.removeHtmlContent('temp');
87
+ }
88
+ });
89
+
90
+ // Add click event to toggle the plane with the HTML content
91
+ box.addEventListener('click', (event) => {
92
+ event.stopPropagation(); // Prevent mouseenter/leave events from firing
93
+ if (box.classList.contains('clicked')) {
94
+ box.classList.remove('clicked');
95
+ this.removeHtmlContent('permanent', box);
96
+ } else {
97
+ box.classList.add('clicked');
98
+ this.showHtmlContent(box, child.outerHTML, 'permanent');
99
+ }
100
+ });
101
+
102
+
103
+ //Select if we want to render the HTML content as a plane or a texture, if not, color
104
+ if (this.data.renderHTML && typeof html2canvas !== "undefined") {
105
+ // Render all, or render only the last child
106
+ if (this.data.renderHTMLOnlyLeafs) {
107
+ if (child.children.length == 0) {
108
+ // That's a leaf
109
+ html2canvas(child).then(function (canvas) {
110
+ box.setAttribute('material', {
111
+ shader: 'flat',
112
+ src: canvas.toDataURL()
113
+ });
114
+ })
115
+ }else{
116
+ box.setAttribute('color', this.colors_grad[childrenLevel]);
117
+ }
118
+
119
+ } else {
120
+ html2canvas(child).then(function (canvas) {
121
+ box.setAttribute('material', {
122
+ shader: 'flat',
123
+ src: canvas.toDataURL()
124
+ });
125
+ })
126
+ }
127
+ } else {
128
+ // If renderHTML is not set, we use the color attribute to color the box
129
+ box.setAttribute('color', this.colors_grad[childrenLevel]);
130
+ }
131
+
132
+
133
+
134
+
135
+ el.appendChild(box);
136
+
137
+ // Create line to the parent
138
+ if (childrenLevel > 0) {
139
+ let line = document.createElement('a-entity')
140
+ line.setAttribute('line', {
141
+ start: `${offsetX} ${offsetY} ${childrenLevel * this.data.distanceLevels}`,
142
+ end: `${offsetX} ${offsetY} ${(childrenLevel * this.data.distanceLevels) - this.data.distanceLevels}`,
143
+ color: 'yellow'
144
+ })
145
+ el.appendChild(line);
146
+ }
147
+
148
+
149
+ if (child.children.length > 0) {
150
+ let newLevel = childrenLevel + 1;
151
+ this.processNodeNoOffset(child, firstOffestToDelete, newLevel);
152
+ }
153
+ }
154
+ },
155
+
156
+ showHtmlContent: function (box, htmlContent, type) {
157
+ // Remove any existing temporary plane
158
+ if (type === 'temp') {
159
+ this.removeHtmlContent('temp');
160
+ }
161
+
162
+ // Create the white plane
163
+ const plane = document.createElement('a-plane');
164
+ plane.setAttribute('class', `${type}-plane`);
165
+ let boxposition = box.getAttribute('position')
166
+ plane.setAttribute('position', { x: boxposition.x, y: boxposition.y + 1, z: boxposition.z + 0.2 });
167
+ plane.setAttribute('width', '3'); // Adjust width as needed
168
+ const lines = htmlContent.split('\n').length;
169
+ const lineHeight = 0.15; // Adjust based on font size and desired spacing
170
+ const planeHeight = Math.max(lines * lineHeight, 0.2);
171
+ plane.setAttribute('height', planeHeight); // Adjust height as needed
172
+ plane.setAttribute('color', 'white');
173
+
174
+ // Create the text element
175
+ const text = document.createElement('a-text');
176
+ text.setAttribute('value', htmlContent);
177
+ text.setAttribute('color', 'black');
178
+ text.setAttribute('align', 'left');
179
+ text.setAttribute('width', '2.5'); // Adjust text width as needed
180
+ text.setAttribute('position', '-1 0 0.01');
181
+
182
+ plane.appendChild(text);
183
+ this.el.appendChild(plane);
184
+
185
+ // Store a reference to the plane in the box element for later removal
186
+ if (type === 'permanent') {
187
+ box.permanentPlane = plane;
188
+ }
189
+ },
190
+
191
+ removeHtmlContent: function (type, box) {
192
+ if (type === 'temp') {
193
+ const tempPlane = document.querySelector('.temp-plane');
194
+ if (tempPlane) {
195
+ tempPlane.parentNode.removeChild(tempPlane);
196
+ }
197
+ } else if (type === 'permanent' && box) {
198
+ if (box.permanentPlane) {
199
+ box.permanentPlane.parentNode.removeChild(box.permanentPlane);
200
+ box.permanentPlane = null;
201
+ }
202
+
203
+ // Remove box from clickedBoxes map
204
+ this.clickedBoxes.delete(box);
205
+ }
206
+ },
207
+
208
+
209
+ colors_dif: [
210
+ "#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#FF00FF", "#00FFFF", "#800000", "#808000",
211
+ "#008000", "#800080", "#008080", "#000080", "#FF4500", "#2E8B57", "#4682B4", "#D2691E",
212
+ "#FF69B4", "#8A2BE2", "#5F9EA0", "#7FFF00", "#DDA0DD", "#B0E0E6", "#FF1493", "#32CD32",
213
+ "#FFD700", "#6A5ACD", "#40E0D0", "#FF6347", "#8B0000", "#9ACD32", "#00CED1", "#9400D3",
214
+ "#ADFF2F", "#F0E68C", "#E9967A", "#8FBC8F", "#483D8B", "#2F4F4F", "#7B68EE", "#6B8E23",
215
+ "#FF7F50", "#BA55D3", "#CD5C5C", "#20B2AA", "#778899", "#4682B4", "#D3D3D3", "#FFB6C1",
216
+ "#FFA07A", "#BDB76B", "#8B008B", "#556B2F", "#9932CC", "#B22222", "#5F9EA0", "#FFDAB9",
217
+ "#ADFF2F", "#E0FFFF", "#7CFC00", "#FF00FF", "#DAA520", "#32CD32", "#FA8072", "#8A2BE2",
218
+ "#7FFFD4", "#B8860B", "#FF4500", "#ADFF2F", "#DB7093", "#F08080", "#FF00FF", "#6495ED",
219
+ "#DC143C", "#00FA9A", "#B0C4DE", "#B03060", "#98FB98", "#6A5ACD", "#708090", "#FFD700",
220
+ "#20B2AA", "#66CDAA", "#7B68EE", "#00FF7F", "#8B0000", "#00FF00", "#F0FFF0", "#DC143C",
221
+ "#FF00FF", "#00BFFF", "#B22222", "#00FFFF", "#FFD700", "#DA70D6", "#F5DEB3", "#F4A460"
222
+ ],
223
+
224
+ colors_grad: [
225
+ "#4B0082", "#800080", "#B22222", "#A0522D", "#CD5C5C", "#8B008B", "#9932CC", "#FF4500",
226
+ "#FF8C00", "#B8860B", "#D2691E", "#DAA520", "#ADFF2F", "#7FFF00", "#32CD32", "#00FF00",
227
+ "#00FA9A", "#40E0D0", "#4682B4", "#1E90FF", "#0000FF", "#4169E1", "#8A2BE2", "#FF00FF",
228
+ "#FF1493", "#FF69B4", "#FF6347", "#FFA07A", "#FFA500", "#FFD700", "#FFFF00", "#FFFACD",
229
+ "#F0E68C", "#E6E6FA", "#F0F8FF", "#E0FFFF", "#AFEEEE", "#ADD8E6", "#87CEFA", "#B0C4DE",
230
+ "#D3D3D3", "#DDA0DD", "#EE82EE", "#F5DEB3", "#FAFAD2", "#FFE4E1", "#FFF0F5", "#F5F5DC",
231
+ "#FFFFE0", "#FFF8DC", "#F8F8FF", "#FFFFFF"
232
+ ],
233
+
234
+ update: function () {
235
+ let data = this.data
236
+ let oldData = this.previousOldData;
237
+
238
+ if (data.html != oldData.html || data.distanceLevels != oldData.distanceLevels) {
239
+ // Disconnect the old observer if it exists
240
+ if (this.observer) {
241
+ this.observer.disconnect();
242
+ }
243
+
244
+ // Remove existing boxes and planes
245
+ while (this.el.firstChild) {
246
+ this.el.removeChild(this.el.firstChild);
247
+ }
248
+
249
+ // Call init again to reinitialize the component
250
+ this.init();
251
+ }
252
+
253
+ }
254
+
255
+ });
@@ -58,8 +58,9 @@ AFRAME.registerComponent('babia-pie', {
58
58
  * Called on each scene tick.
59
59
  */
60
60
  tick: function (t, delta) {
61
+ let self = this
61
62
  if (this.data.animation && this.loaded) {
62
- let elements = document.getElementsByClassName('babiaxrChart')[0].children
63
+ let elements = self.el.children[0].children
63
64
  for (let slice in this.slice_array) {
64
65
  let delay = this.slice_array[slice].delay
65
66
  let max_length = this.slice_array[slice].degreeLenght