1

I’m having a problem here since a couple days ago with Google Chrome. I love pixel art and I'm trying to create a huge animated map using html5.

Everything was ok until I started to use more than 5 or 6 canvas at same time. In Firefox and Internet explorer the map has no problems, but in Chrome the CPU reaches 70% and sometimes even more.

Can you give me some good tips about how solve this kind of issues in Chrome? I've been searching on Internet about improve performance in Chrome but nothing is helping me.

Thanks for your help.

Just for reference this is the map: http://pixelslivewallpaper.github.io/jadsdsengine/Zelda.htm

Peppermint
  • 41
  • 4

1 Answers1

3

Solution:

The easy way to fix this problem is stopping the animation of all the canvas outside the user visible area and resuming them later. The way for calculate this area is capturing the scroll position.

Apparently, Firefox and Internet explorer are doing this job automatically, but in Google Chrome you need do it manually.

Here is the code:

this.loadIsVisibleOptions = function () {
    if (this.stopAnimationWhenIsNotVisible) {
        window.addEventListener("scroll", function (event) { currentJadsdsEngine.isVisible(document.getElementById(canvasID), currentJadsdsEngine) });
    }
}

this.isVisible = function (element, obj) {
    if (element.offsetWidth === 0 || element.offsetHeight === 0) {
        currentJadsdsEngine.stop();
    }
    else {
        var rects = element.getClientRects();
        var result = false;
        var tempClientHeight = document.documentElement.clientHeight;
        var tempClientWidth = document.documentElement.clientWidth;

        for (var i = 0, l = rects.length; i < l; i++) {
            if (rects[i].top + rects[i].height > 0 ? rects[i].top <= tempClientHeight : (rects[i].bottom > 0 && rects[i].bottom <= tempClientHeight) == true) {
                if (rects[i].left + rects[i].width > 0 ? rects[i].left <= tempClientWidth : (rects[i].right > 0 && rects[i].right <= tempClientWidth) == true) {
                    result = true;
                    break;
                }
            }
        }

        if (result) {
            currentJadsdsEngine.resume();
        }
        else {
            currentJadsdsEngine.stop();
        }
    }
}

Inspiration: How do I check if an element is really visible with JavaScript?

Community
  • 1
  • 1
Peppermint
  • 41
  • 4