Skip to content

Commit 8ceb939

Browse files
build
1 parent acafc15 commit 8ceb939

6 files changed

Lines changed: 221 additions & 5 deletions

File tree

dist/littlejs.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,10 @@ declare module "littlejsengine" {
13881388
* @param {Color} c - other color
13891389
* @return {Color} */
13901390
setFrom(c: Color): Color;
1391+
/** Sets the alpha of this color and returns self
1392+
* @param {number} [a] - alpha
1393+
* @return {Color} */
1394+
setAlpha(a?: number): Color;
13911395
/** Returns a new color that is a copy of this
13921396
* @return {Color} */
13931397
copy(): Color;
@@ -2077,6 +2081,19 @@ declare module "littlejsengine" {
20772081
* @return {Vector2}
20782082
* @memberof Draw */
20792083
export function getCameraSize(): Vector2;
2084+
/** Fit the camera to a rectangle in world space by setting cameraPos and cameraScale
2085+
* - worldMargin pads the content rectangle in world units, so the gap scales with the content on resize
2086+
* - screenInset reserves space in screen pixels on each viewport edge (for example a HUD band) and
2087+
* re-centers the content away from that edge, so the reserved band stays a fixed pixel size on resize
2088+
* - worldMargin and screenInset may each be a number for all sides, a Vector2 (x=left/right, y=top/bottom),
2089+
* or an object with any of {top, right, bottom, left}
2090+
* @param {Vector2} center - Center of the rectangle in world space
2091+
* @param {Vector2} size - Size of the rectangle in world space
2092+
* @param {number|Vector2|Object} [worldMargin] - World space padding added around the content rectangle
2093+
* @param {number|Vector2|Object} [screenInset] - Screen space padding in pixels reserved on each viewport edge
2094+
* @return {number} - The new camera scale
2095+
* @memberof Draw */
2096+
export function cameraFit(center: Vector2, size: Vector2, worldMargin?: number | Vector2 | any, screenInset?: number | Vector2 | any): number;
20802097
/** Check if a box, point, or circle is on screen with a circle test
20812098
* If size is a Vector2, uses the length as diameter
20822099
* This can be used to cull offscreen objects from render or update

dist/littlejs.esm.js

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const engineName = 'LittleJS';
3535
* @type {string}
3636
* @default
3737
* @memberof Engine */
38-
const engineVersion = '1.18.17';
38+
const engineVersion = '1.18.17.1';
3939

4040
/** Frames per second to update
4141
* @type {number}
@@ -2233,6 +2233,16 @@ class Color
22332233
* @return {Color} */
22342234
setFrom(c) { return this.set(c.r, c.g, c.b, c.a); }
22352235

2236+
/** Sets the alpha of this color and returns self
2237+
* @param {number} [a] - alpha
2238+
* @return {Color} */
2239+
setAlpha(a=1)
2240+
{
2241+
this.a = a;
2242+
ASSERT_COLOR_VALID(this);
2243+
return this;
2244+
}
2245+
22362246
/** Returns a new color that is a copy of this
22372247
* @return {Color} */
22382248
copy() { return new Color(this.r, this.g, this.b, this.a); }
@@ -5078,6 +5088,62 @@ function screenToWorldTransform(screenPos, screenSize, screenAngle=0)
50785088
* @memberof Draw */
50795089
function getCameraSize() { return mainCanvasSize.scale(1/cameraScale); }
50805090

5091+
/** Fit the camera to a rectangle in world space by setting cameraPos and cameraScale
5092+
* - worldMargin pads the content rectangle in world units, so the gap scales with the content on resize
5093+
* - screenInset reserves space in screen pixels on each viewport edge (for example a HUD band) and
5094+
* re-centers the content away from that edge, so the reserved band stays a fixed pixel size on resize
5095+
* - worldMargin and screenInset may each be a number for all sides, a Vector2 (x=left/right, y=top/bottom),
5096+
* or an object with any of {top, right, bottom, left}
5097+
* @param {Vector2} center - Center of the rectangle in world space
5098+
* @param {Vector2} size - Size of the rectangle in world space
5099+
* @param {number|Vector2|Object} [worldMargin] - World space padding added around the content rectangle
5100+
* @param {number|Vector2|Object} [screenInset] - Screen space padding in pixels reserved on each viewport edge
5101+
* @return {number} - The new camera scale
5102+
* @memberof Draw */
5103+
function cameraFit(center, size, worldMargin, screenInset)
5104+
{
5105+
ASSERT(isVector2(center), 'center must be a vec2');
5106+
ASSERT(isVector2(size), 'size must be a vec2');
5107+
5108+
// pad the content
5109+
const margin = padSides(worldMargin);
5110+
const inset = padSides(screenInset);
5111+
const worldW = size.x + margin.left + margin.right;
5112+
const worldH = size.y + margin.top + margin.bottom;
5113+
const viewW = mainCanvasSize.x - inset.left - inset.right;
5114+
const viewH = mainCanvasSize.y - inset.top - inset.bottom;
5115+
5116+
// bail on a degenerate rect or viewport
5117+
if (!(worldW > 0 && worldH > 0 && viewW > 0 && viewH > 0))
5118+
return cameraScale;
5119+
5120+
// bail on a degenerate rect or viewport rather than NaN the camera
5121+
cameraScale = min(viewW / worldW, viewH / worldH);
5122+
5123+
// calculate offset vectors
5124+
const marginVector = vec2(margin.right - margin.left, margin.top - margin.bottom).scale(.5);
5125+
const insetVector = vec2(inset.right - inset.left, inset.top - inset.bottom).scale(.5 / cameraScale);
5126+
5127+
// apply the offsets and return camera scale
5128+
cameraPos = center.add(marginVector).add(insetVector);
5129+
return cameraScale;
5130+
5131+
function padSides(p)
5132+
{
5133+
// normalize a padding option to {top, right, bottom, left}
5134+
if (p === undefined || isNumber(p))
5135+
p = vec2(p);
5136+
if (isVector2(p))
5137+
return { top: p.y, right: p.x, bottom: p.y, left: p.x };
5138+
return {
5139+
top: p.top || 0,
5140+
right: p.right || 0,
5141+
bottom: p.bottom || 0,
5142+
left: p.left || 0,
5143+
};
5144+
}
5145+
}
5146+
50815147
/** Check if a box, point, or circle is on screen with a circle test
50825148
* If size is a Vector2, uses the length as diameter
50835149
* This can be used to cull offscreen objects from render or update
@@ -16346,6 +16412,7 @@ export
1634616412
toggleFullscreen,
1634716413
setCursor,
1634816414
getCameraSize,
16415+
cameraFit,
1634916416
isOnScreen,
1635016417

1635116418
// WebGL

dist/littlejs.esm.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/littlejs.js

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const engineName = 'LittleJS';
3535
* @type {string}
3636
* @default
3737
* @memberof Engine */
38-
const engineVersion = '1.18.17';
38+
const engineVersion = '1.18.17.1';
3939

4040
/** Frames per second to update
4141
* @type {number}
@@ -2233,6 +2233,16 @@ class Color
22332233
* @return {Color} */
22342234
setFrom(c) { return this.set(c.r, c.g, c.b, c.a); }
22352235

2236+
/** Sets the alpha of this color and returns self
2237+
* @param {number} [a] - alpha
2238+
* @return {Color} */
2239+
setAlpha(a=1)
2240+
{
2241+
this.a = a;
2242+
ASSERT_COLOR_VALID(this);
2243+
return this;
2244+
}
2245+
22362246
/** Returns a new color that is a copy of this
22372247
* @return {Color} */
22382248
copy() { return new Color(this.r, this.g, this.b, this.a); }
@@ -5078,6 +5088,62 @@ function screenToWorldTransform(screenPos, screenSize, screenAngle=0)
50785088
* @memberof Draw */
50795089
function getCameraSize() { return mainCanvasSize.scale(1/cameraScale); }
50805090

5091+
/** Fit the camera to a rectangle in world space by setting cameraPos and cameraScale
5092+
* - worldMargin pads the content rectangle in world units, so the gap scales with the content on resize
5093+
* - screenInset reserves space in screen pixels on each viewport edge (for example a HUD band) and
5094+
* re-centers the content away from that edge, so the reserved band stays a fixed pixel size on resize
5095+
* - worldMargin and screenInset may each be a number for all sides, a Vector2 (x=left/right, y=top/bottom),
5096+
* or an object with any of {top, right, bottom, left}
5097+
* @param {Vector2} center - Center of the rectangle in world space
5098+
* @param {Vector2} size - Size of the rectangle in world space
5099+
* @param {number|Vector2|Object} [worldMargin] - World space padding added around the content rectangle
5100+
* @param {number|Vector2|Object} [screenInset] - Screen space padding in pixels reserved on each viewport edge
5101+
* @return {number} - The new camera scale
5102+
* @memberof Draw */
5103+
function cameraFit(center, size, worldMargin, screenInset)
5104+
{
5105+
ASSERT(isVector2(center), 'center must be a vec2');
5106+
ASSERT(isVector2(size), 'size must be a vec2');
5107+
5108+
// pad the content
5109+
const margin = padSides(worldMargin);
5110+
const inset = padSides(screenInset);
5111+
const worldW = size.x + margin.left + margin.right;
5112+
const worldH = size.y + margin.top + margin.bottom;
5113+
const viewW = mainCanvasSize.x - inset.left - inset.right;
5114+
const viewH = mainCanvasSize.y - inset.top - inset.bottom;
5115+
5116+
// bail on a degenerate rect or viewport
5117+
if (!(worldW > 0 && worldH > 0 && viewW > 0 && viewH > 0))
5118+
return cameraScale;
5119+
5120+
// bail on a degenerate rect or viewport rather than NaN the camera
5121+
cameraScale = min(viewW / worldW, viewH / worldH);
5122+
5123+
// calculate offset vectors
5124+
const marginVector = vec2(margin.right - margin.left, margin.top - margin.bottom).scale(.5);
5125+
const insetVector = vec2(inset.right - inset.left, inset.top - inset.bottom).scale(.5 / cameraScale);
5126+
5127+
// apply the offsets and return camera scale
5128+
cameraPos = center.add(marginVector).add(insetVector);
5129+
return cameraScale;
5130+
5131+
function padSides(p)
5132+
{
5133+
// normalize a padding option to {top, right, bottom, left}
5134+
if (p === undefined || isNumber(p))
5135+
p = vec2(p);
5136+
if (isVector2(p))
5137+
return { top: p.y, right: p.x, bottom: p.y, left: p.x };
5138+
return {
5139+
top: p.top || 0,
5140+
right: p.right || 0,
5141+
bottom: p.bottom || 0,
5142+
left: p.left || 0,
5143+
};
5144+
}
5145+
}
5146+
50815147
/** Check if a box, point, or circle is on screen with a circle test
50825148
* If size is a Vector2, uses the length as diameter
50835149
* This can be used to cull offscreen objects from render or update

dist/littlejs.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/littlejs.release.js

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const engineName = 'LittleJS';
3535
* @type {string}
3636
* @default
3737
* @memberof Engine */
38-
const engineVersion = '1.18.17';
38+
const engineVersion = '1.18.17.1';
3939

4040
/** Frames per second to update
4141
* @type {number}
@@ -1545,6 +1545,16 @@ class Color
15451545
* @return {Color} */
15461546
setFrom(c) { return this.set(c.r, c.g, c.b, c.a); }
15471547

1548+
/** Sets the alpha of this color and returns self
1549+
* @param {number} [a] - alpha
1550+
* @return {Color} */
1551+
setAlpha(a=1)
1552+
{
1553+
this.a = a;
1554+
ASSERT_COLOR_VALID(this);
1555+
return this;
1556+
}
1557+
15481558
/** Returns a new color that is a copy of this
15491559
* @return {Color} */
15501560
copy() { return new Color(this.r, this.g, this.b, this.a); }
@@ -4390,6 +4400,62 @@ function screenToWorldTransform(screenPos, screenSize, screenAngle=0)
43904400
* @memberof Draw */
43914401
function getCameraSize() { return mainCanvasSize.scale(1/cameraScale); }
43924402

4403+
/** Fit the camera to a rectangle in world space by setting cameraPos and cameraScale
4404+
* - worldMargin pads the content rectangle in world units, so the gap scales with the content on resize
4405+
* - screenInset reserves space in screen pixels on each viewport edge (for example a HUD band) and
4406+
* re-centers the content away from that edge, so the reserved band stays a fixed pixel size on resize
4407+
* - worldMargin and screenInset may each be a number for all sides, a Vector2 (x=left/right, y=top/bottom),
4408+
* or an object with any of {top, right, bottom, left}
4409+
* @param {Vector2} center - Center of the rectangle in world space
4410+
* @param {Vector2} size - Size of the rectangle in world space
4411+
* @param {number|Vector2|Object} [worldMargin] - World space padding added around the content rectangle
4412+
* @param {number|Vector2|Object} [screenInset] - Screen space padding in pixels reserved on each viewport edge
4413+
* @return {number} - The new camera scale
4414+
* @memberof Draw */
4415+
function cameraFit(center, size, worldMargin, screenInset)
4416+
{
4417+
ASSERT(isVector2(center), 'center must be a vec2');
4418+
ASSERT(isVector2(size), 'size must be a vec2');
4419+
4420+
// pad the content
4421+
const margin = padSides(worldMargin);
4422+
const inset = padSides(screenInset);
4423+
const worldW = size.x + margin.left + margin.right;
4424+
const worldH = size.y + margin.top + margin.bottom;
4425+
const viewW = mainCanvasSize.x - inset.left - inset.right;
4426+
const viewH = mainCanvasSize.y - inset.top - inset.bottom;
4427+
4428+
// bail on a degenerate rect or viewport
4429+
if (!(worldW > 0 && worldH > 0 && viewW > 0 && viewH > 0))
4430+
return cameraScale;
4431+
4432+
// bail on a degenerate rect or viewport rather than NaN the camera
4433+
cameraScale = min(viewW / worldW, viewH / worldH);
4434+
4435+
// calculate offset vectors
4436+
const marginVector = vec2(margin.right - margin.left, margin.top - margin.bottom).scale(.5);
4437+
const insetVector = vec2(inset.right - inset.left, inset.top - inset.bottom).scale(.5 / cameraScale);
4438+
4439+
// apply the offsets and return camera scale
4440+
cameraPos = center.add(marginVector).add(insetVector);
4441+
return cameraScale;
4442+
4443+
function padSides(p)
4444+
{
4445+
// normalize a padding option to {top, right, bottom, left}
4446+
if (p === undefined || isNumber(p))
4447+
p = vec2(p);
4448+
if (isVector2(p))
4449+
return { top: p.y, right: p.x, bottom: p.y, left: p.x };
4450+
return {
4451+
top: p.top || 0,
4452+
right: p.right || 0,
4453+
bottom: p.bottom || 0,
4454+
left: p.left || 0,
4455+
};
4456+
}
4457+
}
4458+
43934459
/** Check if a box, point, or circle is on screen with a circle test
43944460
* If size is a Vector2, uses the length as diameter
43954461
* This can be used to cull offscreen objects from render or update

0 commit comments

Comments
 (0)