@inglorious/renderer-2d 0.7.0 → 0.8.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.
- package/package.json +5 -5
- package/src/rendering-behavior.js +35 -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.0",
|
|
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.0",
|
|
28
|
+
"@inglorious/utils": "3.5.0"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@inglorious/
|
|
32
|
-
"@inglorious/
|
|
31
|
+
"@inglorious/engine": "2.1.0",
|
|
32
|
+
"@inglorious/utils": "3.5.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"eslint": "^9.34.0",
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { trackMouse } from "@inglorious/engine/behaviors/input/mouse.js"
|
|
2
|
+
import { trackTouch } from "@inglorious/engine/behaviors/input/touch.js"
|
|
2
3
|
|
|
3
4
|
const ORIGIN = 0
|
|
4
5
|
const HALF = 2
|
|
@@ -10,6 +11,10 @@ export function rendering(canvas) {
|
|
|
10
11
|
let _onClick = null
|
|
11
12
|
let _onWheel = null
|
|
12
13
|
|
|
14
|
+
let _onTouchStart = null
|
|
15
|
+
let _onTouchMove = null
|
|
16
|
+
let _onTouchEnd = null
|
|
17
|
+
|
|
13
18
|
return {
|
|
14
19
|
init(entity, event, api) {
|
|
15
20
|
const game = api.getEntity("game")
|
|
@@ -17,6 +22,10 @@ export function rendering(canvas) {
|
|
|
17
22
|
|
|
18
23
|
const canvasWidth = canvas.width
|
|
19
24
|
const canvasHeight = canvas.height
|
|
25
|
+
const dpi = window.devicePixelRatio
|
|
26
|
+
|
|
27
|
+
canvas.width = canvasWidth * dpi
|
|
28
|
+
canvas.height = canvasHeight * dpi
|
|
20
29
|
|
|
21
30
|
const scaleX = canvasWidth / gameWidth
|
|
22
31
|
const scaleY = canvasHeight / gameHeight
|
|
@@ -24,17 +33,15 @@ export function rendering(canvas) {
|
|
|
24
33
|
const scaledGameWidth = gameWidth * scale
|
|
25
34
|
const scaledGameHeight = gameHeight * scale
|
|
26
35
|
|
|
27
|
-
const offsetX = (
|
|
28
|
-
const offsetY = (
|
|
36
|
+
const offsetX = (canvas.width - scaledGameWidth * dpi) / HALF
|
|
37
|
+
const offsetY = (canvas.height - scaledGameHeight * dpi) / HALF
|
|
29
38
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
ctx.clearRect(ORIGIN, ORIGIN, canvasWidth, canvasHeight)
|
|
39
|
+
ctx.clearRect(ORIGIN, ORIGIN, canvas.width, canvas.height)
|
|
33
40
|
ctx.fillStyle = "black"
|
|
34
|
-
ctx.fillRect(ORIGIN, ORIGIN,
|
|
41
|
+
ctx.fillRect(ORIGIN, ORIGIN, canvas.width, canvas.height)
|
|
35
42
|
|
|
36
43
|
ctx.translate(offsetX, offsetY)
|
|
37
|
-
ctx.scale(
|
|
44
|
+
ctx.scale(scale * dpi, scale * dpi)
|
|
38
45
|
|
|
39
46
|
if (game.pixelated) {
|
|
40
47
|
canvas.style.imageRendering = "pixelated"
|
|
@@ -42,7 +49,7 @@ export function rendering(canvas) {
|
|
|
42
49
|
ctx.imageSmoothingEnabled = false
|
|
43
50
|
}
|
|
44
51
|
|
|
45
|
-
const { onMouseMove, onClick, onWheel } =
|
|
52
|
+
const { onMouseMove, onClick, onWheel } = trackMouse(canvas, api)
|
|
46
53
|
_onMouseMove = onMouseMove
|
|
47
54
|
_onClick = onClick
|
|
48
55
|
_onWheel = onWheel
|
|
@@ -50,6 +57,15 @@ export function rendering(canvas) {
|
|
|
50
57
|
canvas.addEventListener("mousemove", _onMouseMove)
|
|
51
58
|
canvas.addEventListener("click", _onClick)
|
|
52
59
|
canvas.addEventListener("wheel", _onWheel)
|
|
60
|
+
|
|
61
|
+
const { onTouchStart, onTouchMove, onTouchEnd } = trackTouch(canvas, api)
|
|
62
|
+
_onTouchStart = onTouchStart
|
|
63
|
+
_onTouchMove = onTouchMove
|
|
64
|
+
_onTouchEnd = onTouchEnd
|
|
65
|
+
|
|
66
|
+
canvas.addEventListener("touchstart", _onTouchStart)
|
|
67
|
+
canvas.addEventListener("touchmove", _onTouchMove)
|
|
68
|
+
canvas.addEventListener("touchend", _onTouchEnd)
|
|
53
69
|
},
|
|
54
70
|
|
|
55
71
|
destroy(entity, id) {
|
|
@@ -64,6 +80,16 @@ export function rendering(canvas) {
|
|
|
64
80
|
if (_onWheel) {
|
|
65
81
|
canvas.removeEventListener("wheel", _onWheel)
|
|
66
82
|
}
|
|
83
|
+
|
|
84
|
+
if (_onTouchStart) {
|
|
85
|
+
canvas.removeEventListener("touchstart", _onTouchStart)
|
|
86
|
+
}
|
|
87
|
+
if (_onTouchMove) {
|
|
88
|
+
canvas.removeEventListener("touchmove", _onTouchMove)
|
|
89
|
+
}
|
|
90
|
+
if (_onTouchEnd) {
|
|
91
|
+
canvas.removeEventListener("touchend", _onTouchEnd)
|
|
92
|
+
}
|
|
67
93
|
},
|
|
68
94
|
}
|
|
69
95
|
}
|
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
|
}
|