Skip to content

Commit 59f3312

Browse files
committed
feat: adjust canvas texture dimensions to improve performance [#1]
1 parent 6de97fb commit 59f3312

3 files changed

Lines changed: 22 additions & 20 deletions

File tree

src/core/app/main.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,6 @@ export default class Main {
292292
this.rendererManager,
293293
new PointerCanvas(
294294
this.debugManager,
295-
this.#imageManager.currentImage.resolution.width,
296-
this.#imageManager.currentImage.resolution.height,
297295
this.#config.pointer?.src ?? pointerImage,
298296
this.#config.pointer?.size ?? 0.15,
299297
this.#config.pointer?.trailing?.factor ?? 0.01,

src/core/app/main/runner/pointer/canvas.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@
99
*/
1010
import * as THREE from 'three'
1111
import DebugManager from '../../../../services/debug-manager.js'
12+
import {CANVAS_TEXTURE_RESOLUTION} from '../../../../etc/config.ts'
1213

1314
export default class Canvas {
14-
/**
15-
* @type {DebugManager}
16-
*/
17-
#debugManager: DebugManager
18-
1915
/**
2016
* @type {THREE.CanvasTexture}
2117
*/
@@ -26,6 +22,11 @@ export default class Canvas {
2622
*/
2723
element: HTMLCanvasElement
2824

25+
/**
26+
* @type {DebugManager}
27+
*/
28+
#debugManager: DebugManager
29+
2930
/**
3031
* @type {CanvasRenderingContext2D}
3132
*/
@@ -60,23 +61,19 @@ export default class Canvas {
6061
* Constructor
6162
*
6263
* @param {DebugManager} debugManager
63-
* @param {number} resolutionWidth
64-
* @param {number} resolutionHeight
6564
* @param {string} pointerImageSrc
6665
* @param {number} pointerImageSize
6766
* @param {number} pointerTrailingFactor
6867
*/
6968
constructor(
7069
debugManager: DebugManager,
71-
resolutionWidth: number,
72-
resolutionHeight: number,
7370
pointerImageSrc: string,
7471
pointerImageSize: number = 0.1,
7572
pointerTrailingFactor: number = 0.05,
7673
) {
7774
this.#debugManager = debugManager
7875
this.#pointerTrailingFactor = pointerTrailingFactor
79-
this.#initCanvasTexture(resolutionWidth, resolutionHeight)
76+
this.#initCanvasTexture()
8077
this.#initPointerImage(pointerImageSrc, pointerImageSize)
8178
}
8279

@@ -206,17 +203,19 @@ export default class Canvas {
206203
* Init canvas texture used to detect pointer location and
207204
* displace points
208205
*
209-
* @param {number} resolutionWidth
210-
* @param {number} resolutionHeight
211206
* @returns {void}
212-
* @note The canvas will have the same number of pixels as the
213-
* image.
214-
* That is why it is used the image resolution as its dimensions
207+
* @note This canvas will work as a texture, but it is not necessary
208+
* to use 1 to 1 map between renderer/image pixels and
209+
* it.
210+
* It is considered that a low-resolution texture
211+
* improves performance without affecting the final effect
215212
*/
216-
#initCanvasTexture(resolutionWidth: number, resolutionHeight: number): void {
213+
#initCanvasTexture(): void {
217214
this.element = document.createElement('canvas')
218-
this.element.width = resolutionWidth
219-
this.element.height = resolutionHeight
215+
this.element.style.width = `${CANVAS_TEXTURE_RESOLUTION}px`
216+
this.element.style.height = `${CANVAS_TEXTURE_RESOLUTION}px`
217+
this.element.width = CANVAS_TEXTURE_RESOLUTION
218+
this.element.height = CANVAS_TEXTURE_RESOLUTION
220219
this.texture = new THREE.CanvasTexture(this.element)
221220

222221
this.#context = this.element.getContext('2d') as CanvasRenderingContext2D

src/core/etc/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* @description Config
3+
* @author C. M. de Picciotto <d3p1@d3p1.dev> (https://d3p1.dev/)
4+
*/
5+
export const CANVAS_TEXTURE_RESOLUTION = 128

0 commit comments

Comments
 (0)