0

I use parent-child logic for rendering elements on canvas. I want to apply opacity to a parent, but don't know how. If I apply it by multiplying it with parent opacity, then I get CURRENT OUTCOME as seen on screenshot. I want the one with DESIRED OUTCOME. SOURCE image is the one without opacity. CURRENT OUTCOME is the one with opacity 0.5, but it should instead look like the one titled DESIRED OUTCOME.

class CanvasNode 
{
    parent: CanvasNode;
    children: Array<CanvasNode>;
    opacity: 1;
    render() { ... some render code ... }
    append(node) { this.children.push(node); }

    // this is not working correctly
    setOpacity(value) { this.opacity = this.parent.opacity * value; } 
}
class CanvasImage extends CanvasNode {}
class CustomCanvas
{
    constructor()
    {
         const canvas = document.createElement('canvas');
         const parent = new CanvasNode();
         const child1 = new CanvasImage('https://img.youtube.com/vi/c4zUnACRrQU/0.jpg');
         parent.append(child1);
         const child2 = new CanvasImage('https://betanews.com/wp-content/uploads/2017/08/new-youtube-logo.jpg');
         parent.append(child2);
         
         // this is not working correctly
         parent.setOpacity(0.5);
    }
}

I want source image to have desired outcome

royalBlue
  • 95
  • 3
  • 9
  • Does this answer your question? [How to change the opacity (alpha, transparency) of an element in a canvas element?](https://stackoverflow.com/questions/2359537/how-to-change-the-opacity-alpha-transparency-of-an-element-in-a-canvas-elemen) – Anurag Srivastava Jul 19 '23 at 12:14
  • Nope. I know how to set opacity for EACH image separately, but I don't know how to set it for parent of those images. – royalBlue Jul 19 '23 at 12:18

0 Answers0