@inglorious/renderer-2d 0.8.0 → 0.8.1
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.
- package/package.json +5 -5
- package/src/coordinates.js +33 -0
- package/src/rendering-behavior.js +16 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/renderer-2d",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "A renderer for the Inglorious Engine, using the Context2D of the Canvas API.",
|
|
5
5
|
"author": "IceOnFire <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
|
|
6
6
|
"license": "MIT",
|
|
@@ -24,12 +24,12 @@
|
|
|
24
24
|
"access": "public"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@inglorious/engine": "2.1.
|
|
28
|
-
"@inglorious/utils": "3.5.
|
|
27
|
+
"@inglorious/engine": "2.1.1",
|
|
28
|
+
"@inglorious/utils": "3.5.1"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@inglorious/engine": "2.1.
|
|
32
|
-
"@inglorious/utils": "3.5.
|
|
31
|
+
"@inglorious/engine": "2.1.1",
|
|
32
|
+
"@inglorious/utils": "3.5.1"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"eslint": "^9.34.0",
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { v } from "@inglorious/utils/v.js"
|
|
2
|
+
|
|
3
|
+
const NO_Y = 0
|
|
4
|
+
const HALF = 2
|
|
5
|
+
|
|
6
|
+
export function createCoordinateConverter(canvas, api) {
|
|
7
|
+
return (clientX, clientY) => {
|
|
8
|
+
const {
|
|
9
|
+
left,
|
|
10
|
+
bottom,
|
|
11
|
+
width: canvasWidth,
|
|
12
|
+
height: canvasHeight,
|
|
13
|
+
} = canvas.getBoundingClientRect()
|
|
14
|
+
|
|
15
|
+
const x = clientX - left
|
|
16
|
+
const y = bottom - clientY
|
|
17
|
+
|
|
18
|
+
const game = api.getEntity("game")
|
|
19
|
+
const [gameWidth, gameHeight] = game.size
|
|
20
|
+
|
|
21
|
+
const scaleX = canvasWidth / gameWidth
|
|
22
|
+
const scaleY = canvasHeight / gameHeight
|
|
23
|
+
const scale = Math.min(scaleX, scaleY)
|
|
24
|
+
|
|
25
|
+
const scaledGameWidth = gameWidth * scale
|
|
26
|
+
const scaledGameHeight = gameHeight * scale
|
|
27
|
+
|
|
28
|
+
const offsetX = (canvasWidth - scaledGameWidth) / HALF
|
|
29
|
+
const offsetY = (canvasHeight - scaledGameHeight) / HALF
|
|
30
|
+
|
|
31
|
+
return v((x - offsetX) / scale, NO_Y, (y - offsetY) / scale)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { trackMouse } from "@inglorious/engine/behaviors/input/mouse.js"
|
|
2
2
|
import { trackTouch } from "@inglorious/engine/behaviors/input/touch.js"
|
|
3
3
|
|
|
4
|
+
import { createCoordinateConverter } from "./coordinates.js"
|
|
5
|
+
|
|
4
6
|
const ORIGIN = 0
|
|
5
7
|
const HALF = 2
|
|
6
8
|
|
|
7
9
|
export function rendering(canvas) {
|
|
10
|
+
canvas.style.touchAction = "none"
|
|
11
|
+
|
|
8
12
|
const ctx = canvas.getContext("2d")
|
|
9
13
|
|
|
10
14
|
let _onMouseMove = null
|
|
@@ -49,7 +53,13 @@ export function rendering(canvas) {
|
|
|
49
53
|
ctx.imageSmoothingEnabled = false
|
|
50
54
|
}
|
|
51
55
|
|
|
52
|
-
const
|
|
56
|
+
const toGamePosition = createCoordinateConverter(canvas, api)
|
|
57
|
+
|
|
58
|
+
const { onMouseMove, onClick, onWheel } = trackMouse(
|
|
59
|
+
canvas,
|
|
60
|
+
api,
|
|
61
|
+
toGamePosition,
|
|
62
|
+
)
|
|
53
63
|
_onMouseMove = onMouseMove
|
|
54
64
|
_onClick = onClick
|
|
55
65
|
_onWheel = onWheel
|
|
@@ -58,7 +68,11 @@ export function rendering(canvas) {
|
|
|
58
68
|
canvas.addEventListener("click", _onClick)
|
|
59
69
|
canvas.addEventListener("wheel", _onWheel)
|
|
60
70
|
|
|
61
|
-
const { onTouchStart, onTouchMove, onTouchEnd } = trackTouch(
|
|
71
|
+
const { onTouchStart, onTouchMove, onTouchEnd } = trackTouch(
|
|
72
|
+
canvas,
|
|
73
|
+
api,
|
|
74
|
+
toGamePosition,
|
|
75
|
+
)
|
|
62
76
|
_onTouchStart = onTouchStart
|
|
63
77
|
_onTouchMove = onTouchMove
|
|
64
78
|
_onTouchEnd = onTouchEnd
|