orbit/js/utils/Textures.js

20 lines
714 B
JavaScript

/**
* Generate small canvas textures (scanlines, vignette, glow, …) and
* register them with the scene's TextureManager.
*
* Cached by key: a second call with the same key reuses the existing
* texture, so multiple overlays/scenes share one.
*
* canvasTexture(scene, '__glow_cyan', 256, 256, (ctx, w, h) => { ... });
*/
export function canvasTexture(scene, key, w, h, draw) {
if (scene.textures.exists(key)) return key;
const canvas = document.createElement('canvas');
canvas.width = Math.max(1, Math.round(w));
canvas.height = Math.max(1, Math.round(h));
const ctx = canvas.getContext('2d');
draw(ctx, canvas.width, canvas.height);
scene.textures.addCanvas(key, canvas);
return key;
}