0

I am working on a game in which I am trying to simulate a torchlike glow effect around a player. To do this, I am first drawing my player, and then a translucent overlay on my canvas. This overlay gives a feeling of darkness, dimming the screen. However, I also want to make the area around the player brighter, either with a glow or by removing the darkness in specific areas of my canvas (in this case, the position of my player). However, when I try to do so, my entire canvas becomes the color of my glow, and the effect is not as desired. Here is what happens:

result

Additionally, here is the code I am using to create the glow effect:

glow() {

    canv.shadowBlur = 75;
    canv.shadowColor = "orange";
    canv.globalAlpha = 0;
    canv.fillRect(this.x, this.y, this.width, this.height);

}

And the darkness effect on my canvas (please note, these are methods in two separate objects):

darkness_effect() {
    
    canv.globalAlpha = 0.6;
    canv.shadowBlur = 0;
    canv.shadowColor = "black";
    canv.fillStyle = "black";
    canv.fillRect(0, 0, canvas.width, canvas. height);

}

And this is the order of the function calling (the other functions can be ignored, they have no effect on the canvas display):

function animate() {

    game.draw_background();
    game.tick_update();

    player.update();
    player.animate();

    game.darkness_effect(); // !

    player.glow(); // !

    requestAnimationFrame(animate);

}

How can I fix this? I only want a circular glow around my player's position, not for the entire screen to be dark orange.

P.S. If all else fails, I'm also willing to try a different method to attain my desired result. Does anyone know of any other ways to do this?

Bubbly
  • 301
  • 3
  • 11
  • Are you clearing the styles between drawing the different elements? For example, if `glow()` is called before `darkness_effect()`, `canv.shadowColor` and `canv.shadowBlur` will carry over because they were never updated in between. – Ouroborus Jul 20 '21 at 03:08
  • I just tried editing my code as per your suggestion, but the problem still (somewhat) remains. Although the glow is somewhat visible, it is still overlapped by the darkness effect. – Bubbly Jul 20 '21 at 03:23
  • Very likely more of the same. You'll need to check all of your drawing functions and see what effect is not getting updated. (They also carry over from end to beginning. Changes in `glow()` will effect `draw_background()`.) – Ouroborus Jul 20 '21 at 03:31
  • You might also try a brighter orange or draw the glow several times. `.shadowBlur` tends to dim colors by its nature. The down side is that `.shadowBlur` is a costly effect. – Ouroborus Jul 20 '21 at 03:37
  • [This might help you](https://stackoverflow.com/questions/43422184/efficient-html5-canvas-glow-effect-without-shape/43423126#43423126). use either a gradient, or create a hole in your overlay using globalCompositeOperation. – Kaiido Jul 20 '21 at 05:50

1 Answers1

1

It seems I've found a workaround. I've continued the use of my darkness effect but also created a new image to simulate the glow. It works as desired now.

new product

Bubbly
  • 301
  • 3
  • 11