littlejsengine 1.9.5 → 1.9.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -64,7 +64,7 @@ function clamp(value, min=0, max=1) { return value < min ? min : value > max ? m
64
64
  * @return {Number}
65
65
  * @memberof Utilities */
66
66
  function percent(value, valueA, valueB)
67
- { return valueB-valueA ? clamp((value-valueA) / (valueB-valueA)) : 0; }
67
+ { return (valueB-=valueA) ? clamp((value-valueA)/valueB) : 0; }
68
68
 
69
69
  /** Linearly interpolates between values passed in using percent
70
70
  * @param {Number} percent
@@ -271,7 +271,7 @@ class RandomGenerator
271
271
  this.seed ^= this.seed << 13;
272
272
  this.seed ^= this.seed >>> 17;
273
273
  this.seed ^= this.seed << 5;
274
- return valueB + (valueA - valueB) * abs(this.seed % 1e9) / 1e9;
274
+ return valueB + (valueA - valueB) * abs(this.seed % 1e8) / 1e8;
275
275
  }
276
276
 
277
277
  /** Returns a floored seeded random value the two values passed in
@@ -282,7 +282,7 @@ class RandomGenerator
282
282
 
283
283
  /** Randomly returns either -1 or 1 deterministically
284
284
  * @return {Number} */
285
- sign() { return this.int(2) * 2 - 1; }
285
+ sign() { return this.float() > .5 ? 1 : -1; }
286
286
  }
287
287
 
288
288
  ///////////////////////////////////////////////////////////////////////////////
@@ -330,6 +330,7 @@ class Vector2
330
330
  * @param {Number} [y] - Y axis location */
331
331
  constructor(x=0, y=0)
