Skip to content

Commit 23c300b

Browse files
authored
Merge pull request #401 from moritz89/master
Remove extending the Javascript builtin objects
2 parents 0a4bd19 + aec5d62 commit 23c300b

File tree

12 files changed

+51
-129
lines changed

12 files changed

+51
-129
lines changed

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@
3838
"eslint": "^8.37.0 ",
3939
"eslint-plugin-jest": "^27.2.1",
4040
"express": "^4.17.1",
41-
"google-closure-compiler": "^20171112.0.0",
41+
"google-closure-compiler": "^20230411.0.0",
4242
"grunt": "^1.1.0",
4343
"grunt-cli": "^1.2.0",
4444
"grunt-closure-tools": "^1.0.0",
45-
"grunt-contrib-concat": "^1.0.1",
45+
"grunt-contrib-concat": "^2.1.0",
4646
"jest": "^28.1.3",
47-
"nodemon": "^1.19.4",
48-
"rimraf": "^2.7.1"
47+
"jest-cli": "^28.1.3",
48+
"nodemon": "^2.0.22",
49+
"rimraf": "^5.0.0",
50+
"yuidocjs": "^0.10.2"
4951
}
5052
}

src/litegraph.d.ts

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,8 @@ export declare class LGraphNode {
698698
getInputInfo(
699699
slot: number
700700
): { link: number; name: string; type: string | 0 } | null;
701+
/** Returns the link info in the connection of an input slot */
702+
getInputLink(slot: number): LLink | null;
701703
/** returns the node connected in the input slot */
702704
getInputNode(slot: number): LGraphNode | null;
703705
/** returns the value of an input with this name, otherwise checks if there is a property with that name */
@@ -729,6 +731,8 @@ export declare class LGraphNode {
729731
* @param link_id in case you want to trigger and specific output link in a slot
730732
*/
731733
clearTriggeredSlot(slot: number, link_id?: number): void;
734+
/** changes node size and triggers callback */
735+
setSize(size: Vector2): void;
732736
/**
733737
* add a new property to this node
734738
* @param name
@@ -801,9 +805,10 @@ export declare class LGraphNode {
801805
direction: string;
802806
links: null;
803807
};
804-
setValue(v: any): void;
805-
/** computes the size of a node according to its inputs and output slots */
806-
computeSize(): [number, number];
808+
/** computes the minimum size of a node according to its inputs and output slots */
809+
computeSize(minHeight?: Vector2): Vector2;
810+
/** returns all the info available about a property of this node */
811+
getPropertyInfo(property: string): object;
807812
/**
808813
* https://github.com/jagenjo/litegraph.js/blob/master/guides/README.md#node-widgets
809814
* @return created widget
@@ -1487,20 +1492,4 @@ declare class ContextMenu {
14871492
getFirstEvent(): void;
14881493
}
14891494

1490-
declare global {
1491-
interface CanvasRenderingContext2D {
1492-
/** like rect but rounded corners */
1493-
roundRect(
1494-
x: number,
1495-
y: number,
1496-
width: number,
1497-
height: number,
1498-
radius: number,
1499-
radiusLow: number
1500-
): void;
1501-
}
1502-
1503-
interface Math {
1504-
clamp(v: number, min: number, max: number): number;
1505-
}
1506-
}
1495+
declare function clamp(v: number, min: number, max: number): number;

src/litegraph.js

