@inglorious/renderer-2d 0.7.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 +49 -9
- package/src/text.js +7 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/renderer-2d",
|
|
3
|
-
"version": "0.
|
|
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/
|
|
28
|
-
"@inglorious/
|
|
27
|
+
"@inglorious/engine": "2.1.1",
|
|
28
|
+
"@inglorious/utils": "3.5.1"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@inglorious/
|
|
32
|
-
"@inglorious/
|
|
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,15 +1,24 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { trackMouse } from "@inglorious/engine/behaviors/input/mouse.js"
|
|
2
|
+
import { trackTouch } from "@inglorious/engine/behaviors/input/touch.js"
|
|
3
|
+
|
|
4
|
+
import { createCoordinateConverter } from "./coordinates.js"
|
|
2
5
|
|
|
3
6
|
const ORIGIN = 0
|
|
4
7
|
const HALF = 2
|
|
5
8
|
|
|
6
9
|
export function rendering(canvas) {
|
|
10
|
+
canvas.style.touchAction = "none"
|
|
11
|
+
|
|
7
12
|
const ctx = canvas.getContext("2d")
|
|
8
13
|
|
|
9
14
|
let _onMouseMove = null
|
|
10
15
|
let _onClick = null
|
|
11
16
|
let _onWheel = null
|
|
12
17
|
|
|
18
|
+
let _onTouchStart = null
|
|
19
|
+
let _onTouchMove = null
|
|
20
|
+
let _onTouchEnd = null
|
|
21
|
+
|
|
13
22
|
return {
|
|
14
23
|
init(entity, event, api) {
|
|
15
24
|
const game = api.getEntity("game")
|
|
@@ -17,6 +26,10 @@ export function rendering(canvas) {
|
|
|
17
26
|
|
|
18
27
|
const canvasWidth = canvas.width
|
|
19
28
|
const canvasHeight = canvas.height
|
|
29
|
+
const dpi = window.devicePixelRatio
|
|
30
|
+
|
|
31
|
+
canvas.width = canvasWidth * dpi
|
|
32
|
+
canvas.height = canvasHeight * dpi
|
|
20
33
|
|
|
21
34
|
const scaleX = canvasWidth / gameWidth
|
|
22
35
|
const scaleY = canvasHeight / gameHeight
|
|
@@ -24,17 +37,15 @@ export function rendering(canvas) {
|
|
|
24
37
|
const scaledGameWidth = gameWidth * scale
|
|
25
38
|
const scaledGameHeight = gameHeight * scale
|
|
26
39
|
|
|
27
|
-
const offsetX = (
|
|
28
|
-
const offsetY = (
|
|
29
|
-
|
|
30
|
-
const dpi = window.devicePixelRatio
|
|
40
|
+
const offsetX = (canvas.width - scaledGameWidth * dpi) / HALF
|
|
41
|
+
const offsetY = (canvas.height - scaledGameHeight * dpi) / HALF
|
|
31
42
|
|
|
32
|
-
ctx.clearRect(ORIGIN, ORIGIN,
|
|
43
|
+
ctx.clearRect(ORIGIN, ORIGIN, canvas.width, canvas.height)
|
|
33
44
|
ctx.fillStyle = "black"
|
|
34
|
-
ctx.fillRect(ORIGIN, ORIGIN,
|
|
45
|
+
ctx.fillRect(ORIGIN, ORIGIN, canvas.width, canvas.height)
|
|
35
46
|
|
|
36
47
|
ctx.translate(offsetX, offsetY)
|
|
37
|
-
ctx.scale(
|
|
48
|
+
ctx.scale(scale * dpi, scale * dpi)
|
|
38
49
|
|
|
39
50
|
if (game.pixelated) {
|
|
40
51
|
canvas.style.imageRendering = "pixelated"
|
|
@@ -42,7 +53,13 @@ export function rendering(canvas) {
|
|
|
42
53
|
ctx.imageSmoothingEnabled = false
|
|
43
54
|
}
|
|
44
55
|
|
|
45
|
-
const
|
|
56
|
+
const toGamePosition = createCoordinateConverter(canvas, api)
|
|
57
|
+
|
|
58
|
+
const { onMouseMove, onClick, onWheel } = trackMouse(
|
|
59
|
+
canvas,
|
|
60
|
+
api,
|
|
61
|
+
toGamePosition,
|
|
62
|
+
)
|
|
46
63
|
_onMouseMove = onMouseMove
|
|
47
64
|
_onClick = onClick
|
|
48
65
|
_onWheel = onWheel
|
|
@@ -50,6 +67,19 @@ export function rendering(canvas) {
|
|
|
50
67
|
canvas.addEventListener("mousemove", _onMouseMove)
|
|
51
68
|
canvas.addEventListener("click", _onClick)
|
|
52
69
|
canvas.addEventListener("wheel", _onWheel)
|
|
70
|
+
|
|
71
|
+
const { onTouchStart, onTouchMove, onTouchEnd } = trackTouch(
|
|
72
|
+
canvas,
|
|
73
|
+
api,
|
|
74
|
+
toGamePosition,
|
|
75
|
+
)
|
|
76
|
+
_onTouchStart = onTouchStart
|
|
77
|
+
_onTouchMove = onTouchMove
|
|
78
|
+
_onTouchEnd = onTouchEnd
|
|
79
|
+
|
|
80
|
+
canvas.addEventListener("touchstart", _onTouchStart)
|
|
81
|
+
canvas.addEventListener("touchmove", _onTouchMove)
|
|
82
|
+
canvas.addEventListener("touchend", _onTouchEnd)
|
|
53
83
|
},
|
|
54
84
|
|
|
55
85
|
destroy(entity, id) {
|
|
@@ -64,6 +94,16 @@ export function rendering(canvas) {
|
|
|
64
94
|
if (_onWheel) {
|
|
65
95
|
canvas.removeEventListener("wheel", _onWheel)
|
|
66
96
|
}
|
|
97
|
+
|
|
98
|
+
if (_onTouchStart) {
|
|
99
|
+
canvas.removeEventListener("touchstart", _onTouchStart)
|
|
100
|
+
}
|
|
101
|
+
if (_onTouchMove) {
|
|
102
|
+
canvas.removeEventListener("touchmove", _onTouchMove)
|
|
103
|
+
}
|
|
104
|
+
if (_onTouchEnd) {
|
|
105
|
+
canvas.removeEventListener("touchend", _onTouchEnd)
|
|
106
|
+
}
|
|
67
107
|
},
|
|
68
108
|
}
|
|
69
109
|
}
|
package/src/text.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
const DEFAULT_SIZE = 16
|
|
2
|
-
const DEFAULT_PADDING =
|
|
2
|
+
const DEFAULT_PADDING = 0
|
|
3
3
|
|
|
4
4
|
export function renderText(entity, ctx) {
|
|
5
5
|
const {
|
|
6
6
|
size = DEFAULT_SIZE,
|
|
7
|
-
|
|
7
|
+
lineHeight = size,
|
|
8
8
|
color = "black",
|
|
9
9
|
font = "sans-serif",
|
|
10
10
|
textAlign = "left",
|
|
@@ -16,7 +16,11 @@ export function renderText(entity, ctx) {
|
|
|
16
16
|
ctx.font = `${size}px ${font}`
|
|
17
17
|
ctx.fillStyle = color
|
|
18
18
|
ctx.textAlign = textAlign
|
|
19
|
-
|
|
19
|
+
|
|
20
|
+
const tokens = value.split("\n")
|
|
21
|
+
tokens.forEach((token, index) => {
|
|
22
|
+
ctx.fillText(token, DEFAULT_PADDING, size + lineHeight * index)
|
|
23
|
+
})
|
|
20
24
|
|
|
21
25
|
ctx.restore()
|
|
22
26
|
}
|