Remove extending the Javascript builtin objects

- Remove extending the Math & CanvasRenderingContext2D classes.
  - It was messing up the typescript 4.9+
  - Using the built-in roundRect function
  - Adding global clamp function
- Add some type corrections in litegraph.d.ts
- Fix a couple of build issues

Co-authored-by: Ranuka Perera <premium@sawrc.com>
Co-authored-by: Moritz Ulmer <moritz.ulmer@posteo.de>
This commit is contained in:
Moritz Ulmer
2023-06-30 13:31:00 +02:00
parent 0a4bd193a3
commit aec5d622a9
12 changed files with 51 additions and 129 deletions

View File

@@ -133,7 +133,7 @@
if(index != null)
{
index = Math.floor(index);
index = Math.clamp( index, 0, this.outputs ? (this.outputs.length - 2) : 0 );
index = clamp( index, 0, this.outputs ? (this.outputs.length - 2) : 0 );
if( index != this.properties.index )
{
this.properties.index = index;

View File

@@ -2077,7 +2077,7 @@ void main() {\n\
LGraphTextureLinearAvgSmooth._shader_avg = new GL.Shader( GL.Shader.SCREEN_VERTEX_SHADER, LGraphTextureLinearAvgSmooth.pixel_shader_avg );
}
var samples = Math.clamp(this.properties.samples,0,64);
var samples = clamp(this.properties.samples,0,64);
var frame = this.frame;
var interval = this.properties.frames_interval;
@@ -2708,11 +2708,11 @@ void main() {\n\
var c = this.properties.color;
ctx.fillStyle =
"rgb(" +
Math.floor(Math.clamp(c[0], 0, 1) * 255) +
Math.floor(clamp(c[0], 0, 1) * 255) +
"," +
Math.floor(Math.clamp(c[1], 0, 1) * 255) +
Math.floor(clamp(c[1], 0, 1) * 255) +
"," +
Math.floor(Math.clamp(c[2], 0, 1) * 255) +
Math.floor(clamp(c[2], 0, 1) * 255) +
")";
if (this.flags.collapsed) {
this.boxcolor = ctx.fillStyle;
@@ -3540,7 +3540,7 @@ LGraphTextureBlur.pixel_shader = "precision highp float;\n\
var currentSource = currentDestination;
var iterations = this.iterations;
iterations = Math.clamp(iterations, 1, 16) | 0;
iterations = clamp(iterations, 1, 16) | 0;
var texel_size = uniforms.u_texel_size;
var intensity = this.intensity;
@@ -4678,14 +4678,14 @@ void main(void){\n\
{
if(split)
{
values[i*4] = Math.clamp( this.sampleCurve(i/num,this._points.R)*255,0,255);
values[i*4+1] = Math.clamp( this.sampleCurve(i/num,this._points.G)*255,0,255);
values[i*4+2] = Math.clamp( this.sampleCurve(i/num,this._points.B)*255,0,255);
values[i*4] = clamp( this.sampleCurve(i/num,this._points.R)*255,0,255);
values[i*4+1] = clamp( this.sampleCurve(i/num,this._points.G)*255,0,255);
values[i*4+2] = clamp( this.sampleCurve(i/num,this._points.B)*255,0,255);
}
else
{
var v = this.sampleCurve(i/num);//sample curve
values[i*4] = values[i*4+1] = values[i*4+2] = Math.clamp(v*255,0,255);
values[i*4] = values[i*4+1] = values[i*4+2] = clamp(v*255,0,255);
}
values[i*4+3] = 255; //alpha fixed
}

View File

@@ -63,10 +63,10 @@
ctx.strokeStyle = colors[i];
ctx.beginPath();
var v = values[0] * scale * -1 + offset;
ctx.moveTo(0, Math.clamp(v, 0, size[1]));
ctx.moveTo(0, clamp(v, 0, size[1]));
for (var j = 1; j < values.length && j < size[0]; ++j) {
var v = values[j] * scale * -1 + offset;
ctx.lineTo(j, Math.clamp(v, 0, size[1]));
ctx.lineTo(j, clamp(v, 0, size[1]));
}
ctx.stroke();
}

View File

@@ -244,7 +244,7 @@
this._remainder = steps % 1;
steps = steps | 0;
var v = Math.clamp(
var v = clamp(
this.properties.value + steps * this.properties.step,
this.properties.min,
this.properties.max
@@ -257,7 +257,7 @@
WidgetNumber.prototype.onMouseUp = function(e, pos) {
if (e.click_time < 200) {
var steps = pos[1] > this.size[1] * 0.5 ? -1 : 1;
this.properties.value = Math.clamp(
this.properties.value = clamp(
this.properties.value + steps * this.properties.step,
this.properties.min,
this.properties.max

View File

@@ -168,7 +168,7 @@
this._last_v = ((v - in_min) / (in_max - in_min)) * (out_max - out_min) + out_min;
this.setOutputData(0, this._last_v);
this.setOutputData(1, Math.clamp( this._last_v, out_min, out_max ));
this.setOutputData(1, clamp( this._last_v, out_min, out_max ));
};
MathRange.prototype.onDrawBackground = function(ctx) {
@@ -498,7 +498,7 @@
var edge1 = this.properties.B;
// Scale, bias and saturate x to 0..1 range
v = Math.clamp((v - edge0) / (edge1 - edge0), 0.0, 1.0);
v = clamp((v - edge0) / (edge1 - edge0), 0.0, 1.0);
// Evaluate polynomial
v = v * v * (3 - 2 * v);

View File

@@ -544,7 +544,7 @@
for(var i = 0; i < 3; ++i)
{
var r = range_max[i] - range_min[i];
this._clamped[i] = Math.clamp( this._value[i], range_min[i], range_max[i] );
this._clamped[i] = clamp( this._value[i], range_min[i], range_max[i] );
if(r == 0)
{
this._value[i] = (target_min[i] + target_max[i]) * 0.5;
@@ -553,7 +553,7 @@
var n = (this._value[i] - range_min[i]) / r;
if(this.properties.clamp)
n = Math.clamp(n,0,1);
n = clamp(n,0,1);
var t = target_max[i] - target_min[i];
this._value[i] = target_min[i] + n * t;
}

View File

@@ -891,13 +891,13 @@
case "value1":
var v = this.getInputData(i);
if (v != null) {
this.properties.value1 = Math.clamp(v|0,0,127);
this.properties.value1 = clamp(v|0,0,127);
}
break;
case "value2":
var v = this.getInputData(i);
if (v != null) {
this.properties.value2 = Math.clamp(v|0,0,127);
this.properties.value2 = clamp(v|0,0,127);
}
break;
}