Skip to content

Commit 8e8eeb4

Browse files
Add background image setting for canvas
1 parent b6cf977 commit 8e8eeb4

File tree

5 files changed

+46
-24
lines changed

5 files changed

+46
-24
lines changed

src/components/graph/GraphCanvas.vue

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,20 @@ watch(
168168
await colorPaletteService.loadColorPalette(currentPaletteId)
169169
}
170170
)
171+
172+
watch(
173+
() => settingStore.get('Comfy.Canvas.BackgroundImage'),
174+
async () => {
175+
if (!canvasStore.canvas) return
176+
const currentPaletteId = colorPaletteStore.activePaletteId
177+
if (!currentPaletteId) return
178+
179+
// Reload color palette to apply background image
180+
await colorPaletteService.loadColorPalette(currentPaletteId)
181+
// Mark background canvas as dirty
182+
canvasStore.canvas.setDirty(false, true)
183+
}
184+
)
171185
watch(
172186
() => colorPaletteStore.activePaletteId,
173187
async (newValue) => {

src/components/sidebar/tabs/QueueSidebarTab.vue

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@
9292
</template>
9393

9494
<script setup lang="ts">
95-
import { truncate } from 'lodash'
9695
import Button from 'primevue/button'
9796
import ConfirmPopup from 'primevue/confirmpopup'
9897
import ContextMenu from 'primevue/contextmenu'
@@ -108,7 +107,6 @@ import VirtualGrid from '@/components/common/VirtualGrid.vue'
108107
import { ComfyNode } from '@/schemas/comfyWorkflowSchema'
109108
import { api } from '@/scripts/api'
110109
import { app } from '@/scripts/app'
111-
import { useColorPaletteService } from '@/services/colorPaletteService'
112110
import { useLitegraphService } from '@/services/litegraphService'
113111
import { useCommandStore } from '@/stores/commandStore'
114112
import {
@@ -117,7 +115,6 @@ import {
117115
useQueueStore
118116
} from '@/stores/queueStore'
119117
import { useSettingStore } from '@/stores/settingStore'
120-
import { getPathDetails } from '@/utils/formatUtil'
121118
122119
import SidebarTabTemplate from './SidebarTabTemplate.vue'
123120
import ResultGallery from './queue/ResultGallery.vue'
@@ -224,29 +221,13 @@ const menuItems = computed<MenuItem[]>(() => [
224221
visible: !!menuTargetNode.value
225222
},
226223
{
227-
label: t('Set as background'),
224+
label: t('g.setAsBackground'),
228225
icon: 'pi pi-image',
229226
command: () => {
230227
const url = menuTargetTask.value?.previewOutput?.url
231-
const filepath = menuTargetTask.value?.previewOutput?.filename
232-
if (!url || !filepath) return
233-
234-
const { forkCurrentColorPalette, addCustomColorPalette } =
235-
useColorPaletteService()
236-
237-
const palettePatch = {
238-
colors: {
239-
comfy_base: { 'bg-img': `url('${url}') no-repeat center /cover` },
240-
litegraph_base: { CLEAR_BACKGROUND_COLOR: 'transparent' }
241-
}
228+
if (url) {
229+
void settingStore.set('Comfy.Canvas.BackgroundImage', url)
242230
}
243-
244-
const { filename } = getPathDetails(filepath)
245-
const forkedPalette = forkCurrentColorPalette(palettePatch, {
246-
name: `${t('g.customBackground')} - ${truncate(filename, { length: 32 })}`
247-
})
248-
249-
addCustomColorPalette(forkedPalette)
250231
}
251232
}
252233
])

src/constants/coreSettings.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,20 @@ export const CORE_SETTINGS: SettingParams[] = [
818818
defaultValue: false,
819819
versionAdded: '1.18.0'
820820
},
821+
{
822+
id: 'Comfy.Canvas.BackgroundImage',
823+
category: ['Appearance', 'Canvas', 'Background'],
824+
name: 'Canvas background image',
825+
type: 'text',
826+
tooltip:
827+
'Image URL for the canvas background. You can right-click an image in the outputs panel and select "Set as Background" to use it.',
828+
defaultValue: '',
829+
versionAdded: '1.20.4',
830+
attrs: {
831+
inputmode: 'url',
832+
placeholder: 'https://example.com/image.png'
833+
}
834+
},
821835
{
822836
id: 'LiteGraph.Pointer.TrackpadGestures',
823837
category: ['LiteGraph', 'Pointer', 'Trackpad Gestures'],

src/schemas/apiSchema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ const zNodeBadgeMode = z.enum(
350350
const zSettings = z.object({
351351
'Comfy.ColorPalette': z.string(),
352352
'Comfy.CustomColorPalettes': colorPalettesSchema,
353+
'Comfy.Canvas.BackgroundImage': z.string().optional(),
353354
'Comfy.ConfirmClear': z.boolean(),
354355
'Comfy.DevMode': z.boolean(),
355356
'Comfy.Workflow.ShowMissingNodesWarning': z.boolean(),

src/services/colorPaletteService.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,13 @@ export const useColorPaletteService = () => {
132132
// Sets the colors of the LiteGraph objects
133133
app.canvas.node_title_color = palette.NODE_TITLE_COLOR
134134
app.canvas.default_link_color = palette.LINK_COLOR
135-
app.canvas.background_image = palette.BACKGROUND_IMAGE
136-
app.canvas.clear_background_color = palette.CLEAR_BACKGROUND_COLOR
135+
const backgroundImage = settingStore.get('Comfy.Canvas.BackgroundImage')
136+
if (backgroundImage) {
137+
app.canvas.clear_background_color = 'transparent'
138+
} else {
139+
app.canvas.background_image = palette.BACKGROUND_IMAGE
140+
app.canvas.clear_background_color = palette.CLEAR_BACKGROUND_COLOR
141+
}
137142
app.canvas._pattern = undefined
138143

139144
for (const [key, value] of Object.entries(palette)) {
@@ -165,6 +170,13 @@ export const useColorPaletteService = () => {
165170
for (const [key, value] of Object.entries(comfyColorPalette)) {
166171
rootStyle.setProperty('--' + key, value)
167172
}
173+
const backgroundImage = settingStore.get('Comfy.Canvas.BackgroundImage')
174+
if (backgroundImage) {
175+
rootStyle.setProperty(
176+
'--bg-img',
177+
`url('${backgroundImage}') no-repeat center /cover`
178+
)
179+
}
168180
}
169181
}
170182

0 commit comments

Comments
 (0)