332
332
  {
333
+ ASSERT(typeof x === 'number' && typeof y === 'number');
333
334
  /** @property {Number} - X axis location */
334
335
  this.x = x;
335
336
  /** @property {Number} - Y axis location */
@@ -656,12 +657,14 @@ class Color
656
657
  * @return {Color} */
657
658
  setHSLA(h=0, s=0, l=1, a=1)
658
659
  {
660
+ h = mod(h,1);
661
+ s = clamp(s);
662
+ l = clamp(l);
659
663
  const q = l < .5 ? l*(1+s) : l+s-l*s, p = 2*l-q,
660
664
  f = (p, q, t)=>
661
- (t = ((t%1)+1)%1) < 1/6 ? p+(q-p)*6*t :
662
- t < 1/2 ? q :
663
- t < 2/3 ? p+(q-p)*(2/3-t)*6 : p;
664
-
665
+ (t = mod(t,1))*6 < 1 ? p+(q-p)*6*t :
666
+ t*2 < 1 ? q :
667
+ t*3 < 2 ? p+(q-p)*(4-t*6) : p;
665
668
  this.r = f(p, q, h + 1/3);
666
669
  this.g = f(p, q, h);
667
670
  this.b = f(p, q, h - 1/3);
@@ -720,7 +723,7 @@ class Color
720
723
  const toHex = (c)=> ((c=c*255|0)<16 ? '0' : '') + c.toString(16);
721
724
  return '#' + toHex(this.r) + toHex(this.g) + toHex(this.b) + (useAlpha ? toHex(this.a) : '');
722
725
  }
723
-
726
+
724
727
  /** Set this color from a hex code
725
728
  * @param {String} hex - html hex code
726
729
  * @return {Color} */
@@ -776,11 +779,11 @@ class Timer
776
779
 
777
780
  /** Returns true if set and has not elapsed
778
781
  * @return {Boolean} */
779
- active() { return time <= this.time; }
782
+ active() { return time < this.time; }
780
783
 
781
784
  /** Returns true if set and elapsed
782
785
  * @return {Boolean} */
783
- elapsed() { return time > this.time; }
786
+ elapsed() { return time >= this.time; }
784
787
 
785
788
  /** Get how long since elapsed, returns 0 if not set (returns negative if currently active)
786
789
  * @return {Number} */
@@ -30,6 +30,8 @@ let glShader, glActiveTexture, glArrayBuffer, glGeometryBuffer, glPositionData,
30
30
  // Initalize WebGL, called automatically by the engine
31
31
  function glInit()
32
32
  {
33
+ if (!glEnable || headlessMode) return;
34
+
33
35
  // create the canvas and textures
34
36
  glCanvas = document.createElement('canvas');
35
37
  glContext = glCanvas.getContext('webgl2');
@@ -42,11 +44,11 @@ function glInit()
42
44
  '#version 300 es\n' + // specify GLSL ES version
43
45
  'precision highp float;'+ // use highp for better accuracy
44
46
  'uniform mat4 m;'+ // transform matrix
45
- 'in vec2 g;'+ // geometry
46
- 'in vec4 p,u,c,a;'+ // position/size, uvs, color, additiveColor
47
- 'in float r;'+ // rotation
48
- 'out vec2 v;'+ // return uv, color, additiveColor
49
- 'out vec4 d,e;'+ // return uv, color, additiveColor
47
+ 'in vec2 g;'+ // in: geometry
48
+ 'in vec4 p,u,c,a;'+ // in: position/size, uvs, color, additiveColor
49
+ 'in float r;'+ // in: rotation
50
+ 'out vec2 v;'+ // out: uv
51
+ 'out vec4 d,e;'+ // out: color, additiveColor
50
52
  'void main(){'+ // shader entry point
51
53
  'vec2 s=(g-.5)*p.zw;'+ // get size offset
52
54
  'gl_Position=m*vec4(p.xy+s*cos(r)-vec2(-s.y,s)*sin(r),1,1);'+ // transform position
@@ -56,10 +58,10 @@ function glInit()
56
58
  ,
57
59
  '#version 300 es\n' + // specify GLSL ES version
58
60
  'precision highp float;'+ // use highp for better accuracy
59
- 'in vec2 v;'+ // uv
60
- 'in vec4 d,e;'+ // color, additiveColor
61
61
  'uniform sampler2D s;'+ // texture
62
- 'out vec4 c;'+ // out color
62
+ 'in vec2 v;'+ // in: uv
63
+ 'in vec4 d,e;'+ // in: color, additiveColor
64
+ 'out vec4 c;'+ // out: color
63
65
  'void main(){'+ // shader entry point
64
66
  'c=texture(s,v)*d+e;'+ // modulate texture by color plus additive
65
67
  '}' // end of shader
@@ -81,9 +83,11 @@ function glInit()
81
83
  // Setup render each frame, called automatically by engine
82
84
  function glPreRender()
83
85
  {
86
+ if (!glEnable || headlessMode) return;
87
+
84
88
  // clear and set to same size as main canvas
85
89
  glContext.viewport(0, 0, glCanvas.width=mainCanvas.width, glCanvas.height=mainCanvas.height);
86
- glContext.clear(gl_COLOR_BUFFER_BIT);
90
+ //glContext.clear(gl_COLOR_BUFFER_BIT); // auto cleared when size is set
87
91
 
88
92
  // set up the shader
89
93
  glContext.useProgram(glShader);
@@ -117,12 +121,12 @@ function glPreRender()
117
121
  const s = vec2(2*cameraScale).divide(mainCanvasSize);
118
122
  const p = vec2(-1).subtract(cameraPos.multiply(s));
119
123
  glContext.uniformMatrix4fv(glContext.getUniformLocation(glShader, 'm'), false,
120
- new Float32Array([
124
+ [
121
125
  s.x, 0, 0, 0,
122
126
  0, s.y, 0, 0,
123
127
  1, 1, 1, 1,
124
128
  p.x, p.y, 0, 0
125
- ])
129
+ ]
126
130
  );
127
131
  }
128
132
 
@@ -133,7 +137,7 @@ function glPreRender()
133
137
  function glSetTexture(texture)
134
138
  {
135
139
  // must flush cache with the old texture to set a new one
136
- if (texture == glActiveTexture)
140
+ if (headlessMode || texture == glActiveTexture)
137
141
  return;
138
142
 
139
143
  glFlush();
@@ -193,7 +197,6 @@ function glCreateTexture(image)
193
197
  const filter = canvasPixelated ? gl_NEAREST : gl_LINEAR;
194
198
  glContext.texParameteri(gl_TEXTURE_2D, gl_TEXTURE_MIN_FILTER, filter);
195
199
  glContext.texParameteri(gl_TEXTURE_2D, gl_TEXTURE_MAG_FILTER, filter);
196
-
197
200
  return texture;
198
201
  }
199
202
 
@@ -217,12 +220,12 @@ function glFlush()
217
220
  }
218
221
 
219
222
  /** Draw any sprites still in the buffer, copy to main canvas and clear
220
- * @param {CanvasRenderingContext2D} context
223
+ * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} context
221
224
  * @param {Boolean} [forceDraw]
222
225
  * @memberof WebGL */
223
226
  function glCopyToContext(context, forceDraw=false)
224
227
  {
225
- if (!glInstanceCount && !forceDraw) return;
228
+ if (!glEnable || !glInstanceCount && !forceDraw) return;
226
229
 
227
230
  glFlush();
228
231
 
@@ -279,7 +282,7 @@ let glPostShader, glPostTexture, glPostIncludeOverlay;
279
282
  function glInitPostProcess(shaderCode, includeOverlay=false)
280
283
  {
281
284
  ASSERT(!glPostShader, 'can only have 1 post effects shader');
282
-
285
+ if (headlessMode) return;
283
286
  if (!shaderCode) // default shader pass through
284
287
  shaderCode = 'void mainImage(out vec4 c,vec2 p){c=texture(iChannel0,p/iResolution.xy);}';
285
288
 
@@ -318,8 +321,7 @@ function glInitPostProcess(shaderCode, includeOverlay=false)
318
321
  // Render the post processing shader, called automatically by the engine
319
322
  function glRenderPostProcess()
320
323
  {
321
- if (!glPostShader)
322
- return;
324
+ if (!glPostShader || headlessMode) return;
323
325
 
324
326
  // prepare to render post process shader
325
327
  if (glEnable)
package/LittleJS.zip DELETED
Binary file