I tried to make this happen with canvas' globalCompositeOperation
, but had no luck so I'm asking here. There are similar questions here, but I did not find my case among them.
I have layers in my canvas area as so (drawing order from bottom to top):
- The canvas base is filled with pure white (#fff, with fillRect)
- First image
house
is a picture of a house. The background is transparent. (see below) - Second image
roofOverlay
is an overlay "masking" image that has the roof area coloured red (can be anything, but red for clarity, see below)
Both images take up the whole canvas and are lined up perfectly on top of each other, so that the red roof area matches the house.
I then have a repeating background repeatPattern
pattern what I want to use ONLY inside the red areas: to fill the red area with repeatPattern
. (can be anything, but assume hexagons or whatever)
In pseudocode, this would ideally be something in the lines of:
roofOverlay.maskBackground(repeatPattern)
(On a sidenote, I would also like to be able to mess with the background pattern HSL-values, but I think that's quite straightforward once I get the pattern to even display)
Expected result:
The expected result would be a house which roof is textured with the repeatPattern image.
Note: I'm aware of clipping paths with masks, but I cannot use them here. The example is simplified and drawing all the paths for multiple different houses would be way too much work. I only have the overlayed png-files for the roof.
Images for reference
house
roofOverlay