Lines changed: 8 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -3569,8 +3569,8 @@
35693569
/**
35703570
* computes the minimum size of a node according to its inputs and output slots
35713571
* @method computeSize
3572-
* @param {number} minHeight
3573-
* @return {number} the total size
3572+
* @param {vec2} minHeight
3573+
* @return {vec2} the total size
35743574
*/
35753575
LGraphNode.prototype.computeSize = function(out) {
35763576
if (this.constructor.size) {
@@ -9997,7 +9997,7 @@ LGraphNode.prototype.executeAction = function(action)
99979997
break;
99989998
case "slider":
99999999
var old_value = w.value;
10000-
var nvalue = Math.clamp((x - 15) / (widget_width - 30), 0, 1);
10000+
var nvalue = clamp((x - 15) / (widget_width - 30), 0, 1);
1000110001
if(w.options.read_only) break;
1000210002
w.value = w.options.min + (w.options.max - w.options.min) * nvalue;
1000310003
if (old_value != w.value) {
@@ -13378,82 +13378,6 @@ LGraphNode.prototype.executeAction = function(action)
1337813378
};
1337913379

1338013380
//API *************************************************
13381-
//like rect but rounded corners
13382-
if (typeof(window) != "undefined" && window.CanvasRenderingContext2D && !window.CanvasRenderingContext2D.prototype.roundRect) {
13383-
window.CanvasRenderingContext2D.prototype.roundRect = function(
13384-
x,
13385-
y,
13386-
w,
13387-
h,
13388-
radius,
13389-
radius_low
13390-
) {
13391-
var top_left_radius = 0;
13392-
var top_right_radius = 0;
13393-
var bottom_left_radius = 0;
13394-
var bottom_right_radius = 0;
13395-
13396-
if ( radius === 0 )
13397-
{
13398-
this.rect(x,y,w,h);
13399-
return;
13400-
}
13401-
13402-
if(radius_low === undefined)
13403-
radius_low = radius;
13404-
13405-
//make it compatible with official one
13406-
if(radius != null && radius.constructor === Array)
13407-
{
13408-
if(radius.length == 1)
13409-
top_left_radius = top_right_radius = bottom_left_radius = bottom_right_radius = radius[0];
13410-
else if(radius.length == 2)
13411-
{
13412-
top_left_radius = bottom_right_radius = radius[0];
13413-
top_right_radius = bottom_left_radius = radius[1];
13414-
}
13415-
else if(radius.length == 4)
13416-
{
13417-
top_left_radius = radius[0];
13418-
top_right_radius = radius[1];
13419-
bottom_left_radius = radius[2];
13420-
bottom_right_radius = radius[3];
13421-
}
13422-
else
13423-
return;
13424-
}
13425-
else //old using numbers
13426-
{
13427-
top_left_radius = radius || 0;
13428-
top_right_radius = radius || 0;
13429-
bottom_left_radius = radius_low || 0;
13430-
bottom_right_radius = radius_low || 0;
13431-
}
13432-
13433-
//top right
13434-
this.moveTo(x + top_left_radius, y);
13435-
this.lineTo(x + w - top_right_radius, y);
13436-
this.quadraticCurveTo(x + w, y, x + w, y + top_right_radius);
13437-
13438-
//bottom right
13439-
this.lineTo(x + w, y + h - bottom_right_radius);
13440-
this.quadraticCurveTo(
13441-
x + w,
13442-
y + h,
13443-
x + w - bottom_right_radius,
13444-
y + h
13445-
);
13446-
13447-
//bottom left
13448-
this.lineTo(x + bottom_right_radius, y + h);
13449-
this.quadraticCurveTo(x, y + h, x, y + h - bottom_left_radius);
13450-
13451-
//top left
13452-
this.lineTo(x, y + bottom_left_radius);
13453-
this.quadraticCurveTo(x, y, x + top_left_radius, y);
13454-
};
13455-
}//if
13456-
1345713381
function compareObjects(a, b) {
1345813382
for (var i in a) {
1345913383
if (a[i] != b[i]) {
@@ -14182,10 +14106,10 @@ LGraphNode.prototype.executeAction = function(action)
1418214106
return;
1418314107
}
1418414108
if( !is_edge_point ) //not edges
14185-
point[0] = Math.clamp(x,0,1);
14109+
point[0] = clamp(x, 0, 1);
1418614110
else
1418714111
point[0] = s == 0 ? 0 : 1;
14188-
point[1] = 1.0 - Math.clamp(y,0,1);
14112+
point[1] = 1.0 - clamp(y, 0, 1);
1418914113
points.sort(function(a,b){ return a[0] - b[0]; });
1419014114
this.selected = points.indexOf(point);
1419114115
this.must_update = true;
@@ -14333,10 +14257,11 @@ LGraphNode.prototype.executeAction = function(action)
1433314257
return oDOM.removeEventListener(sEvent, fCall, capture);
1433414258
}
1433514259
}
14336-
14337-
Math.clamp = function(v, a, b) {
14260+
14261+
function clamp(v, a, b) {
1433814262
return a > v ? a : b < v ? b : v;
1433914263
};
14264+
global.clamp = clamp;
1434014265

1434114266
if (typeof window != "undefined" && !window["requestAnimationFrame"]) {
1434214267
window.requestAnimationFrame =

src/nodes/events.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
if(index != null)
134134
{
135135
index = Math.floor(index);
136-
index = Math.clamp( index, 0, this.outputs ? (this.outputs.length - 2) : 0 );
136+
index = clamp( index, 0, this.outputs ? (this.outputs.length - 2) : 0 );
137137
if( index != this.properties.index )
138138
{
139139
this.properties.index = index;

src/nodes/gltextures.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,7 +2077,7 @@ void main() {\n\
20772077
LGraphTextureLinearAvgSmooth._shader_avg = new GL.Shader( GL.Shader.SCREEN_VERTEX_SHADER, LGraphTextureLinearAvgSmooth.pixel_shader_avg );
20782078
}
20792079

2080-
var samples = Math.clamp(this.properties.samples,0,64);
2080+
var samples = clamp(this.properties.samples,0,64);
20812081
var frame = this.frame;
20822082
var interval = this.properties.frames_interval;
20832083

@@ -2708,11 +2708,11 @@ void main() {\n\
27082708
var c = this.properties.color;
27092709
ctx.fillStyle =
27102710
"rgb(" +
2711-
Math.floor(Math.clamp(c[0], 0, 1) * 255) +
2711+
Math.floor(clamp(c[0], 0, 1) * 255) +
27122712
"," +
2713-
Math.floor(Math.clamp(c[1], 0, 1) * 255) +
2713+
Math.floor(clamp(c[1], 0, 1) * 255) +
27142714
"," +
2715-
Math.floor(Math.clamp(c[2], 0, 1) * 255) +
2715+
Math.floor(clamp(c[2], 0, 1) * 255) +
27162716
")";
27172717
if (this.flags.collapsed) {
27182718
this.boxcolor = ctx.fillStyle;
@@ -3540,7 +3540,7 @@ LGraphTextureBlur.pixel_shader = "precision highp float;\n\
35403540
var currentSource = currentDestination;
35413541

35423542
var iterations = this.iterations;
3543-
iterations = Math.clamp(iterations, 1, 16) | 0;
3543+
iterations = clamp(iterations, 1, 16) | 0;
35443544
var texel_size = uniforms.u_texel_size;
35453545
var intensity = this.intensity;
35463546

@@ -4678,14 +4678,14 @@ void main(void){\n\
46784678
{
46794679
if(split)
46804680
{
4681-
values[i*4] = Math.clamp( this.sampleCurve(i/num,this._points.R)*255,0,255);
4682-
values[i*4+1] = Math.clamp( this.sampleCurve(i/num,this._points.G)*255,0,255);
4683-
values[i*4+2] = Math.clamp( this.sampleCurve(i/num,this._points.B)*255,0,255);
4681+
values[i*4] = clamp( this.sampleCurve(i/num,this._points.R)*255,0,255);
4682+
values[i*4+1] = clamp( this.sampleCurve(i/num,this._points.G)*255,0,255);
4683+
values[i*4+2] = clamp( this.sampleCurve(i/num,this._points.B)*255,0,255);
46844684
}
46854685
else
46864686
{
46874687
var v = this.sampleCurve(i/num);//sample curve
4688-
values[i*4] = values[i*4+1] = values[i*4+2] = Math.clamp(v*255,0,255);
4688+
values[i*4] = values[i*4+1] = values[i*4+2] = clamp(v*255,0,255);
46894689
}
46904690
values[i*4+3] = 255; //alpha fixed
46914691
}

src/nodes/graphics.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@
6363
ctx.strokeStyle = colors[i];
6464
ctx.beginPath();
6565
var v = values[0] * scale * -1 + offset;
66-
ctx.moveTo(0, Math.clamp(v, 0, size[1]));
66+
ctx.moveTo(0, clamp(v, 0, size[1]));
6767
for (var j = 1; j < values.length && j < size[0]; ++j) {
6868
var v = values[j] * scale * -1 + offset;
69-
ctx.lineTo(j, Math.clamp(v, 0, size[1]));
69+
ctx.lineTo(j, clamp(v, 0, size[1]));
7070
}
7171
ctx.stroke();
7272
}

src/nodes/interface.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@
244244
this._remainder = steps % 1;
245245
steps = steps | 0;
246246

247-
var v = Math.clamp(
247+
var v = clamp(
248248
this.properties.value + steps * this.properties.step,
249249
this.properties.min,
250250
this.properties.max
@@ -257,7 +257,7 @@
257257
WidgetNumber.prototype.onMouseUp = function(e, pos) {
258258
if (e.click_time < 200) {
259259
var steps = pos[1] > this.size[1] * 0.5 ? -1 : 1;
260-
this.properties.value = Math.clamp(
260+
this.properties.value = clamp(
261261
this.properties.value + steps * this.properties.step,
262262
this.properties.min,
263263
this.properties.max

src/nodes/math.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168

169169
this._last_v = ((v - in_min) / (in_max - in_min)) * (out_max - out_min) + out_min;
170170
this.setOutputData(0, this._last_v);
171-
this.setOutputData(1, Math.clamp( this._last_v, out_min, out_max ));
171+
this.setOutputData(1, clamp( this._last_v, out_min, out_max ));
172172
};
173173

174174
MathRange.prototype.onDrawBackground = function(ctx) {
@@ -498,7 +498,7 @@
498498
var edge1 = this.properties.B;
499499

500500
// Scale, bias and saturate x to 0..1 range
501-
v = Math.clamp((v - edge0) / (edge1 - edge0), 0.0, 1.0);
501+
v = clamp((v - edge0) / (edge1 - edge0), 0.0, 1.0);
502502
// Evaluate polynomial
503503
v = v * v * (3 - 2 * v);
504504

src/nodes/math3d.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@
544544
for(var i = 0; i < 3; ++i)
545545
{
546546
var r = range_max[i] - range_min[i];
547-
this._clamped[i] = Math.clamp( this._value[i], range_min[i], range_max[i] );
547+
this._clamped[i] = clamp( this._value[i], range_min[i], range_max[i] );
548548
if(r == 0)
549549
{
550550
this._value[i] = (target_min[i] + target_max[i]) * 0.5;
@@ -553,7 +553,7 @@
553553

554554
var n = (this._value[i] - range_min[i]) / r;
555555
if(this.properties.clamp)
556-
n = Math.clamp(n,0,1);
556+
n = clamp(n,0,1);
557557
var t = target_max[i] - target_min[i];
558558
this._value[i] = target_min[i] + n * t;
559559
}

src/nodes/midi.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -891,13 +891,13 @@
891891
case "value1":
892892
var v = this.getInputData(i);
893893
if (v != null) {
894-
this.properties.value1 = Math.clamp(v|0,0,127);
894+
this.properties.value1 = clamp(v|0,0,127);
895895
}
896896
break;
897897
case "value2":
898898
var v = this.getInputData(i);
899899
if (v != null) {
900-
this.properties.value2 = Math.clamp(v|0,0,127);
900+
this.properties.value2 = clamp(v|0,0,127);
901901
}
902902
break;
903903
}

0 commit comments

Comments
 (0)