@canvas-harness/core 0.1.1 → 0.1.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.
package/dist/index.cjs CHANGED
@@ -4871,16 +4871,18 @@ var applyCameraTransform = (surface, camera) => {
4871
4871
  };
4872
4872
  var worldViewport = (surface, camera) => viewportWorldRect(camera, surface.cssWidth, surface.cssHeight);
4873
4873
  var drawWithNodeTransform = (ctx, node, fn) => {
4874
- ctx.save();
4875
4874
  if (node.angle === 0) {
4876
4875
  ctx.translate(node.x, node.y);
4877
- } else {
4878
- const cx = node.x + node.w / 2;
4879
- const cy = node.y + node.h / 2;
4880
- ctx.translate(cx, cy);
4881
- ctx.rotate(node.angle);
4882
- ctx.translate(-node.w / 2, -node.h / 2);
4876
+ fn();
4877
+ ctx.translate(-node.x, -node.y);
4878
+ return;
4883
4879
  }
4880
+ ctx.save();
4881
+ const cx = node.x + node.w / 2;
4882
+ const cy = node.y + node.h / 2;
4883
+ ctx.translate(cx, cy);
4884
+ ctx.rotate(node.angle);
4885
+ ctx.translate(-node.w / 2, -node.h / 2);
4884
4886
  fn();
4885
4887
  ctx.restore();
4886
4888
  };
@@ -4902,6 +4904,12 @@ var createRenderer = (opts) => {
4902
4904
  let interactiveDirty = false;
4903
4905
  let overlaySet = /* @__PURE__ */ new Set();
4904
4906
  let lastDrawn = 0;
4907
+ let sortedNodeIdsCache = null;
4908
+ let sortedEdgeIdsCache = null;
4909
+ const invalidateSortedCaches = () => {
4910
+ sortedNodeIdsCache = null;
4911
+ sortedEdgeIdsCache = null;
4912
+ };
4905
4913
  const requestRepaint = () => {
4906
4914
  staticDirty = true;
4907
4915
  loop.requestFrame();
@@ -5031,7 +5039,9 @@ var createRenderer = (opts) => {
5031
5039
  }
5032
5040
  if (def.renderCanvas) {
5033
5041
  drawWithNodeTransform(staticSurface.ctx, node, () => {
5042
+ staticSurface.ctx.save();
5034
5043
  def.renderCanvas(staticSurface.ctx, node, renderEnv);
5044
+ staticSurface.ctx.restore();
5035
5045
  });
5036
5046
  drawn++;
5037
5047
  }
@@ -5064,11 +5074,19 @@ var createRenderer = (opts) => {
5064
5074
  }
5065
5075
  }
5066
5076
  if (def.drawPlaceholder) {
5067
- drawWithNodeTransform(ctx, node, () => def.drawPlaceholder(ctx, node, env));
5077
+ drawWithNodeTransform(ctx, node, () => {
5078
+ ctx.save();
5079
+ def.drawPlaceholder(ctx, node, env);
5080
+ ctx.restore();
5081
+ });
5068
5082
  return true;
5069
5083
  }
5070
5084
  if (def.renderCanvas) {
5071
- drawWithNodeTransform(ctx, node, () => def.renderCanvas(ctx, node, env));
5085
+ drawWithNodeTransform(ctx, node, () => {
5086
+ ctx.save();
5087
+ def.renderCanvas(ctx, node, env);
5088
+ ctx.restore();
5089
+ });
5072
5090
  return true;
5073
5091
  }
5074
5092
  return false;
@@ -5135,14 +5153,23 @@ var createRenderer = (opts) => {
5135
5153
  isMoving: isMoving2
5136
5154
  });
5137
5155
  };
5156
+ const getSortedEdgeIds = () => {
5157
+ if (sortedEdgeIdsCache) return sortedEdgeIdsCache;
5158
+ const all = store.getAllEdges();
5159
+ sortedEdgeIdsCache = all.slice().sort((a, b) => a.z - b.z || (a.id < b.id ? -1 : 1)).map((e) => e.id);
5160
+ return sortedEdgeIdsCache;
5161
+ };
5138
5162
  const visibleEdges = (viewport) => {
5139
5163
  const ids = store.querySpatial({ rect: viewport }).edges;
5164
+ if (ids.length === 0) return [];
5165
+ const visibleSet = new Set(ids);
5166
+ const sorted = getSortedEdgeIds();
5140
5167
  const result = [];
5141
- for (const id of ids) {
5168
+ for (const id of sorted) {
5169
+ if (!visibleSet.has(id)) continue;
5142
5170
  const e = store.getEdge(id);
5143
5171
  if (e) result.push(e);
5144
5172
  }
5145
- result.sort((a, b) => a.z - b.z || (a.id < b.id ? -1 : 1));
5146
5173
  return result;
5147
5174
  };
5148
5175
  const paintInteractive = () => {
@@ -5290,21 +5317,31 @@ var createRenderer = (opts) => {
5290
5317
  }
5291
5318
  return m;
5292
5319
  };
5320
+ const getSortedNodeIds = () => {
5321
+ if (sortedNodeIdsCache) return sortedNodeIdsCache;
5322
+ const all = store.getAllNodes();
5323
+ sortedNodeIdsCache = all.slice().sort((a, b) => a.z - b.z || (a.id < b.id ? -1 : 1)).map((n) => n.id);
5324
+ return sortedNodeIdsCache;
5325
+ };
5293
5326
  const visibleNodes = (camera, viewport) => {
5294
5327
  const ids = store.querySpatial({ rect: viewport }).nodes;
5328
+ if (ids.length === 0) return [];
5329
+ const visibleSet = new Set(ids);
5330
+ const sorted = getSortedNodeIds();
5295
5331
  const result = [];
5296
5332
  const minWorldSize = MIN_ON_SCREEN_SIZE_PX / camera.z;
5297
- for (const id of ids) {
5333
+ for (const id of sorted) {
5334
+ if (!visibleSet.has(id)) continue;
5298
5335
  const n = store.getNode(id);
5299
5336
  if (!n) continue;
5300
5337
  if (n.w < minWorldSize && n.h < minWorldSize) continue;
5301
5338
  if (intersectsViewport(n, viewport)) result.push(n);
5302
5339
  }
5303
- result.sort((a, b) => a.z - b.z || (a.id < b.id ? -1 : 1));
5304
5340
  return result;
5305
5341
  };
5306
5342
  const loop = createFrameLoop({ draw: drawFrame });
5307
5343
  const onStoreChange = () => {
5344
+ invalidateSortedCaches();
5308
5345
  staticDirty = true;
5309
5346
  interactiveDirty = true;
5310
5347
  loop.requestFrame();