import { chainCallback, addMiddleClickPan, addWheelPassthrough } from '../utility.js'; const { app } = window.comfyAPI.app; const { api } = window.comfyAPI.api; const STYLE_ID = "kj-pov-stylesheet"; const _cssUrl = new URL("./preview_override.css", import.meta.url).href; function ensureStyles() { if (document.getElementById(STYLE_ID)) return; const link = document.createElement("link"); link.id = STYLE_ID; link.rel = "stylesheet"; link.href = _cssUrl; document.head.appendChild(link); } // Walks subgraph chain for IDs like "12:7:5". Mirrors getNodeByExecutionId (not exported). function findNodeByQualifiedId(rootGraph, qid) { if (!rootGraph || !qid) return null; const parts = String(qid).split(":"); let graph = rootGraph; for (let i = 0; i < parts.length - 1; i++) { const parentId = parseInt(parts[i], 10); if (!Number.isFinite(parentId)) return null; const parentNode = graph?.getNodeById?.(parentId); if (!parentNode?.subgraph) return null; graph = parentNode.subgraph; } const leafId = parseInt(parts[parts.length - 1], 10); if (!Number.isFinite(leafId)) return null; return graph?.getNodeById?.(leafId) || null; } api.addEventListener("moxie_preview_override", (e) => { const data = e.detail; if (!data || data.node_id == null) return; const node = findNodeByQualifiedId(app.graph, data.node_id); if (node?._kjPreviewHandler) node._kjPreviewHandler(data); }); const GRAPH_PAD_X = 4; function fmt(n, d) { return Number.isFinite(n) ? n.toFixed(d) : "—"; } function el(tag, className, parent) { const e = document.createElement(tag); if (className) e.className = className; if (parent) parent.appendChild(e); return e; } function b64ToBlob(b64, mime) { const bin = atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; i++) arr[i] = bin.charCodeAt(i); return new Blob([arr], { type: mime }); } function syncCanvasDPR(canvas) { const dpr = window.devicePixelRatio || 1; const cssW = canvas.clientWidth || canvas.width; const cssH = canvas.clientHeight || canvas.height; if (canvas.width !== Math.round(cssW * dpr) || canvas.height !== Math.round(cssH * dpr)) { canvas.width = Math.round(cssW * dpr); canvas.height = Math.round(cssH * dpr); } const ctx = canvas.getContext("2d"); ctx.setTransform(dpr, 0, 0, dpr, 0, 0); return { ctx, W: cssW, H: cssH }; } function drawGridlines(ctx, W, H, padX, padY) { ctx.strokeStyle = "#1e1e1e"; ctx.lineWidth = 1; for (let g = 1; g < 4; g++) { const y = Math.round(padY + (g / 4) * (H - 2 * padY)) + 0.5; ctx.beginPath(); ctx.moveTo(padX, y); ctx.lineTo(W - padX, y); ctx.stroke(); } } // σ + Δ overlaid; each series normalised to its own max for comparable shapes. function drawSigmaDeltaGraph(canvas, sigmas, deltas, step, totalSteps, hoverStep, dbCurve, lockedStep) { const { ctx, W, H } = syncCanvasDPR(canvas); const padX = GRAPH_PAD_X, padY = 3; const iW = W - 2 * padX, iH = H - 2 * padY; ctx.clearRect(0, 0, W, H); drawGridlines(ctx, W, H, padX, padY); const n = sigmas?.length || 0; const xSteps = Math.max(totalSteps || n, n, deltas?.length || 0); const xAt = i => padX + (i / Math.max(1, xSteps - 1)) * iW; let sYAt = null; if (n > 1) { let sMax = -Infinity, sMin = Infinity; for (const s of sigmas) { if (s > sMax) sMax = s; if (s < sMin) sMin = s; } if (sMin > 0) sMin = 0; const sRange = Math.max(sMax - sMin, 1e-6); sYAt = v => padY + (1 - (v - sMin) / sRange) * iH; ctx.strokeStyle = "rgba(208, 208, 208, 0.55)"; ctx.lineWidth = 1; ctx.setLineDash([3, 3]); ctx.beginPath(); for (let i = 0; i < n; i++) { const px = padX + (i / (n - 1)) * iW; const py = sYAt(sigmas[i]); if (i === 0) ctx.moveTo(px, py); else ctx.lineTo(px, py); } ctx.stroke(); ctx.setLineDash([]); const i = Math.max(0, Math.min(n - 1, step)); const mx = padX + (i / Math.max(1, n - 1)) * iW; const my = sYAt(sigmas[i]); ctx.fillStyle = "#d0d0d0"; ctx.beginPath(); ctx.arc(mx, my, 2.5, 0, Math.PI * 2); ctx.fill(); } let dYAt = null; if (deltas && deltas.length >= 1) { let dMax = -Infinity; for (const v of deltas) if (Number.isFinite(v) && v > dMax) dMax = v; const dRange = Math.max(dMax, 1e-6); dYAt = v => padY + (1 - v / dRange) * iH; // delta[i] is plotted at boundary (i+1); flat-extend delta[0] back to boundary 0. const lastB = deltas.length; ctx.beginPath(); ctx.moveTo(xAt(0), H - padY); ctx.lineTo(xAt(0), dYAt(deltas[0])); for (let i = 0; i < deltas.length; i++) ctx.lineTo(xAt(i + 1), dYAt(deltas[i])); ctx.lineTo(xAt(lastB), H - padY); ctx.closePath(); ctx.fillStyle = "rgba(230, 126, 34, 0.15)"; ctx.fill(); ctx.strokeStyle = "#e67e22"; ctx.lineWidth = 1.3; if (deltas.length === 1) { ctx.fillStyle = "#e67e22"; ctx.beginPath(); ctx.arc(xAt(1), dYAt(deltas[0]), 2, 0, Math.PI * 2); ctx.fill(); } else { ctx.beginPath(); for (let i = 0; i < deltas.length; i++) { const px = xAt(i + 1), py = dYAt(deltas[i]); if (i === 0) ctx.moveTo(px, py); else ctx.lineTo(px, py); } ctx.stroke(); } } // SamplerDetailBoost curve. `null` entries = outside the gate (drawn as faint baseline), // finite entries = active (drawn as the bright cyan line, auto-scaled to its own peak). if (Array.isArray(dbCurve) && dbCurve.length > 1) { let dbMaxAbs = 0; for (const v of dbCurve) if (Number.isFinite(v) && Math.abs(v) > dbMaxAbs) dbMaxAbs = Math.abs(v); const dbXAt = i => padX + (i / Math.max(1, dbCurve.length - 1)) * iW; const baselineY = H - padY - 0.5; const isActive = v => Number.isFinite(v); ctx.strokeStyle = "rgba(120, 200, 220, 0.25)"; ctx.lineWidth = 1; ctx.beginPath(); let inactiveOpen = false; for (let i = 0; i < dbCurve.length; i++) { if (!isActive(dbCurve[i])) { if (!inactiveOpen) { ctx.moveTo(dbXAt(i), baselineY); inactiveOpen = true; } else ctx.lineTo(dbXAt(i), baselineY); } else { inactiveOpen = false; } } ctx.stroke(); if (dbMaxAbs > 1e-9) { const dbYAt = v => padY + (1 - Math.abs(v) / dbMaxAbs) * iH; ctx.strokeStyle = "rgba(120, 200, 220, 0.85)"; ctx.fillStyle = "rgba(120, 200, 220, 0.85)"; ctx.lineWidth = 1.2; ctx.beginPath(); let segStart = -1; for (let i = 0; i <= dbCurve.length; i++) { const v = i < dbCurve.length ? dbCurve[i] : null; if (isActive(v)) { if (segStart < 0) { segStart = i; ctx.moveTo(dbXAt(i), dbYAt(v)); } else ctx.lineTo(dbXAt(i), dbYAt(v)); } else if (segStart >= 0) { if (i - segStart === 1) { const v0 = dbCurve[segStart]; ctx.fillRect(dbXAt(segStart) - 1, dbYAt(v0) - 1, 2, 2); } segStart = -1; } } ctx.stroke(); } } if (lockedStep != null && lockedStep >= 0 && lockedStep < xSteps) { const lx = xAt(lockedStep) + 0.5; ctx.strokeStyle = "rgba(245, 200, 60, 0.9)"; ctx.lineWidth = 1.2; ctx.setLineDash([4, 2]); ctx.beginPath(); ctx.moveTo(lx, padY); ctx.lineTo(lx, H - padY); ctx.stroke(); ctx.setLineDash([]); } if (hoverStep != null && hoverStep >= 0 && hoverStep < xSteps) { const hx = xAt(hoverStep) + 0.5; ctx.strokeStyle = "rgba(208, 208, 208, 0.5)"; ctx.lineWidth = 1; ctx.beginPath(); ctx.moveTo(hx, padY); ctx.lineTo(hx, H - padY); ctx.stroke(); if (sYAt && hoverStep < n) { ctx.fillStyle = "#d0d0d0"; ctx.beginPath(); ctx.arc(hx - 0.5, sYAt(sigmas[hoverStep]), 2.5, 0, Math.PI * 2); ctx.fill(); } // delta[k-1] is plotted at boundary k. if (dYAt && hoverStep >= 1 && (hoverStep - 1) < deltas.length) { ctx.fillStyle = "#e67e22"; ctx.beginPath(); ctx.arc(hx - 0.5, dYAt(deltas[hoverStep - 1]), 2.5, 0, Math.PI * 2); ctx.fill(); } } } // totalSteps fixes the x-axis so the line grows left-to-right, not stretching to fill. function drawLineGraph(canvas, values, totalSteps) { const { ctx, W, H } = syncCanvasDPR(canvas); const padX = GRAPH_PAD_X, padY = 3; const iW = W - 2 * padX, iH = H - 2 * padY; ctx.clearRect(0, 0, W, H); drawGridlines(ctx, W, H, padX, padY); if (!values || values.length < 1) return; let vMax = -Infinity, vMin = Infinity; for (const v of values) { if (v > vMax) vMax = v; if (v < vMin) vMin = v; } if (vMin > 0) vMin = 0; const vRange = Math.max(vMax - vMin, 1e-6); const xSteps = Math.max(totalSteps || values.length, values.length); ctx.beginPath(); ctx.moveTo(padX, H - padY); for (let i = 0; i < values.length; i++) { const px = padX + (i / Math.max(1, xSteps - 1)) * iW; const py = padY + (1 - (values[i] - vMin) / vRange) * iH; ctx.lineTo(px, py); } ctx.lineTo(padX + ((values.length - 1) / Math.max(1, xSteps - 1)) * iW, H - padY); ctx.closePath(); ctx.fillStyle = "rgba(230, 126, 34, 0.15)"; ctx.fill(); ctx.strokeStyle = "#e67e22"; ctx.lineWidth = 1.3; ctx.beginPath(); for (let i = 0; i < values.length; i++) { const px = padX + (i / Math.max(1, xSteps - 1)) * iW; const py = padY + (1 - (values[i] - vMin) / vRange) * iH; if (i === 0) ctx.moveTo(px, py); else ctx.lineTo(px, py); } ctx.stroke(); } app.registerExtension({ name: "Moxie.PreviewOverride", async beforeRegisterNodeDef(nodeType, nodeData) { if (nodeData?.name !== "MoxiePreviewOverride") return; chainCallback(nodeType.prototype, "onNodeCreated", function () { ensureStyles(); const node = this; const root = el("div", "kj-pov-root"); const imageArea = el("div", "kj-pov-image-area", root); // Double-buffered: decode() on the visible-to-be element so the bitmap is reused. const imgA = el("img", "kj-pov-img", imageArea); const imgB = el("img", "kj-pov-img", imageArea); imgA.draggable = false; imgB.draggable = false; imgB.style.opacity = "0"; let visibleImg = imgA; let pendingImg = imgB; const img = imgA; // alias used by class-toggle code; applied to both buffers // WebP path: ImageDecoder → VideoFrame[] → canvas, driven by a global timer. const videoCanvas = el("canvas", "kj-pov-img", imageArea); videoCanvas.style.opacity = "0"; const videoCtx = videoCanvas.getContext("2d"); // MP4 path: double-buffered