@inglorious/renderer-2d 0.6.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/renderer-2d",
3
- "version": "0.6.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,13 +24,18 @@
24
24
  "access": "public"
25
25
  },
26
26
  "dependencies": {
27
- "@inglorious/engine": "0.11.0",
28
- "@inglorious/utils": "3.0.0"
27
+ "@inglorious/engine": "2.1.0",
28
+ "@inglorious/utils": "3.5.0"
29
+ },
30
+ "peerDependencies": {
31
+ "@inglorious/engine": "2.1.0",
32
+ "@inglorious/utils": "3.5.0"
29
33
  },
30
34
  "devDependencies": {
31
35
  "eslint": "^9.34.0",
32
36
  "prettier": "^3.6.2",
33
- "vitest": "^1.6.0"
37
+ "vitest": "^1.6.0",
38
+ "@inglorious/eslint-config": "1.0.0"
34
39
  },
35
40
  "scripts": {
36
41
  "format": "prettier --write '**/*.{js,jsx}'",
package/src/fps.js CHANGED
@@ -7,7 +7,7 @@ export function renderFps(entity, ctx) {
7
7
 
8
8
  renderText(
9
9
  {
10
- ...entity.dt,
10
+ ...entity,
11
11
  value: `FPS: ${(ONE_SECOND / value).toFixed(accuracy)}`,
12
12
  },
13
13
  ctx,
@@ -1,4 +1,5 @@
1
- import { track } from "@inglorious/engine/behaviors/input/mouse.js"
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 = (canvasWidth - scaledGameWidth) / HALF
28
- const offsetY = (canvasHeight - scaledGameHeight) / HALF
36
+ const offsetX = (canvas.width - scaledGameWidth * dpi) / HALF
37
+ const offsetY = (canvas.height - scaledGameHeight * dpi) / HALF
29
38
 
30
- const dpi = window.devicePixelRatio
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, canvasWidth, canvasHeight)
41
+ ctx.fillRect(ORIGIN, ORIGIN, canvas.width, canvas.height)
35
42
 
36
43
  ctx.translate(offsetX, offsetY)
37
- ctx.scale(dpi * scale, dpi * 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 } = track(canvas, api)
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,14 +1,26 @@
1
1
  const DEFAULT_SIZE = 16
2
- const DEFAULT_PADDING = 10
2
+ const DEFAULT_PADDING = 0
3
3
 
4
4
  export function renderText(entity, ctx) {
5
- const { size = DEFAULT_SIZE, value = "" } = entity
5
+ const {
6
+ size = DEFAULT_SIZE,
7
+ lineHeight = size,
8
+ color = "black",
9
+ font = "sans-serif",
10
+ textAlign = "left",
11
+ value = "",
12
+ } = entity
6
13
 
7
14
  ctx.save()
8
15
 
9
- ctx.font = `${size}px sans serif`
10
- ctx.fillStyle = "black"
11
- ctx.fillText(value, DEFAULT_PADDING, DEFAULT_PADDING + size)
16
+ ctx.font = `${size}px ${font}`
17
+ ctx.fillStyle = color
18
+ ctx.textAlign = textAlign
19
+
20
+ const tokens = value.split("\n")
21
+ tokens.forEach((token, index) => {
22
+ ctx.fillText(token, DEFAULT_PADDING, size + lineHeight * index)
23
+ })
12
24
 
13
25
  ctx.restore()
14
26
  }