mwg API
    Preparing search index...

    Function withTextureCanvas

    • Opens a canvas the size of texture, draws it in, and hands the 2D context to paint to make whatever further edits it needs (a palette remap, a composited overlay, a rewritten alpha channel); the result becomes the returned Texture. Shared by every texture-level image modifier (recolorTexture here, ~BLIT/~MASK/~BLEND in ImageModifiers.ts) so each one only has to write its own pixel or compositing logic, not the canvas bookkeeping. Returns texture unchanged if there is no usable 2D canvas.

      Parameters

      Returns Texture

      import { withTextureCanvas } from '@datamoc/mw_games/two-d/render';
      import type { Texture2D } from '@datamoc/mw_games/two-d/render';

      declare const texture: Texture2D;

      // a minimal custom modifier: invert every pixel's RGB, alpha untouched
      const inverted = withTextureCanvas(texture, {}, (context, width, height) => {
      const { data } = context.getImageData(0, 0, width, height);
      for (let i = 0; i < data.length; i += 4) {
      data[i] = 255 - data[i];
      data[i + 1] = 255 - data[i + 1];
      data[i + 2] = 255 - data[i + 2];
      }
      context.putImageData({ data, width, height }, 0, 0);
      });