如何修改litegraph.js节点的输出插槽颜色?
关于LiteGraph.js自定义节点插槽颜色修改的问题
我制作了一个基于LiteGraph.js的测试节点,代码如下:
class CustomConstSpecialNumber { constructor() { this.title = "Const Special"; this.desc = "Constant special"; this.color = '#222234' this.bgcolor = '#333257' this.addOutput("value", "special", {color: "#DDDD"}); this.addProperty("value", 1.0); } onExecute() { this.setOutputData(0, this.properties["value"]); } }
但输出插槽的颜色始终为绿色:
请问是否可以修改插槽的颜色?如果可以,具体该如何操作?
可以修改,问题出在你当前的写法上——LiteGraph.js会优先使用数据类型对应的全局颜色配置,而非addOutput里直接传的color参数。另外你写的#DDDD是不完整的颜色值,需要补全为6位或8位十六进制格式(比如#DDDDDD)。下面提供两种可行方案:
方案1:全局注册自定义数据类型并指定颜色
在定义节点类之后、注册节点类型之前,先给special类型绑定颜色:
// 注册自定义数据类型及对应颜色 LiteGraph.registerType("special", { color: "#DDDDDD" }); // 注册你的自定义节点 LiteGraph.registerNodeType("custom/const_special", CustomConstSpecialNumber);
这样所有使用special类型的插槽都会自动应用这个颜色,适合需要复用该类型的场景。
方案2:在节点类中重写颜色获取方法
如果不想全局修改类型配置,可以单独给这个节点的插槽重写颜色逻辑:
class CustomConstSpecialNumber { constructor() { this.title = "Const Special"; this.desc = "Constant special"; this.color = '#222234' this.bgcolor = '#333257' this.addOutput("value", "special"); this.addProperty("value", 1.0); } onExecute() { this.setOutputData(0, this.properties["value"]); } // 重写输出插槽颜色获取方法 getOutputSlotColor(index) { // 给第0个插槽设置自定义颜色 if (index === 0) { return "#DDDDDD"; } // 其他插槽沿用默认逻辑 return super.getOutputSlotColor(index); } }
这种方式只对当前节点生效,更灵活。
内容的提问来源于stack exchange,提问作者LeMoussel
相关产品推荐
相关产品推荐

