@inweb/viewer-visualize 26.9.0 → 26.9.2

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.
@@ -67,14 +67,15 @@ export class Viewer
67
67
  private _activeDragger: IDragger | null;
68
68
  private _components: Array<IComponent>;
69
69
  private _enableAutoUpdate: boolean;
70
- private _isNeedRender: boolean;
71
- private _isRunAsyncUpdate: boolean;
70
+ private _renderNeeded: boolean;
72
71
  private _renderTime: DOMHighResTimeStamp;
72
+ private _isRunAsyncUpdate: boolean;
73
73
 
74
74
  protected _options: Options;
75
75
  protected _visualizeJsUrl = "";
76
76
  protected _visualizeJs: any;
77
77
  protected _visualizeTimestamp: number;
78
+ protected _viewer: any;
78
79
  protected _crossOrigin;
79
80
 
80
81
  private canvaseventlistener: (event: Event) => void;
@@ -126,7 +127,7 @@ export class Viewer
126
127
  this.canvaseventlistener = (event: Event) => this.emit(event);
127
128
 
128
129
  this._enableAutoUpdate = params.enableAutoUpdate ?? true;
129
- this._isNeedRender = false;
130
+ this._renderNeeded = false;
130
131
  this._isRunAsyncUpdate = false;
131
132
 
132
133
  this.render = this.render.bind(this);
@@ -196,16 +197,19 @@ export class Viewer
196
197
  async initialize(canvas: HTMLCanvasElement, onProgress?: (event: ProgressEvent) => void): Promise<this> {
197
198
  this.addEventListener("optionschange", (event) => this.syncOptions(event.data));
198
199
 
199
- if (canvas.style.width === "" && canvas.style.height === "") {
200
- canvas.style.width = "100%";
201
- canvas.style.height = "100%";
202
- }
200
+ const rect = canvas.parentElement.getBoundingClientRect();
201
+ const width = rect.width || 1;
202
+ const height = rect.height || 1;
203
+
204
+ canvas.width = Math.round(width * window.devicePixelRatio);
205
+ canvas.height = Math.round(height * window.devicePixelRatio);
206
+
207
+ canvas.style.width = width + "px";
208
+ canvas.style.height = height + "px";
209
+
203
210
  canvas.parentElement.style.touchAction = "none";
204
211
  canvas.style.touchAction = "none";
205
212
 
206
- canvas.width = canvas.clientWidth * window.devicePixelRatio;
207
- canvas.height = canvas.clientHeight * window.devicePixelRatio;
208
-
209
213
  this._visualizeTimestamp = Date.now();
210
214
  const visualizeTimestamp = this._visualizeTimestamp;
211
215
 
@@ -225,9 +229,10 @@ export class Viewer
225
229
  );
226
230
 
227
231
  this._visualizeJs = visualizeJs;
228
- this.visualizeJs.canvas = canvas;
229
- this.visualizeJs.Viewer.create();
230
- this.visualizeJs.getViewer().resize(0, canvas.width, canvas.height, 0);
232
+ this._visualizeJs.canvas = canvas;
233
+
234
+ this._viewer = visualizeJs.Viewer.create();
235
+ this._viewer.resize(0, canvas.width, canvas.height, 0);
231
236
 
232
237
  this.canvas = canvas;
233
238
  this.canvasEvents.forEach((x) => canvas.addEventListener(x, this.canvaseventlistener));
@@ -238,7 +243,6 @@ export class Viewer
238
243
  this._components.push(components.createComponent(name, this));
239
244
  }
240
245
 
241
- this.syncOpenCloudVisualStyle(true);
242
246
  this.syncOptions();
243
247
  this.syncOverlay();
244
248
 
@@ -269,10 +273,11 @@ export class Viewer
269
273
  this.canvas = undefined;
270
274
  }
271
275
 
272
- if (this._visualizeJs) this._visualizeJs.getViewer().clear();
276
+ if (this._viewer) this._viewer.clear();
273
277
 
274
278
  this._visualizeJs = undefined;
275
279
  this._visualizeTimestamp = undefined;
280
+ this._viewer = undefined;
276
281
 
277
282
  return this;
278
283
  }
@@ -284,40 +289,57 @@ export class Viewer
284
289
  return !!this.visualizeJs;
285
290
  }
286
291
 
292
+ setSize(width: number, height: number, updateStyle = true): void {
293
+ if (!this.visualizeJs) return;
294
+
295
+ this.canvas.width = Math.round(width * window.devicePixelRatio);
296
+ this.canvas.height = Math.round(height * window.devicePixelRatio);
297
+
298
+ if (updateStyle) {
299
+ this.canvas.style.width = width + "px";
300
+ this.canvas.style.height = height + "px";
301
+ }
302
+
303
+ this._viewer.resize(0, this.canvas.width, this.canvas.height, 0);
304
+
305
+ this.update(true);
306
+ this.emitEvent({ type: "resize", width, height });
307
+ }
308
+
287
309
  // internal render/resize routines
288
310
 
