@netless/forge-whiteboard 1.1.5 → 1.2.0-beta.0

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.
@@ -28018,6 +28018,7 @@ var ImageModel = class extends ElementModel {
28018
28018
  }
28019
28019
  if (!this.imageSets.querySelector(`[id='${this.uuid}']`)) {
28020
28020
  const img = document.createElement("img");
28021
+ img.crossOrigin = "anonymous";
28021
28022
  img.src = this.src;
28022
28023
  img.id = this.uuid;
28023
28024
  this.imageSets.appendChild(img);
@@ -28224,14 +28225,55 @@ var RenderableModel = class extends import_eventemitter3.default {
28224
28225
  const model = new ImageModel(yMap, this.scope, this.imageSets, this.liveCursor, this.isPerformanceMode);
28225
28226
  model.bindObserver();
28226
28227
  model.root.set("src", src);
28227
- const initMatrix = new this.scope.Matrix();
28228
+ model.ownerId = this.userManager.selfId;
28229
+ this.fitImageToViewport(src, model);
28230
+ }
28231
+ fitImageToViewport(src, model) {
28228
28232
  const center = this.scope.project.view.center;
28229
- initMatrix.translate({
28233
+ const fallbackMatrix = new this.scope.Matrix();
28234
+ fallbackMatrix.translate({
28230
28235
  x: center.x,
28231
28236
  y: center.y
28232
28237
  });
28233
- model.appendPointsMatrix(initMatrix);
28234
- model.ownerId = this.userManager.selfId;
28238
+ this.setElementPointsMatrix(model, fallbackMatrix);
28239
+ const img = document.createElement("img");
28240
+ img.crossOrigin = "anonymous";
28241
+ img.onload = () => {
28242
+ const naturalWidth = img.naturalWidth || img.width;
28243
+ const naturalHeight = img.naturalHeight || img.height;
28244
+ if (naturalWidth <= 0 || naturalHeight <= 0) {
28245
+ return;
28246
+ }
28247
+ const viewportBounds = this.scope.project.view.bounds;
28248
+ const maxWidth = viewportBounds.width * 2 / 3;
28249
+ const maxHeight = viewportBounds.height * 2 / 3;
28250
+ const fitScale = Math.min(maxWidth / naturalWidth, maxHeight / naturalHeight, 1);
28251
+ const nextMatrix = new this.scope.Matrix();
28252
+ nextMatrix.translate({
28253
+ x: center.x,
28254
+ y: center.y
28255
+ });
28256
+ if (fitScale > 0 && Number.isFinite(fitScale)) {
28257
+ nextMatrix.scale(fitScale);
28258
+ }
28259
+ this.setElementPointsMatrix(model, nextMatrix);
28260
+ };
28261
+ img.onerror = () => {
28262
+ (0, import_forge_room6.log)("[@netless/forge-whiteboard] failed to preload image for viewport fitting", {
28263
+ src
28264
+ }, "warn");
28265
+ };
28266
+ img.src = src;
28267
+ }
28268
+ setElementPointsMatrix(model, matrix) {
28269
+ const values = [matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty];
28270
+ if (model.root.doc) {
28271
+ model.root.doc.transact(() => {
28272
+ model.root.set(ElementModel.KEYS.pointsMatrix, values);
28273
+ }, elementsUndoOrigin);
28274
+ } else {
28275
+ model.root.set(ElementModel.KEYS.pointsMatrix, values);
28276
+ }
28235
28277
  }
28236
28278
  createCurve() {
28237
28279
  let shadow = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : false;
@@ -33140,9 +33182,14 @@ var WhiteboardApplication = class _WhiteboardApplication extends import_forge_ro
33140
33182
  this.snapshotScope.view.matrix.scale(scale, this.paperScope.project.view.bounds.topLeft);
33141
33183
  this.snapshotScope.view.viewSize = this.paperScope.project.view.viewSize.clone().multiply(scale);
33142
33184
  }
33143
- return new Promise((resolve) => {
33185
+ return new Promise((resolve, reject) => {
33144
33186
  setTimeout(() => {
33145
- resolve(this.snapshotScope.view.element.toDataURL("image/png"));
33187
+ try {
33188
+ const res = this.snapshotScope.view.element.toDataURL("image/png");
33189
+ resolve(res);
33190
+ } catch (error) {
33191
+ reject(error);
33192
+ }
33146
33193
  }, 32);
33147
33194
  });
33148
33195
  }