Files
ComfyUI_frontend/src/LGraphIcon.ts
Christian Byrne 7c88bda647 Add optional icon to LGraphBadge and use for API nodes (#930)
Adds option to include an icon when creating an `LGraphBadge`. Then,
adds the icon to the title of nodes whose `constructor.nodeData` has a
non-null `credits_cost` property (API nodes that ComfyUI_frontend was
able to successfully query a credit cost for).


![Selection_1228](https://github.com/user-attachments/assets/9b63c0d1-b25b-4066-858f-7f32488eeb38)


- https://github.com/Comfy-Org/ComfyUI-private/pull/2
- https://github.com/Comfy-Org/litegraph.js/pull/930
- https://github.com/Comfy-Org/ComfyUI_frontend/pull/3470

---------

Co-authored-by: filtered <176114999+webfiltered@users.noreply.github.com>
2025-04-20 10:10:45 +10:00

69 lines
1.6 KiB
TypeScript

export interface LGraphIconOptions {
unicode: string
fontFamily?: string
color?: string
bgColor?: string
fontSize?: number
circlePadding?: number
xOffset?: number
yOffset?: number
}
export class LGraphIcon {
unicode: string
fontFamily: string
color: string
bgColor?: string
fontSize: number
circlePadding: number
xOffset: number
yOffset: number
constructor({
unicode,
fontFamily = "PrimeIcons",
color = "#e6c200",
bgColor,
fontSize = 16,
circlePadding = 2,
xOffset = 0,
yOffset = 0,
}: LGraphIconOptions) {
this.unicode = unicode
this.fontFamily = fontFamily
this.color = color
this.bgColor = bgColor
this.fontSize = fontSize
this.circlePadding = circlePadding
this.xOffset = xOffset
this.yOffset = yOffset
}
draw(ctx: CanvasRenderingContext2D, x: number, y: number) {
x += this.xOffset
y += this.yOffset
const { font, textBaseline, textAlign, fillStyle } = ctx
ctx.font = `${this.fontSize}px '${this.fontFamily}'`
ctx.textBaseline = "middle"
ctx.textAlign = "center"
const iconRadius = this.fontSize / 2 + this.circlePadding
// Draw icon background circle if bgColor is set
if (this.bgColor) {
ctx.beginPath()
ctx.arc(x + iconRadius, y, iconRadius, 0, 2 * Math.PI)
ctx.fillStyle = this.bgColor
ctx.fill()
}
// Draw icon
ctx.fillStyle = this.color
ctx.fillText(this.unicode, x + iconRadius, y)
ctx.font = font
ctx.textBaseline = textBaseline
ctx.textAlign = textAlign
ctx.fillStyle = fillStyle
}
}