289
- public render(time: DOMHighResTimeStamp) {
311
+ public render(time?: DOMHighResTimeStamp) {
290
312
  if (!this.visualizeJs) return;
291
-
292
313
  if (this._isRunAsyncUpdate) return;
293
314
 
294
- const visViewer = this.visualizeJs.getViewer();
295
- if (visViewer.isRunningAnimation() || this._isNeedRender) {
296
- visViewer.update();
297
- this._activeDragger?.updatePreview?.();
298
- this._isNeedRender = !visViewer.getActiveDevice().isValid();
315
+ const renderNeeded = this.visViewer().isRunningAnimation() || this._renderNeeded;
316
+ if (!renderNeeded) return;
299
317
 
300
- const deltaTime = (time - this._renderTime) / 1000;
301
- this._renderTime = time;
302
- this.emitEvent({ type: "render", time, deltaTime });
303
- }
318
+ if (!time) time = performance.now();
319
+ const deltaTime = (time - this._renderTime) / 1000;
320
+
321
+ this._renderTime = time;
322
+ this._renderNeeded = !this.visViewer().getActiveDevice().isValid();
323
+
324
+ this.visViewer().update();
325
+ this._activeDragger?.updatePreview?.();
326
+
327
+ this.emitEvent({ type: "render", time, deltaTime });
304
328
  }
305
329
 
306
330
  public resize(): this {
307
- if (!this.visualizeJs) return this;
308
-
309
- const { clientWidth, clientHeight } = this.canvas;
331
+ console.warn(
332
+ "Viewer.resize() has been deprecated since 26.9 and will be removed in a future release, use Viewer.setSize() instead."
333
+ );
310
334
 
311
- if (!clientWidth || !clientHeight) return this; // <- invisible viewer, or viewer with parent removed
335
+ if (!this.visualizeJs) return this;
336
+ if (!this.canvas.parentElement) return this;
312
337
 
313
- this.canvas.width = clientWidth * window.devicePixelRatio;
314
- this.canvas.height = clientHeight * window.devicePixelRatio;
338
+ const { width, height } = this.canvas.parentElement.getBoundingClientRect();
315
339
 
316
- const visViewer = this.visualizeJs.getViewer();
317
- visViewer.resize(0, this.canvas.width, this.canvas.height, 0);
340
+ if (!width || !height) return this; // <- invisible viewer, or viewer with parent removed
318
341
 
319
- this.update(true);
320
- this.emitEvent({ type: "resize", width: clientWidth, height: clientHeight });
342
+ this.setSize(width, height);
321
343
 
322
344
  return this;
323
345
  }
@@ -337,12 +359,8 @@ export class Viewer
337
359
  */
338
360
  update(force = false) {
339
361
  if (this._enableAutoUpdate) {
340
- if (force) {
341
- this.visViewer()?.update();
342
- this._activeDragger?.updatePreview?.();
343
- } else {
344
- this._isNeedRender = true;
345
- }
362
+ this._renderNeeded = true;
363
+ if (force) this.render();
346
364
  }
347
365
  this.emitEvent({ type: "update", data: force });
348
366
  }
@@ -380,9 +398,11 @@ export class Viewer
380
398
  * @param maxScheduleUpdateCount - Maximum count of scheduled updates.
381
399
  */
382
400
  async updateAsync(maxScheduleUpdateTimeInMs = 50, maxScheduleUpdateCount = 50): Promise<void> {
401
+ if (!this.visualizeJs) return;
402
+
383
403
  this._isRunAsyncUpdate = true;
384
- const device = this.visViewer().getActiveDevice();
385
404
  try {
405
+ const device = this.visViewer().getActiveDevice();
386
406
  for (let iterationCount = 0; !device.isValid() && iterationCount < maxScheduleUpdateCount; iterationCount++) {
387
407
  await this.scheduleUpdateAsync(maxScheduleUpdateTimeInMs);
388
408
  }
@@ -407,7 +427,7 @@ export class Viewer
407
427
  * instance.
408
428
  */
409
429
  visLib(): any {
410
- return this.visualizeJs;
430
+ return this._visualizeJs;
411
431
  }
412
432
 
413
433
  /**
@@ -415,16 +435,16 @@ export class Viewer
415
435
  * instance.
416
436
  */
417
437
  visViewer(): any {
418
- return this.visualizeJs?.getViewer();
438
+ return this._viewer;
419
439
  }
420
440
 
421
441
  // update the VisualizeJS options
422
442
 
423
- syncOpenCloudVisualStyle(isInitializing: boolean): this {
443
+ syncOpenCloudVisualStyle(): this {
424
444
  if (!this.visualizeJs) return this;
425
445
 
426
446
  const visLib = this.visLib();
427
- const visViewer = visLib.getViewer();
447
+ const visViewer = this.visViewer();
428
448
 
429
449
  const device = visViewer.getActiveDevice();
430
450
  if (device.isNull()) return this;
@@ -474,8 +494,10 @@ export class Viewer
474
494
  syncOptions(options: IOptions = this.options): this {
475
495
  if (!this.visualizeJs) return this;
476
496
 
497
+ this.syncOpenCloudVisualStyle();
498
+
477
499
  const visLib = this.visLib();
478
- const visViewer = visLib.getViewer();
500
+ const visViewer = this.visViewer();
479
501
 
480
502
  const device = visViewer.getActiveDevice();
481
503
  if (device.isNull()) return this;
@@ -497,7 +519,7 @@ export class Viewer
497
519
  visViewer.shadows = options.shadows;
498
520
 
499
521
  const canvas = visLib.canvas;
500
- device.invalidate([0, canvas.clientWidth, canvas.clientHeight, 0]);
522
+ device.invalidate([0, canvas.width, canvas.height, 0]);
501
523
  }
502
524
 
503
525
  if (options.groundShadow !== visViewer.groundShadow) {
@@ -550,7 +572,7 @@ export class Viewer
550
572
  const params = options.enableCustomHighlight ? options : Options.defaults();
551
573
 
552
574
  const visLib = this.visLib();
553
- const visViewer = visLib.getViewer();
575
+ const visViewer = this.visViewer();
554
576
  const { Entry, OdTvRGBColorDef } = visLib;
555
577
 
556
578
  const highlightStyleId = visViewer.findHighlightStyle("Web_Default");
@@ -593,7 +615,7 @@ export class Viewer
593
615
  if (!device.isNull()) {
594
616
  const canvas = visLib.canvas;
595
617
 
596
- device.invalidate([0, canvas.clientWidth, canvas.clientHeight, 0]);
618
+ device.invalidate([0, canvas.width, canvas.height, 0]);
597
619
  device.delete();
598
620
  }
599
621
 
@@ -839,7 +861,7 @@ export class Viewer
839
861
  if (!this.visualizeJs) return;
840
862
 
841
863
  const visLib = this.visLib();
842
- const visViewer = visLib.getViewer();
864
+ const visViewer = this.visViewer();
843
865
 
844
866
  const device = visViewer.getActiveDevice();
845
867
  if (isExist(options.sceneGraph)) {
@@ -982,18 +1004,16 @@ export class Viewer
982
1004
 
983
1005
  this.emitEvent({ type: "open", file: buffer });
984
1006
 
985
- const visLib = this.visLib();
986
- const visViewer = visLib.getViewer();
1007
+ const visViewer = this.visViewer();
987
1008
 
988
1009
  this.emitEvent({ type: "geometrystart", file: buffer });
989
1010
  try {
990
1011
  const data = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
991
1012
  visViewer.parseFile(data);
992
1013
 
993
- this.syncOpenCloudVisualStyle(false);
994
1014
  this.syncOptions();
995
1015
  this.syncOverlay();
996
- this.resize();
1016
+ this.update(true);
997
1017
 
998
1018
  this.emitEvent({ type: "geometryprogress", data: 1, file: buffer });
999
1019
  this.emitEvent({ type: "databasechunk", data, file: buffer });
@@ -1023,18 +1043,16 @@ export class Viewer
1023
1043
 
1024
1044
  this.emitEvent({ type: "open", file: buffer });
1025
1045
 
1026
- const visLib = this.visLib();
1027
- const visViewer = visLib.getViewer();
1046
+ const visViewer = this.visViewer();
1028
1047
 
1029
1048
  this.emitEvent({ type: "geometrystart", file: buffer });
1030
1049
  try {
1031
1050
  const data = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
1032
1051
  visViewer.parseVsfx(data);
1033
1052
 
1034
- this.syncOpenCloudVisualStyle(false);
1035
1053
  this.syncOptions();
1036
1054
  this.syncOverlay();
1037
- this.resize();
1055
+ this.update(true);
1038
1056
 
1039
1057
  this.emitEvent({ type: "geometryprogress", data: 1, file: buffer });
1040
1058
  this.emitEvent({ type: "databasechunk", data, file: buffer });
@@ -1060,24 +1078,22 @@ export class Viewer
1060
1078
  clear(): this {
1061
1079
  if (!this.visualizeJs) return this;
1062
1080
 
1063
- const visLib = this.visLib();
1064
- const visViewer = visLib.getViewer();
1081
+ const visViewer = this.visViewer();
1065
1082
 
1066
1083
  this.setActiveDragger();
1067
1084
  this.clearSlices();
1068
1085
  this.clearOverlay();
1069
1086
  this.clearSelected();
1070
1087
 
1071
- visViewer.clear();
1072
- visViewer.createLocalDatabase();
1073
-
1074
1088
  this.loaders.forEach((loader) => loader.dispose());
1075
1089
  this.loaders = [];
1076
1090
 
1077
- this.syncOpenCloudVisualStyle(true);
1091
+ visViewer.clear();
1092
+ visViewer.createLocalDatabase();
1093
+
1078
1094
  this.syncOptions();
1079
1095
  this.syncOverlay();
1080
- this.resize();
1096
+ this.update(true);
1081
1097
 
1082
1098
  this.emitEvent({ type: "clear" });
1083
1099