| 28 | } |
| 29 | |
| 30 | function createWidget(node, app) { |
| 31 | const widget = { |
| 32 | type: "StableFast3DViewer", |
| 33 | name: "preview", |
| 34 | callback: () => { }, |
| 35 | draw: function (ctx, node, widgetWidth, widgetY, widgetHeight) { |
| 36 | const margin = 10; |
| 37 | const top_offset = 5; |
| 38 | const visible = app.canvas.ds.scale > 0.5; |
| 39 | const w = widgetWidth - margin * 4; |
| 40 | const clientRectBound = ctx.canvas.getBoundingClientRect(); |
| 41 | const transform = new DOMMatrix() |
| 42 | .scaleSelf( |
| 43 | clientRectBound.width / ctx.canvas.width, |
| 44 | clientRectBound.height / ctx.canvas.height |
| 45 | ) |
| 46 | .multiplySelf(ctx.getTransform()) |
| 47 | .translateSelf(margin, margin + widgetY); |
| 48 | |
| 49 | Object.assign(this.visualizer.style, { |
| 50 | left: `${transform.a * margin + transform.e}px`, |
| 51 | top: `${transform.d + transform.f + top_offset}px`, |
| 52 | width: `${(w * transform.a)}px`, |
| 53 | height: `${(w * transform.d - widgetHeight - (margin * 15) * transform.d)}px`, |
| 54 | position: "absolute", |
| 55 | overflow: "hidden", |
| 56 | zIndex: app.graph._nodes.indexOf(node), |
| 57 | }); |
| 58 | |
| 59 | Object.assign(this.visualizer.children[0].style, { |
| 60 | transformOrigin: "50% 50%", |
| 61 | width: '100%', |
| 62 | height: '100%', |
| 63 | border: '0 none', |
| 64 | }); |
| 65 | |
| 66 | this.visualizer.hidden = !visible; |
| 67 | }, |
| 68 | }; |
| 69 | |
| 70 | const container = document.createElement('div'); |
| 71 | |
| 72 | node.visualizer = new Visualizer(node, container); |
| 73 | widget.visualizer = container; |
| 74 | widget.parent = node; |
| 75 | document.body.appendChild(widget.visualizer); |
| 76 | node.addCustomWidget(widget); |
| 77 | |
| 78 | node.onDrawBackground = (ctx) => { |
| 79 | node.visualizer.iframe.hidden = this.flags.collapsed; |
| 80 | }; |
| 81 | |
| 82 | node.onRemoved = () => { |
| 83 | for (let w in node.widgets) { |
| 84 | if (node.widgets[w].visualizer) { |
| 85 | node.widgets[w].visualizer.remove(); |
| 86 | } |
| 87 | } |