File size: 8,302 Bytes
1e3b872
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// touchdesigner的背景效果,把appinfo的输出,选择一张图片作为背景

window._bg_img = null

/**
 * draws the back canvas (the one containing the background and the connections)
 * @method drawBackCanvas
 **/
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]

  //clear
  if (this.clear_background) {
    ctx.clearRect(viewport[0], viewport[1], viewport[2], viewport[3])
  }

  //show subgraph stack header
  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)
  }

  //reset in case of error
  if (!this.viewport) {
    ctx.restore()
    ctx.setTransform(1, 0, 0, 1, 0, 0)
  }
  this.visible_links.length = 0

  if (this.graph) {
    //apply transformations
    ctx.save()
    this.ds.toCanvasContext(ctx)

    //render BG
    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) {
        //使得 alpha 越接近0时变化越缓慢。
        let alpha = (1.0 - 0.5 / this.ds.scale) * this.editor_alpha
        ctx.globalAlpha = Math.min(Math.max(0, Math.sqrt(alpha)), 1)
        // console.log((1.0 - 0.5 / this.ds.scale) * this.editor_alpha)
      } else {
        ctx.globalAlpha = this.editor_alpha
      }
      ctx.imageSmoothingEnabled = ctx.imageSmoothingEnabled = false // ctx.mozImageSmoothingEnabled =
      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 //= ctx.mozImageSmoothingEnabled
    }

    //groups
    if (this.graph._groups.length && !this.live_mode) {
      this.drawGroups(canvas, ctx)
    }

    if (this.onDrawBackground) {
      this.onDrawBackground(ctx, this.visible_area)
    }
    if (this.onBackgroundRender) {
      //LEGACY
      console.error(
        'WARNING! onBackgroundRender deprecated, now is named onDrawBackground '
      )
      this.onBackgroundRender = null
    }

    //DEBUG: show clipping area
    //ctx.fillStyle = "red";
    //ctx.fillRect( this.visible_area[0] + 10, this.visible_area[1] + 10, this.visible_area[2] - 20, this.visible_area[3] - 20);

    //bg
    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)'
    }

    //draw connections
    if (!this.live_mode) {
      this.drawConnections(ctx)
    }

    ctx.shadowColor = 'rgba(0,0,0,0)'

    //restore state
    ctx.restore()
  }

  if (ctx.finish) {
    ctx.finish()
  }

  this.dirty_bgcanvas = false
  this.dirty_canvas = true //to force to repaint the front canvas with the bgcanvas
}

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) {
  //   const img = new Image()
  //   img.src = 'path/to/your/image.jpg' // 替换为你的图片路径
  // console.log('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])
        // console.log(b)
        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
  }
  // let img=new Image();
  // img.src=BACKGROUND_IMAGE;

  //去掉透明度过度
  // app.canvas.zoom_modify_alpha=false;
  //整体透明度
  app.canvas.editor_alpha = 1.1
  //   app.canvas._pattern=ctx.createPattern(img, "no-repeat");
  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()
  }

  // 启动bg方法每秒运行一次
  start () {
    if (!this.running) {
      this.intervalId = setInterval(() => this.bg(), 1500)
      this.running = true
    }
  }

  // 停止bg方法的运行
  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)
      }
    }
  }

  // 切换start和stop
  toggle () {
    if (this.running) {
      this.stop()
    } else {
      this.start()
    }
  }

  // 获取运行状态
  isRunning () {
    return this.running
  }
}

// 示例用法
// const runner = new BgRunner();
// runner.start();
// setTimeout(() => runner.stop(), 5000);

export const td_bg = new BgRunner()