Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

html - Improving site performance with many images

I am currently making a game in HTML/JS that includes approximately 1200 blocks per level. All of the blocks are individual images, but they are a lot of the time similar. They are 20*20 pixels. After inserting the pictures instead of placeholder divs, the perfomance has gone down a lot.

I am not sure, if it is because of the bandwith, but I would expect the pc to cache the images and reuse it.

Or maybe it is a memory problem with the amount of images.

socket.on("sendBlocks",function(blocks,blocksCoords){
    if(typeof blocksCoords[area.X + "_" + area.Y] !== "undefined"){
    mapLimit.artX = 0;
    mapLimit.artY = -1;
    while(mapLimit.X + mapLimit.Y != mapLimit.artX + mapLimit.artY){
        mapLimit.artY = mapLimit.artY + 1;
        if(mapLimit.artY > mapLimit.Y){
            mapLimit.artX = mapLimit.artX + 1;
            mapLimit.artY = 0;
        }
        //Change block, executed for every art-coord.
        if(typeof blocksCoords[area.X + "_" + area.Y][mapLimit.artX + "_" + mapLimit.artY] !== "undefined"){
            switch(blocksCoords[area.X + "_" + area.Y][mapLimit.artX + "_" + mapLimit.artY].type){
                case "wood":
                    $("#" + mapLimit.artX.toString() + "_" + mapLimit.artY.toString()).attr("src","https://db.tt/TyZBx7EG");
                    break;
                case "empty":
                    $("#" + mapLimit.artX.toString() + "_" + mapLimit.artY.toString()).attr("src","https://db.tt/SdXqMMiE");
                    break;
            }
        }else if(typeof blocksCoords[area.X + "_" + area.Y][mapLimit.artX + "_" + mapLimit.artY] === "undefined"){
            $("#" + mapLimit.artX.toString() + "_" + mapLimit.artY.toString()).attr("src","https://db.tt/SdXqMMiE");
        }
    }
    }else if(typeof blocksCoords[area.X + "_" + area.Y] === "undefined"){
        $(".block").css("background-color","white");
    }

This code will be executed every time that the blocks are updated from the server. Checking if the block is wood, empty or undefined. Giving it different textures for each type of block.

The server updates the blocks every 100 ms, is that too fast?

Any suggestions to how this problem can be solved? Thanks a lot :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A browser allows a maximum of 6-8 connection to a domain. So say you have 1200 images, it can only download 8 images at a time. This is something I learned recently. The solution to this was to create subdomains like images.website.com. That way the browser will treat the domain as if its a different one and allow you more 6-8 connections. So now 8 connections for website.com and 8 connection for images.website.come. That gives you 16 connections. Its upto you how many subdomains you'd like to make.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...