MCPcopy Create free account
hub / github.com/cifertech/DisplayKit / ensureRulers

Function ensureRulers

app.js:294–392  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

292}
293
294function ensureRulers() {
295 const wrapper = document.querySelector(".preview-wrapper");
296 const frame = document.querySelector(".preview-frame");
297 if (!wrapper || !frame) return;
298 // Rulers sit in wrapper coordinates (not scaled with previewViewport transform).
299 wrapper.style.position = wrapper.style.position || "relative";
300
301 if (!rulerCorner) {
302 rulerCorner = document.createElement("div");
303 rulerCorner.className = "ruler-corner";
304 wrapper.appendChild(rulerCorner);
305 }
306 if (!rulerX) {
307 rulerX = document.createElement("div");
308 rulerX.className = "ruler-x";
309 rulerCanvasX = document.createElement("canvas");
310 rulerCanvasX.className = "ruler-canvas";
311 rulerX.appendChild(rulerCanvasX);
312 wrapper.appendChild(rulerX);
313 }
314 if (!rulerY) {
315 rulerY = document.createElement("div");
316 rulerY.className = "ruler-y";
317 rulerCanvasY = document.createElement("canvas");
318 rulerCanvasY.className = "ruler-canvas";
319 rulerY.appendChild(rulerCanvasY);
320 wrapper.appendChild(rulerY);
321 }
322
323 // Size rulers to match the preview size on screen.
324 const previewRect = preview.getBoundingClientRect();
325 const wrapperRect = wrapper.getBoundingClientRect();
326 const w = Math.max(0, Math.round(previewRect.width));
327 const h = Math.max(0, Math.round(previewRect.height));
328 const left = Math.round(previewRect.left - wrapperRect.left);
329 const top = Math.round(previewRect.top - wrapperRect.top);
330
331 rulerCorner.style.left = (left - 28) + "px";
332 rulerCorner.style.top = (top - 28) + "px";
333 rulerX.style.left = left + "px";
334 rulerX.style.top = (top - 28) + "px";
335 rulerX.style.width = w + "px";
336 rulerY.style.left = (left - 28) + "px";
337 rulerY.style.top = top + "px";
338 rulerY.style.height = h + "px";
339
340 // Canvas pixel ratio
341 const dpr = window.devicePixelRatio || 1;
342 rulerCanvasX.width = Math.max(1, Math.floor(w * dpr));
343 rulerCanvasX.height = Math.max(1, Math.floor(28 * dpr));
344 rulerCanvasX.style.width = w + "px";
345 rulerCanvasX.style.height = "28px";
346
347 rulerCanvasY.width = Math.max(1, Math.floor(28 * dpr));
348 rulerCanvasY.height = Math.max(1, Math.floor(h * dpr));
349 rulerCanvasY.style.width = "28px";
350 rulerCanvasY.style.height = h + "px";
351

Callers 2

applyViewportTransformFunction · 0.85
updatePreviewSizeFunction · 0.85

Calls 1

clampFunction · 0.70

Tested by

no test coverage detected