@antha/graphics-2d 0.7.0 → 0.9.0
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.
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { type AnthaGraphics2dModState } from './antha-graphics-2d.mod.js';
|
|
2
|
+
/**
|
|
3
|
+
* The logical dimensions and scale of a virtual viewport.
|
|
4
|
+
*
|
|
5
|
+
* @category Internal
|
|
6
|
+
*/
|
|
7
|
+
export type VirtualViewport = {
|
|
8
|
+
height: number;
|
|
9
|
+
scale: number;
|
|
10
|
+
width: number;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* The dimensions of a virtual viewport without its scale.
|
|
14
|
+
*
|
|
15
|
+
* @category Internal
|
|
16
|
+
*/
|
|
17
|
+
export type VirtualViewportSize = Pick<VirtualViewport, 'height' | 'width'>;
|
|
18
|
+
/**
|
|
19
|
+
* State added by {@link createAnthaVirtualViewportMod}.
|
|
20
|
+
*
|
|
21
|
+
* @category Internal
|
|
22
|
+
*/
|
|
23
|
+
export type AnthaVirtualViewportModState = AnthaGraphics2dModState & {
|
|
24
|
+
virtualViewport: VirtualViewport | undefined;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Calculates the logical viewport for a physical screen size.
|
|
28
|
+
*
|
|
29
|
+
* @category Util
|
|
30
|
+
*/
|
|
31
|
+
export declare function calculateVirtualViewport({ screenSize, virtualWidth, }: Readonly<{
|
|
32
|
+
screenSize: Readonly<VirtualViewportSize>;
|
|
33
|
+
virtualWidth: number;
|
|
34
|
+
}>): {
|
|
35
|
+
height: number;
|
|
36
|
+
scale: number;
|
|
37
|
+
width: number;
|
|
38
|
+
} | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Maps a pointer position from canvas coordinates into a virtual viewport.
|
|
41
|
+
*
|
|
42
|
+
* @category Util
|
|
43
|
+
*/
|
|
44
|
+
export declare function calculateVirtualViewportPoint({ canvasBounds, clientPoint, virtualViewport, }: Readonly<{
|
|
45
|
+
canvasBounds: Readonly<{
|
|
46
|
+
height: number;
|
|
47
|
+
left: number;
|
|
48
|
+
top: number;
|
|
49
|
+
width: number;
|
|
50
|
+
}>;
|
|
51
|
+
clientPoint: Readonly<{
|
|
52
|
+
x: number;
|
|
53
|
+
y: number;
|
|
54
|
+
}>;
|
|
55
|
+
virtualViewport: Readonly<VirtualViewportSize>;
|
|
56
|
+
}>): {
|
|
57
|
+
x: number;
|
|
58
|
+
y: number;
|
|
59
|
+
} | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* Creates Pixi options that preserve visual density when a virtual viewport is scaled.
|
|
62
|
+
*
|
|
63
|
+
* @category Util
|
|
64
|
+
*/
|
|
65
|
+
export declare function createVirtualViewportPixiOptions(): {
|
|
66
|
+
autoDensity: boolean;
|
|
67
|
+
resolution: number;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* A pre-built mod that scales an Antha UI and Pixi canvas to a logical viewport width.
|
|
71
|
+
*
|
|
72
|
+
* @category Pre-Built Mods
|
|
73
|
+
*/
|
|
74
|
+
export declare function createAnthaVirtualViewportMod({ virtualWidth, }: Readonly<{
|
|
75
|
+
/**
|
|
76
|
+
* The reference width for your game screen. Any browser window wider or skinnier than this will
|
|
77
|
+
* be scaled.
|
|
78
|
+
*/
|
|
79
|
+
virtualWidth: number;
|
|
80
|
+
}>): import("@antha/engine").AnthaMod<NoInfer<AnthaVirtualViewportModState>>;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { defineAnthaMod } from '@antha/engine';
|
|
2
|
+
/**
|
|
3
|
+
* Calculates the logical viewport for a physical screen size.
|
|
4
|
+
*
|
|
5
|
+
* @category Util
|
|
6
|
+
*/
|
|
7
|
+
export function calculateVirtualViewport({ screenSize, virtualWidth, }) {
|
|
8
|
+
if (!screenSize.width || !virtualWidth) {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
const scale = screenSize.width / virtualWidth;
|
|
12
|
+
return {
|
|
13
|
+
height: screenSize.height / scale,
|
|
14
|
+
scale,
|
|
15
|
+
width: virtualWidth,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Maps a pointer position from canvas coordinates into a virtual viewport.
|
|
20
|
+
*
|
|
21
|
+
* @category Util
|
|
22
|
+
*/
|
|
23
|
+
export function calculateVirtualViewportPoint({ canvasBounds, clientPoint, virtualViewport, }) {
|
|
24
|
+
if (!canvasBounds.width || !canvasBounds.height) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
x: ((clientPoint.x - canvasBounds.left) / canvasBounds.width) * virtualViewport.width,
|
|
29
|
+
y: ((clientPoint.y - canvasBounds.top) / canvasBounds.height) * virtualViewport.height,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Creates Pixi options that preserve visual density when a virtual viewport is scaled.
|
|
34
|
+
*
|
|
35
|
+
* @category Util
|
|
36
|
+
*/
|
|
37
|
+
export function createVirtualViewportPixiOptions() {
|
|
38
|
+
return {
|
|
39
|
+
autoDensity: true,
|
|
40
|
+
resolution: globalThis.devicePixelRatio || 1,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function hasSameVirtualViewport({ previousVirtualViewport, virtualViewport, }) {
|
|
44
|
+
return (previousVirtualViewport != undefined &&
|
|
45
|
+
Math.abs(previousVirtualViewport.height - virtualViewport.height) < 0.01 &&
|
|
46
|
+
Math.abs(previousVirtualViewport.scale - virtualViewport.scale) < 0.0001 &&
|
|
47
|
+
previousVirtualViewport.width === virtualViewport.width);
|
|
48
|
+
}
|
|
49
|
+
function updateVirtualViewportHostElement({ hostElement, virtualViewport, }) {
|
|
50
|
+
hostElement.style.height = `${100 / virtualViewport.scale}%`;
|
|
51
|
+
hostElement.style.transform = `scale(${virtualViewport.scale})`;
|
|
52
|
+
hostElement.style.transformOrigin = 'top left';
|
|
53
|
+
hostElement.style.width = `${100 / virtualViewport.scale}%`;
|
|
54
|
+
}
|
|
55
|
+
function resetVirtualViewportHostElement({ hostElement, }) {
|
|
56
|
+
hostElement.style.removeProperty('height');
|
|
57
|
+
hostElement.style.removeProperty('transform');
|
|
58
|
+
hostElement.style.removeProperty('transform-origin');
|
|
59
|
+
hostElement.style.removeProperty('width');
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* A pre-built mod that scales an Antha UI and Pixi canvas to a logical viewport width.
|
|
63
|
+
*
|
|
64
|
+
* @category Pre-Built Mods
|
|
65
|
+
*/
|
|
66
|
+
export function createAnthaVirtualViewportMod({ virtualWidth, }) {
|
|
67
|
+
return defineAnthaMod({
|
|
68
|
+
modName: 'antha-virtual-viewport',
|
|
69
|
+
cleanup({ hostElement, state }) {
|
|
70
|
+
resetVirtualViewportHostElement({
|
|
71
|
+
hostElement,
|
|
72
|
+
});
|
|
73
|
+
state.virtualViewport = undefined;
|
|
74
|
+
},
|
|
75
|
+
execute({ hostElement, state }) {
|
|
76
|
+
const virtualViewport = calculateVirtualViewport({
|
|
77
|
+
screenSize: hostElement.getBoundingClientRect(),
|
|
78
|
+
virtualWidth,
|
|
79
|
+
});
|
|
80
|
+
if (!virtualViewport) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const hasViewportChanged = !hasSameVirtualViewport({
|
|
84
|
+
previousVirtualViewport: state.virtualViewport,
|
|
85
|
+
virtualViewport,
|
|
86
|
+
}) || hostElement.style.transform !== `scale(${virtualViewport.scale})`;
|
|
87
|
+
if (hasViewportChanged) {
|
|
88
|
+
updateVirtualViewportHostElement({
|
|
89
|
+
hostElement,
|
|
90
|
+
virtualViewport,
|
|
91
|
+
});
|
|
92
|
+
state.virtualViewport = virtualViewport;
|
|
93
|
+
}
|
|
94
|
+
const pixiApplication = state.pixi?.pixiApplication;
|
|
95
|
+
if (!pixiApplication ||
|
|
96
|
+
(!hasViewportChanged &&
|
|
97
|
+
pixiApplication.renderer.resolution ===
|
|
98
|
+
(globalThis.devicePixelRatio || 1) * virtualViewport.scale &&
|
|
99
|
+
pixiApplication.stage.scale.x === 1 &&
|
|
100
|
+
pixiApplication.stage.scale.y === 1)) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
pixiApplication.renderer.resolution =
|
|
104
|
+
(globalThis.devicePixelRatio || 1) * virtualViewport.scale;
|
|
105
|
+
pixiApplication.resize();
|
|
106
|
+
pixiApplication.stage.scale.set(1);
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antha/graphics-2d",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "An Antha mod for rendering 2D graphics to a canvas.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"vir",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"compile": "virmator compile",
|
|
33
33
|
"docs": "virmator docs",
|
|
34
34
|
"test": "virmator test web",
|
|
35
|
-
"test:coverage": "npm test coverage",
|
|
35
|
+
"test:coverage": "NODE_OPTIONS=--import=tsx npm test coverage",
|
|
36
36
|
"test:docs": "virmator docs check"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
@@ -40,19 +40,19 @@
|
|
|
40
40
|
"@augment-vir/common": "^32.2.3"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@antha/engine": "^0.
|
|
44
|
-
"@antha/web-test-runner-plugin-pixi": "^0.
|
|
43
|
+
"@antha/engine": "^0.9.0",
|
|
44
|
+
"@antha/web-test-runner-plugin-pixi": "^0.9.0",
|
|
45
45
|
"@augment-vir/test": "^32.2.3",
|
|
46
46
|
"@electrovir/color": "^1.7.11",
|
|
47
47
|
"@web/dev-server-esbuild": "^2.0.0",
|
|
48
48
|
"@web/test-runner": "^1.0.0",
|
|
49
49
|
"@web/test-runner-playwright": "^1.0.0",
|
|
50
50
|
"istanbul-smart-text-reporter": "^1.1.5",
|
|
51
|
-
"pixi.js": "^8.
|
|
52
|
-
"vira": "31.31.
|
|
51
|
+
"pixi.js": "^8.20.0",
|
|
52
|
+
"vira": "31.31.6"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
|
-
"@antha/engine": "^0.
|
|
55
|
+
"@antha/engine": "^0.9.0",
|
|
56
56
|
"@electrovir/color": ">=1",
|
|
57
57
|
"element-vir": ">=26",
|
|
58
58
|
"pixi.js": ">=8",
|