Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7738521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:17:14+00:00 2026-06-01T08:17:14+00:00

I am trying to implement a lighting effect in an HTML5/JavaScript game using tile

  • 0

I am trying to implement a lighting effect in an HTML5/JavaScript game using tile replacement. What I have now is kind of working, but the transitions do not look smooth/natural enough as the light source moves around. Here’s where I am now:

  1. Right now I have a background map that has a light/shadow spectrum PNG tilesheet applied to it – going from darkest tile to completely transparent. By default the darkest tile is drawn across the entire level on launch, covering all other layers etc.
  2. I am using my predetermined tile sizes (40 x 40px) to calculate the position of each tile and store its x and y coordinates in an array.
  3. I am then spawning a transparent 40 x 40px “grid block” entity at each position in the array
  4. The engine I’m using ([ImpactJS][1]) then allows me to calculate the distance from my light source entity to every instance of this grid block entity.
  5. I can then replace the tile underneath each of those grid block tiles with a tile of the appropriate transparency.

Currently I’m doing the calculation like this in each instance of the grid block entity that is spawned on the map:

var dist = this.distanceTo( ig.game.player );

var percentage = 100 * dist / 960;

if (percentage < 2) {
    // Spawns tile 64 of the shadow spectrum tilesheet at the specified position
    ig.game.backgroundMaps[2].setTile( this.pos.x, this.pos.y, 64 ); 
} else if (percentage < 4) {
    ig.game.backgroundMaps[2].setTile( this.pos.x, this.pos.y, 63 );
} else if (percentage < 6) {
    ig.game.backgroundMaps[2].setTile( this.pos.x, this.pos.y, 62 );
}       

(sorry about the weird spacing, I still haven’t gotten the hang of pasting code in here properly)

The problem is that like I said, this type of calculation does not make the light source look very natural. Tile switching looks too sharp whereas ideally they would fade in and out smoothly using the spectrum tilesheet (I copied the tilesheet from another game that manages to do this, so I know it’s not a problem with the tile shades. I’m just not sure how the other game is doing it). I’m thinking that perhaps my method of using percentages to switch out tiles could be replaced with a better/more dynamic proximity formula of some sort that would allow for smoother transitions? Might anyone have any ideas for what I can do to improve the visuals here, or a better way of calculating proximity with the information I’m collecting about each tile?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:17:15+00:00Added an answer on June 1, 2026 at 8:17 am
    var size = 32;
    var a = [size];
    
    for (i = 0; i < size; i++) {
        a[i] = [size];
        for (y = 0; y < size; y++) {
            a[i][y] = 1;
        }
    }
    
    function display(arr) {
        h = "";
        for (y = 0; y < size; y++) {
            for (x = 0; x < size; x++) {
                h += "<span style='background-color:rgba(255,255,255," + (arr[x][y] / 10) + ")'></span>";
                if (x == size - 1) {
                    h += "<br/>";
                }
            }
        }
        $("#h").html(h);
    }
    
    function LightSource(x, y, l) {
        this.x = x;
        this.y = y;
        this.l = l;
    }
    
    var ls = [];
    ls.push(new LightSource(6, 6, 4));
    ls.push(new LightSource(2, 2, 3));
    ls.push(new LightSource(9, 9, 5));
    ls.push(new LightSource(20, 14, 8));
    ls.push(new LightSource(20, 19, 8));
    
    for (z = 0; z < ls.length; z++) {
        for (x = 0; x < size; x++) {
            for (y = 0; y < size; y++) {
                xd = x - ls[z].x;
                yd = y - ls[z].y;
                dist = Math.sqrt(xd * xd + yd * yd);
                if (ls[z].l - dist > 0) {
                    a[x][y] += ((10 - a[x][y]) / 10) * (ls[z].l - dist);
                }
            }
        }
    }
    
    display(a);​
    

    https://gamedev.stackexchange.com/questions/23454/grid-cell-based-light-system

    http://jsfiddle.net/ZmPqL/

    http://jsfiddle.net/vCgeM/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

trying to implement a multiplayer. Using the sample from Game Center - Sending and
Im trying to implement pagination using multiple searching criteria. Supposed I Have student table.
I'm trying implement pull to refresh on a ListFragment but right now none of
Trying to implement Piwik using REST API over http but need a little help.
Trying to implement sticky footer but its not working as planned. It throws it
Trying to implement a timer for my game that I'm making. I have a
I trying to implement the active record pattern using Java/JDBC and MySQL along with
Iam trying to implement a simple JMS(traditional not using springs) code in eclipse using
Im trying to implement an Observer/Observable pattern on an EC2 instance. I have been
Trying to implement search with Sunspot Gem wich is using Solr.Fulltext search works fine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.