* Creates a new instance of LGraphCanvas. * @param canvas The canvas HTML element (or its id) to use, or null / undefined to leave blank. * @param graph The graph that owns this canvas. * @param options
(
canvas: HTMLCanvasElement,
graph: LGraph,
options?: LGraphCanvas["options"],
)
| 663 | * @param options |
| 664 | */ |
| 665 | constructor( |
| 666 | canvas: HTMLCanvasElement, |
| 667 | graph: LGraph, |
| 668 | options?: LGraphCanvas["options"], |
| 669 | ) { |
| 670 | options ||= {} |
| 671 | this.options = options |
| 672 | |
| 673 | // if(graph === undefined) |
| 674 | // throw ("No graph assigned"); |
| 675 | this.background_image = LGraphCanvas.DEFAULT_BACKGROUND_IMAGE |
| 676 | |
| 677 | this.ds = new DragAndScale(canvas) |
| 678 | this.pointer = new CanvasPointer(canvas) |
| 679 | |
| 680 | this.linkConnector.events.addEventListener("link-created", () => this.#dirty()) |
| 681 | |
| 682 | // @deprecated Workaround: Keep until connecting_links is removed. |
| 683 | this.linkConnector.events.addEventListener("reset", () => { |
| 684 | this.connecting_links = null |
| 685 | this.dirty_bgcanvas = true |
| 686 | }) |
| 687 | |
| 688 | // Dropped a link on the canvas |
| 689 | this.linkConnector.events.addEventListener("dropped-on-canvas", (customEvent) => { |
| 690 | if (!this.connecting_links) return |
| 691 | |
| 692 | const e = customEvent.detail |
| 693 | this.emitEvent({ |
| 694 | subType: "empty-release", |
| 695 | originalEvent: e, |
| 696 | linkReleaseContext: { links: this.connecting_links }, |
| 697 | }) |
| 698 | |
| 699 | const firstLink = this.linkConnector.renderLinks[0] |
| 700 | |
| 701 | // No longer in use |
| 702 | // add menu when releasing link in empty space |
| 703 | if (LiteGraph.release_link_on_empty_shows_menu) { |
| 704 | const linkReleaseContext = this.linkConnector.state.connectingTo === "input" |
| 705 | ? { |
| 706 | node_from: firstLink.node as LGraphNode, |
| 707 | slot_from: firstLink.fromSlot as INodeOutputSlot, |
| 708 | type_filter_in: firstLink.fromSlot.type, |
| 709 | } |
| 710 | : { |
| 711 | node_to: firstLink.node as LGraphNode, |
| 712 | slot_to: firstLink.fromSlot as INodeInputSlot, |
| 713 | type_filter_out: firstLink.fromSlot.type, |
| 714 | } |
| 715 | |
| 716 | const afterRerouteId = firstLink.fromReroute?.id |
| 717 | |
| 718 | if ("shiftKey" in e && e.shiftKey) { |
| 719 | if (this.allow_searchbox) { |
| 720 | this.showSearchBox(e as unknown as MouseEvent, linkReleaseContext as IShowSearchOptions) |
| 721 | } |
| 722 | } else if (this.linkConnector.state.connectingTo === "input") { |
nothing calls this directly
no test coverage detected