this post was submitted on 04 Oct 2023
8 points (83.3% liked)

Web Development

0 readers
1 users here now

Icon base by Delapouite under CC BY 3.0 with modifications to add a gradient

founded 1 year ago
MODERATORS
 

I quite new to web development. I created a simple game which suppose to raise awareness to the RP syndrome (which I suffer from). The simple goal is to find Waldo but the twist is that there is nothing but him on the screen.

I want to add a "flashlight courser effect" meaning that the only visible part of the screen should be the one around the courser (for some fixed or maybe chosen radius). But, I don't want the rest of the screen (the non-visible) to be black but white / blank (as this is in a way the way that I see the world). The other elements (buttons, score and counter) should stay visible always.

Can someone advice me on how can it be done? Ideally there should be two switches (on the corner) one controlling the mode (full visibility vs limited field of vision) and the second one controlling the type of "darkness" (black vs white).

Here is the link to the repo with the current code: https://github.com/menisadi/RPWaldo

Thanks in advance

Update: Thanks everyone!!! I used the SVG idea and added the flashlight effect" (even added a switch to toggle the "flashlight mode" on and off). Working great. Thanks!

top 8 comments
sorted by: hot top controversial new old
[–] glimse@lemmy.world 7 points 1 year ago (2 children)

(i haven't done webdev in YEARS so I'm pulling this out of my ass) what about a bigass png with transparency that follows your cursor?

[–] Spzi@lemm.ee 5 points 1 year ago (1 children)

Yes, or if you want to learn more about this approach, search for masks, layers.

[–] glimse@lemmy.world 3 points 1 year ago

Is that still the way? I haven't touched web programming since like 2016 and I figured a ton has changed. Can't even remember what JavaScript library I used for animation lol

[–] menisadi@programming.dev 3 points 1 year ago (2 children)

Interesting. But won't it work only for the background? Waldo isn't part of the background, he is a separate object as he changes position after each time you find him and click on it (I forgot to mention this important point).

[–] glimse@lemmy.world 4 points 1 year ago

You'd just have to make the image that follows your cursor have a higher z-index than Waldo

[–] glimse@lemmy.world 4 points 1 year ago

I looked up a guide, I wasn't too far off but this shows a better way to do it:

https://www.geeksforgeeks.org/how-to-create-a-spotlight-effect-using-html-and-css/

Just make your own image and get rid of this part:

/* Reduce the brightness of the image */ .img { filter: brightness(10%); }

[–] navigatron@beehaw.org 4 points 1 year ago

I would use z-indexes to setup the layering. Have a base layer, the “filter” layer, and then the ui on top.

Basically 3 divs, with z-index 1/2/3 respectively. Put elements on the base layer or ui layer based on if you should see them always or not.

For the filter, I would do an svg of a rectangle, big enough to cover the screen, and a hole in it. Then use js to set the svg’s position relative to the mouse.

The svg could easily be made transparent, black, or even get that fade effect around the edges.

[–] silas@programming.dev 2 points 1 year ago* (last edited 1 year ago)

I would look into a circular/radial CSS background gradient on a fixed-position fullscreen div with pointer-events set to none. You can make the center transparent and the rest whatever color/transparency you want. Then, use JS to set the circular gradient’s position relative to the cursor (use CSS variables if necessary)