@inglorious/renderer-2d 0.4.0 → 0.6.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.4.0",
3
+ "version": "0.6.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,8 +24,8 @@
24
24
  "access": "public"
25
25
  },
26
26
  "dependencies": {
27
- "@inglorious/engine": "0.9.0",
28
- "@inglorious/utils": "2.1.0"
27
+ "@inglorious/engine": "0.11.0",
28
+ "@inglorious/utils": "3.0.0"
29
29
  },
30
30
  "devDependencies": {
31
31
  "eslint": "^9.34.0",
@@ -1,4 +1,4 @@
1
- import { snap, zero } from "@inglorious/utils/math/linear-algebra/vector.js"
1
+ import { snap, zero } from "@inglorious/utils/math/vector.js"
2
2
 
3
3
  export function absolutePosition(render) {
4
4
  return (entity, ctx, { api }) => {
@@ -6,11 +6,11 @@ export function absolutePosition(render) {
6
6
  const [x, y, z] = snap(position)
7
7
 
8
8
  const game = api.getEntity("game")
9
- const [, , , screenHeight] = game.bounds
9
+ const [, gameHeight] = game.size
10
10
 
11
11
  ctx.save()
12
12
 
13
- ctx.translate(x, screenHeight - y - z)
13
+ ctx.translate(x, gameHeight - y - z)
14
14
  render(entity, ctx, api)
15
15
 
16
16
  ctx.restore()
@@ -1,5 +1,8 @@
1
1
  import { track } from "@inglorious/engine/behaviors/input/mouse.js"
2
2
 
3
+ const ORIGIN = 0
4
+ const HALF = 2
5
+
3
6
  export function rendering(canvas) {
4
7
  const ctx = canvas.getContext("2d")
5
8
 
@@ -10,14 +13,28 @@ export function rendering(canvas) {
10
13
  return {
11
14
  init(entity, event, api) {
12
15
  const game = api.getEntity("game")
13
- const [, , width, height] = game.bounds
16
+ const [gameWidth, gameHeight] = game.size
17
+
18
+ const canvasWidth = canvas.width
19
+ const canvasHeight = canvas.height
20
+
21
+ const scaleX = canvasWidth / gameWidth
22
+ const scaleY = canvasHeight / gameHeight
23
+ const scale = Math.min(scaleX, scaleY)
24
+ const scaledGameWidth = gameWidth * scale
25
+ const scaledGameHeight = gameHeight * scale
26
+
27
+ const offsetX = (canvasWidth - scaledGameWidth) / HALF
28
+ const offsetY = (canvasHeight - scaledGameHeight) / HALF
14
29
 
15
- canvas.style.width = `${width}px`
16
- canvas.style.height = `${height}px`
17
30
  const dpi = window.devicePixelRatio
18
- canvas.width = width * dpi
19
- canvas.height = height * dpi
20
- ctx.scale(dpi, dpi)
31
+
32
+ ctx.clearRect(ORIGIN, ORIGIN, canvasWidth, canvasHeight)
33
+ ctx.fillStyle = "black"
34
+ ctx.fillRect(ORIGIN, ORIGIN, canvasWidth, canvasHeight)
35
+
36
+ ctx.translate(offsetX, offsetY)
37
+ ctx.scale(dpi * scale, dpi * scale)
21
38
 
22
39
  if (game.pixelated) {
23
40
  canvas.style.imageRendering = "pixelated"
@@ -35,7 +52,9 @@ export function rendering(canvas) {
35
52
  canvas.addEventListener("wheel", _onWheel)
36
53
  },
37
54
 
38
- destroy() {
55
+ destroy(entity, id) {
56
+ if (id !== entity.id) return
57
+
39
58
  if (_onMouseMove) {
40
59
  canvas.removeEventListener("mousemove", _onMouseMove)
41
60
  }
@@ -1,4 +1,4 @@
1
- import { to2D } from "@inglorious/utils/math/linear-algebra/2d.js"
1
+ import { to2D } from "@inglorious/utils/math/vector.js"
2
2
 
3
3
  import { absolutePosition } from "./absolute-position.js"
4
4
 
@@ -19,9 +19,9 @@ export function renderingSystem(canvas) {
19
19
  const { game, ...worldEntities } = state.entities
20
20
 
21
21
  // 1. Clear canvas
22
- const [, , width, height] = game.bounds
22
+ const [gameWidth, gameHeight] = game.size
23
23
  ctx.fillStyle = game.backgroundColor || "lightgrey"
24
- ctx.fillRect(ORIGIN, ORIGIN, width, height)
24
+ ctx.fillRect(ORIGIN, ORIGIN, gameWidth, gameHeight)
25
25
 
26
26
  // 2. Find active camera
27
27
  const camera = Object.values(state.entities).find(
@@ -37,11 +37,11 @@ export function renderingSystem(canvas) {
37
37
 
38
38
  // Center the view on the camera and apply zoom.
39
39
  // The order of operations is crucial here.
40
- ctx.translate(width / HALF, height / HALF)
40
+ ctx.translate(gameWidth / HALF, gameHeight / HALF)
41
41
  ctx.scale(zoom, zoom)
42
42
  // This vertical translation compensates for the coordinate system flip
43
43
  // that happens inside the absolutePosition decorator.
44
- ctx.translate(ORIGIN, -height)
44
+ ctx.translate(ORIGIN, -gameHeight)
45
45
  // This translation moves the world relative to the camera.
46
46
  ctx.translate(-cameraX, cameraZ)
47
47
  }
@@ -1,7 +1,7 @@
1
1
  /* eslint-disable no-magic-numbers */
2
2
 
3
- import { zero } from "@inglorious/utils/math/linear-algebra/vector.js"
4
3
  import { pi } from "@inglorious/utils/math/trigonometry.js"
4
+ import { zero } from "@inglorious/utils/math/vector.js"
5
5
 
6
6
  export function renderCircle(entity, ctx) {
7
7
  const {
@@ -1,6 +1,6 @@
1
1
  /* eslint-disable no-magic-numbers */
2
2
 
3
- import { zero } from "@inglorious/utils/math/linear-algebra/vector.js"
3
+ import { zero } from "@inglorious/utils/math/vector.js"
4
4
 
5
5
  export function renderRectangle(entity, ctx) {
6
6
  const {