Blend modes

Blend modes were not used at the end, because keying the background of a sprite with chroma key turned out to be impossible, since the blender render creates sprites with black half transparent pixels, which would be impossible to key out later by image processing.

This is sub-layout for documentation pages

This was last updated when game had version: dev - 2.0.2
Top

1 Introduction

Blend modes allow to digitally edit an image. Here, we are interested mainly in image tinting, but it can be used for whatever effect.

2 Pixel manipulation

We are using HTML canvas to draw the images. Unfortunately, there does not seem to be a way of getting image pixel data other than using getImageData function
The following code allows to get pixel data of a rectangle

    var data: ImageData = ctx.getImageData(x, y, image.naturalWidth, image.naturalHeight);
  
It extracts the actual pixels of the canvas.
putImageData function allows to set pixel data back to the canvas
    ctx.putImageData(data, x, y);
  

Note: Unless there is some more clever way of pixel manipulation built in the canvas and draw context JS system, then I bet that this approach is used also for general texture drawing


2.1 Processing

Suppose, that target texture of size \(300, 100\) is supposed to be drawn with a blend mode.


A texture we want to draw with a blend mode


We first extract the pixels from the canvas on the target rectangle, where the texture will be drawn

Visualization of extracting pixel data


Then we draw the wanted image into the canvas (which does not matter, because we have the pixel data backuped)

A texture we want to draw with a blend mode


We extract the drawn pixels again to get them to the memory Then, we can perform whatever blend operation with the two rectangles full of pixel data, and finally, we put the blended result back to the canvas

3 Functions


3.1 Multiply
\(e=a*b\)
3.2 Overlay
\(e=\frac{a}{255} * (a+ \frac{2*b} {255} * (255 - a))\)