Files
ComfyUI_frontend/src/LGraphBadge.ts
filtered 91077aa20b Redesign invalid node indicator (#358)
* nit

* Remove unused code

Gradient can (and should) be impl. directly by caching a CanvasGradient.

* nit

* nit - Refactor

* Remove redundant code

* Add line width & colour options to shape stroke

* Rename drawSelectionBounding to strokeShape

* nit - Doc

* Fix rounded corners not scaling with padding

* Optimise node badge draw

* Redesign invalid node visual indication

Customisable boundary indicator now used, replacing red background.

* Update snapshot

---------

Co-authored-by: huchenlei <huchenlei@proton.me>
2024-11-29 16:26:25 -05:00

91 lines
1.9 KiB
TypeScript

export enum BadgePosition {
TopLeft = "top-left",
TopRight = "top-right",
}
export interface LGraphBadgeOptions {
text: string
fgColor?: string
bgColor?: string
fontSize?: number
padding?: number
height?: number
cornerRadius?: number
}
export class LGraphBadge {
text: string
fgColor: string
bgColor: string
fontSize: number
padding: number
height: number
cornerRadius: number
constructor({
text,
fgColor = "white",
bgColor = "#0F1F0F",
fontSize = 12,
padding = 6,
height = 20,
cornerRadius = 5,
}: LGraphBadgeOptions) {
this.text = text
this.fgColor = fgColor
this.bgColor = bgColor
this.fontSize = fontSize
this.padding = padding
this.height = height
this.cornerRadius = cornerRadius
}
get visible() {
return this.text.length > 0
}
getWidth(ctx: CanvasRenderingContext2D) {
if (!this.visible) return 0
const { font } = ctx
ctx.font = `${this.fontSize}px sans-serif`
const textWidth = ctx.measureText(this.text).width
ctx.font = font
return textWidth + this.padding * 2
}
draw(
ctx: CanvasRenderingContext2D,
x: number,
y: number,
): void {
if (!this.visible) return
const { fillStyle } = ctx
ctx.font = `${this.fontSize}px sans-serif`
const badgeWidth = this.getWidth(ctx)
const badgeX = 0
// Draw badge background
ctx.fillStyle = this.bgColor
ctx.beginPath()
if (ctx.roundRect) {
ctx.roundRect(x + badgeX, y, badgeWidth, this.height, this.cornerRadius)
} else {
// Fallback for browsers that don't support roundRect
ctx.rect(x + badgeX, y, badgeWidth, this.height)
}
ctx.fill()
// Draw badge text
ctx.fillStyle = this.fgColor
ctx.fillText(
this.text,
x + badgeX + this.padding,
y + this.height - this.padding,
)
ctx.fillStyle = fillStyle
}
}