mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-05-01 11:42:06 +00:00
CurveEditor ES6 class conversion (#162)
This commit is contained in:
@@ -14084,8 +14084,8 @@ const globalExport = {};
|
||||
};
|
||||
|
||||
//used by some widgets to render a curve editor
|
||||
function CurveEditor( points )
|
||||
{
|
||||
class CurveEditor {
|
||||
constructor(points) {
|
||||
this.points = points;
|
||||
this.selected = -1;
|
||||
this.nearest = -1;
|
||||
@@ -14094,12 +14094,10 @@ const globalExport = {};
|
||||
this.margin = 5;
|
||||
}
|
||||
|
||||
CurveEditor.sampleCurve = function(f,points)
|
||||
{
|
||||
static sampleCurve(f, points) {
|
||||
if (!points)
|
||||
return;
|
||||
for(var i = 0; i < points.length - 1; ++i)
|
||||
{
|
||||
for (var i = 0; i < points.length - 1; ++i) {
|
||||
var p = points[i];
|
||||
var pn = points[i + 1];
|
||||
if (pn[0] < f)
|
||||
@@ -14113,8 +14111,7 @@ const globalExport = {};
|
||||
return 0;
|
||||
}
|
||||
|
||||
CurveEditor.prototype.draw = function( ctx, size, graphcanvas, background_color, line_color, inactive )
|
||||
{
|
||||
draw(ctx, size, graphcanvas, background_color, line_color, inactive) {
|
||||
var points = this.points;
|
||||
if (!points)
|
||||
return;
|
||||
@@ -14127,8 +14124,7 @@ const globalExport = {};
|
||||
ctx.save();
|
||||
ctx.translate(this.margin, this.margin);
|
||||
|
||||
if(background_color)
|
||||
{
|
||||
if (background_color) {
|
||||
ctx.fillStyle = "#111";
|
||||
ctx.fillRect(0, 0, w, h);
|
||||
ctx.fillStyle = "#222";
|
||||
@@ -14140,16 +14136,14 @@ const globalExport = {};
|
||||
if (inactive)
|
||||
ctx.globalAlpha = 0.5;
|
||||
ctx.beginPath();
|
||||
for(var i = 0; i < points.length; ++i)
|
||||
{
|
||||
for (var i = 0; i < points.length; ++i) {
|
||||
var p = points[i];
|
||||
ctx.lineTo(p[0] * w, (1.0 - p[1]) * h);
|
||||
}
|
||||
ctx.stroke();
|
||||
ctx.globalAlpha = 1;
|
||||
if (!inactive)
|
||||
for(var i = 0; i < points.length; ++i)
|
||||
{
|
||||
for (var i = 0; i < points.length; ++i) {
|
||||
var p = points[i];
|
||||
ctx.fillStyle = this.selected == i ? "#FFF" : (this.nearest == i ? "#DDD" : "#AAA");
|
||||
ctx.beginPath();
|
||||
@@ -14160,8 +14154,7 @@ const globalExport = {};
|
||||
}
|
||||
|
||||
//localpos is mouse in curve editor space
|
||||
CurveEditor.prototype.onMouseDown = function( localpos, graphcanvas )
|
||||
{
|
||||
onMouseDown(localpos, graphcanvas) {
|
||||
var points = this.points;
|
||||
if (!points)
|
||||
return;
|
||||
@@ -14178,8 +14171,7 @@ const globalExport = {};
|
||||
//search closer one
|
||||
this.selected = this.getCloserPoint(pos, max_dist);
|
||||
//create one
|
||||
if(this.selected == -1)
|
||||
{
|
||||
if (this.selected == -1) {
|
||||
var point = [x / w, 1 - y / h];
|
||||
points.push(point);
|
||||
points.sort(function (a, b) { return a[0] - b[0]; });
|
||||
@@ -14190,8 +14182,7 @@ const globalExport = {};
|
||||
return true;
|
||||
}
|
||||
|
||||
CurveEditor.prototype.onMouseMove = function( localpos, graphcanvas )
|
||||
{
|
||||
onMouseMove(localpos, graphcanvas) {
|
||||
var points = this.points;
|
||||
if (!points)
|
||||
return;
|
||||
@@ -14204,17 +14195,16 @@ const globalExport = {};
|
||||
var max_dist = 30 / graphcanvas.ds.scale;
|
||||
this._nearest = this.getCloserPoint(curvepos, max_dist);
|
||||
var point = points[s];
|
||||
if(point)
|
||||
{
|
||||
if (point) {
|
||||
var is_edge_point = s == 0 || s == points.length - 1;
|
||||
if( !is_edge_point && (localpos[0] < -10 || localpos[0] > this.size[0] + 10 || localpos[1] < -10 || localpos[1] > this.size[1] + 10) )
|
||||
{
|
||||
if (!is_edge_point && (localpos[0] < -10 || localpos[0] > this.size[0] + 10 || localpos[1] < -10 || localpos[1] > this.size[1] + 10)) {
|
||||
points.splice(s, 1);
|
||||
this.selected = -1;
|
||||
return;
|
||||
}
|
||||
if (!is_edge_point) //not edges
|
||||
point[0] = clamp(x, 0, 1);
|
||||
|
||||
else
|
||||
point[0] = s == 0 ? 0 : 1;
|
||||
point[1] = 1.0 - clamp(y, 0, 1);
|
||||
@@ -14224,14 +14214,12 @@ const globalExport = {};
|
||||
}
|
||||
}
|
||||
|
||||
CurveEditor.prototype.onMouseUp = function( localpos, graphcanvas )
|
||||
{
|
||||
onMouseUp(localpos, graphcanvas) {
|
||||
this.selected = -1;
|
||||
return false;
|
||||
}
|
||||
|
||||
CurveEditor.prototype.getCloserPoint = function(pos, max_dist)
|
||||
{
|
||||
getCloserPoint(pos, max_dist) {
|
||||
var points = this.points;
|
||||
if (!points)
|
||||
return -1;
|
||||
@@ -14243,8 +14231,7 @@ const globalExport = {};
|
||||
var min_dist = 1000000;
|
||||
var closest = -1;
|
||||
var last_valid = -1;
|
||||
for(var i = 0; i < num; ++i)
|
||||
{
|
||||
for (var i = 0; i < num; ++i) {
|
||||
var p = points[i];
|
||||
p2[0] = p[0] * w;
|
||||
p2[1] = (1.0 - p[1]) * h;
|
||||
@@ -14258,6 +14245,7 @@ const globalExport = {};
|
||||
}
|
||||
return closest;
|
||||
}
|
||||
}
|
||||
|
||||
LiteGraph.CurveEditor = CurveEditor;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user