| |
|
|
| window._bg_img = null |
|
|
| |
| |
| |
| |
| LGraphCanvas.prototype.drawBackCanvas = function () { |
| var canvas = this.bgcanvas |
| if ( |
| canvas.width != this.canvas.width || |
| canvas.height != this.canvas.height |
| ) { |
| canvas.width = this.canvas.width |
| canvas.height = this.canvas.height |
| } |
|
|
| if (!this.bgctx) { |
| this.bgctx = this.bgcanvas.getContext('2d') |
| } |
| var ctx = this.bgctx |
| if (ctx.start) { |
| ctx.start() |
| } |
|
|
| var viewport = this.viewport || [0, 0, ctx.canvas.width, ctx.canvas.height] |
|
|
| |
| if (this.clear_background) { |
| ctx.clearRect(viewport[0], viewport[1], viewport[2], viewport[3]) |
| } |
|
|
| |
| if (this._graph_stack && this._graph_stack.length) { |
| ctx.save() |
| var parent_graph = this._graph_stack[this._graph_stack.length - 1] |
| var subgraph_node = this.graph._subgraph_node |
| ctx.strokeStyle = subgraph_node.bgcolor |
| ctx.lineWidth = 10 |
| ctx.strokeRect(1, 1, canvas.width - 2, canvas.height - 2) |
| ctx.lineWidth = 1 |
| ctx.font = '40px Arial' |
| ctx.textAlign = 'center' |
| ctx.fillStyle = subgraph_node.bgcolor || '#AAA' |
| var title = '' |
| for (var i = 1; i < this._graph_stack.length; ++i) { |
| title += this._graph_stack[i]._subgraph_node.getTitle() + ' >> ' |
| } |
| ctx.fillText(title + subgraph_node.getTitle(), canvas.width * 0.5, 40) |
| ctx.restore() |
| } |
|
|
| var bg_already_painted = false |
| if (this.onRenderBackground) { |
| bg_already_painted = this.onRenderBackground(canvas, ctx) |
| } |
|
|
| |
| if (!this.viewport) { |
| ctx.restore() |
| ctx.setTransform(1, 0, 0, 1, 0, 0) |
| } |
| this.visible_links.length = 0 |
|
|
| if (this.graph) { |
| |
| ctx.save() |
| this.ds.toCanvasContext(ctx) |
|
|
| |
| if ( |
| this.ds.scale < 1 && |
| !bg_already_painted && |
| this.clear_background_color |
| ) { |
| ctx.fillStyle = this.clear_background_color |
| ctx.fillRect( |
| this.visible_area[0], |
| this.visible_area[1], |
| this.visible_area[2], |
| this.visible_area[3] |
| ) |
| } |
|
|
| |
| if (this.background_image && this.ds.scale > 0.5 && !bg_already_painted) { |
| if (this.zoom_modify_alpha) { |
| |
| let alpha = (1.0 - 0.5 / this.ds.scale) * this.editor_alpha |
| ctx.globalAlpha = Math.min(Math.max(0, Math.sqrt(alpha)), 1) |
| |
| } else { |
| ctx.globalAlpha = this.editor_alpha |
| } |
| ctx.imageSmoothingEnabled = ctx.imageSmoothingEnabled = false |
| if (!this._bg_img || this._bg_img.name != this.background_image) { |
| this._bg_img = new Image() |
| this._bg_img.name = this.background_image |
| this._bg_img.src = this.background_image |
| var that = this |
| this._bg_img.onload = function () { |
| that.draw(true, true) |
| } |
| } |
|
|
| var pattern = null |
| if (this._pattern == null && this._bg_img.width > 0) { |
| pattern = ctx.createPattern(this._bg_img, 'repeat') |
| this._pattern_img = this._bg_img |
| this._pattern = pattern |
| } else { |
| pattern = this._pattern |
| } |
|
|
| if (pattern) { |
| ctx.fillStyle = pattern |
| ctx.fillRect( |
| this.visible_area[0], |
| this.visible_area[1], |
| this.visible_area[2], |
| this.visible_area[3] |
| ) |
| ctx.fillStyle = 'transparent' |
| } |
|
|
| ctx.globalAlpha = 1.0 |
| ctx.imageSmoothingEnabled = ctx.imageSmoothingEnabled = true |
| } |
|
|
| |
| if (this.graph._groups.length && !this.live_mode) { |
| this.drawGroups(canvas, ctx) |
| } |
|
|
| if (this.onDrawBackground) { |
| this.onDrawBackground(ctx, this.visible_area) |
| } |
| if (this.onBackgroundRender) { |
| |
| console.error( |
| 'WARNING! onBackgroundRender deprecated, now is named onDrawBackground ' |
| ) |
| this.onBackgroundRender = null |
| } |
|
|
| |
| |
| |
|
|
| |
| if (this.render_canvas_border) { |
| ctx.strokeStyle = '#235' |
| ctx.strokeRect(0, 0, canvas.width, canvas.height) |
| } |
|
|
| if (this.render_connections_shadows) { |
| ctx.shadowColor = '#000' |
| ctx.shadowOffsetX = 0 |
| ctx.shadowOffsetY = 0 |
| ctx.shadowBlur = 6 |
| } else { |
| ctx.shadowColor = 'rgba(0,0,0,0)' |
| } |
|
|
| |
| if (!this.live_mode) { |
| this.drawConnections(ctx) |
| } |
|
|
| ctx.shadowColor = 'rgba(0,0,0,0)' |
|
|
| |
| ctx.restore() |
| } |
|
|
| if (ctx.finish) { |
| ctx.finish() |
| } |
|
|
| this.dirty_bgcanvas = false |
| this.dirty_canvas = true |
| } |
|
|
| function imgToCanvasBase64 (img) { |
| const canvas = document.createElement('canvas') |
| const ctx = canvas.getContext('2d') |
| canvas.width = img.width |
| canvas.height = img.height |
| ctx.drawImage(img, 0, 0) |
| const base64 = canvas.toDataURL('image/png') |
|
|
| return base64 |
| } |
|
|
| |
| function convertImageToBase64 (img) { |
| |
| |
| |
| try { |
| const base64 = imgToCanvasBase64(img) |
| return base64 |
| } catch (error) { |
| console.error(error) |
| } |
| } |
|
|
| function getInputsAndOutputs () { |
| const outputs = |
| `PreviewImage,SaveImage,TransparentImage,VHS_VideoCombine,VideoCombine_Adv,Image Save,SaveImageAndMetadata_`.split( |
| ',' |
| ) |
|
|
| let outputsId = [] |
|
|
| for (let node of app.graph._nodes) { |
| if (outputs.includes(node.type)) { |
| outputsId.push(node.id) |
| } |
| } |
|
|
| return outputsId |
| } |
|
|
| function getRandomElement (arr) { |
| const randomIndex = Math.floor(Math.random() * arr.length) |
| return arr[randomIndex] |
| } |
|
|
| async function getBG () { |
| var outputs = [] |
|
|
| for (let id of app.graph |
| .getNodeById(50) |
| .widgets.filter(w => w.name === 'output_ids')[0] |
| .value.split('\n')) { |
| if (getInputsAndOutputs().map(Number).includes(Number(id))) { |
| if (app.graph.getNodeById(id).imgs && app.graph.getNodeById(id).imgs[0]) { |
| let b = convertImageToBase64(app.graph.getNodeById(id).imgs[0]) |
| |
| outputs.push(b) |
| } |
| } |
| } |
|
|
| var BACKGROUND_IMAGE = getRandomElement(outputs), |
| CLEAR_BACKGROUND_COLOR = 'rgba(0,0,0,0.9)' |
|
|
| if (!window._bg_img) { |
| window._bg_img = app.canvas._bg_img.src |
| } |
| |
| |
|
|
| |
| |
| |
| app.canvas.editor_alpha = 1.1 |
| |
| app.canvas.updateBackground(BACKGROUND_IMAGE, CLEAR_BACKGROUND_COLOR) |
| app.canvas.draw(true, true) |
| } |
|
|
| class BgRunner { |
| constructor () { |
| this.intervalId = null |
| this.running = false |
| } |
|
|
| |
| bg () { |
| console.log('方法bg正在运行') |
| getBG() |
| } |
|
|
| |
| start () { |
| if (!this.running) { |
| this.intervalId = setInterval(() => this.bg(), 1500) |
| this.running = true |
| } |
| } |
|
|
| |
| stop () { |
| if (this.running) { |
| clearInterval(this.intervalId) |
| this.intervalId = null |
| this.running = false |
|
|
| if (window._bg_img) { |
| var BACKGROUND_IMAGE = window._bg_img, |
| CLEAR_BACKGROUND_COLOR = 'rgba(0,0,0,1)' |
| app.canvas.editor_alpha = 1 |
|
|
| app.canvas.updateBackground(BACKGROUND_IMAGE, CLEAR_BACKGROUND_COLOR) |
| app.canvas.draw(true, true) |
| } |
| } |
| } |
|
|
| |
| toggle () { |
| if (this.running) { |
| this.stop() |
| } else { |
| this.start() |
| } |
| } |
|
|
| |
| isRunning () { |
| return this.running |
| } |
| } |
|
|
| |
| |
| |
| |
|
|
| export const td_bg = new BgRunner() |
|
|