0

The problem lies whenever the program does the animation at a very fast rate, it becomes extremely lagging. I have been trying all I can to improve this code below... But I can not think of anything useful...

I know some of you might say: "Draw less" or something like that, but that just is not possible.

This basically draws a small rectangle with rounded corners and makes it larger each frame until it reaches a specific size seen in the if statements.

Full code if needed: https://github.com/GunZi200/Memory-Colour/blob/master/SourceCode.js

function turnEvent(AnX, AnY) {
    var lengd = rects.length;
    eventDone = false,
    one30 = 18,
    one40 = 18,  
    one301 = false, 
    one401 = false;
    for (var i = 0; i < lengd; i += 1) {
        // Indentifying rectangle in use, so we can access the color, and position.
        if (collides([rects[i]], AnX, AnY)) {
            var rightBox = rects[i];
            var rectangle = rects2[i];
        }
    }
    rounded_rect(rectangle.x, rectangle.y, 90, 110, 10, 'black', 'black');
    function render() {
        ctx.beginPath();
        ctx.fillStyle = rightBox.color;
        ctx.moveTo((rectangle.x + 42 - one40) * Xf, (rectangle.y + 32 - one30) * Yf);
        ctx.lineTo((rectangle.x + 48 + one40) * Xf, (rectangle.y + 32 - one30) * Yf);
        ctx.quadraticCurveTo((rectangle.x + 58 + one30) * Xf, (rectangle.y + 32 - one30) * Yf, (rectangle.x + 58 + one30) * Xf, (rectangle.y + 42 - one40) * Yf);
        ctx.lineTo((rectangle.x + 58 + one30) * Xf, (rectangle.y + 68 + one40) * Yf);
        ctx.quadraticCurveTo((rectangle.x + 58 + one30) * Xf, (rectangle.y + 78 + one30) * Yf, (rectangle.x + 48 + one40) * Xf, (rectangle.y + 78 + one30) * Yf);
        ctx.lineTo((rectangle.x + 42 - one40) * Xf, (rectangle.y + 78 + one30) * Yf);
        ctx.quadraticCurveTo((rectangle.x + 32 - one30) * Xf, (rectangle.y + 78 + one30) * Yf, (rectangle.x + 32 - one30) * Xf, (rectangle.y + 68 + one40) * Yf);
        ctx.lineTo((rectangle.x + 32 - one30) * Xf, (rectangle.y + 42 - one40) * Yf);
        ctx.quadraticCurveTo((rectangle.x + 32 - one30) * Xf, (rectangle.y + 32 - one30) * Yf, (rectangle.x + 42 - one40) * Xf, (rectangle.y + 32 - one30) * Yf);
        ctx.fill();
        ctx.closePath();
        ctx.fillStyle = fillstyle;
        if (one30 === 30) {
            one30 += 0;
            one301 = true;
        } else {
            one30 += 2;
        }
        if (one40 === 30) {
            one401 = true;
        } else {
            one40 += 1;
        }
        if (one401 && one301) {
            eventDone = true;
        }
    }
    (function animloop(){
        if (eventDone) {//condition to stop requestAnimationFrame();
            eventDone = false;
            ctx.clearRect(0, 0, 310 * Xf, 365 * Yf);
            ---------ctx.putImageData(imgData, 0, 0);--------------
            return;
        };
        requestAnimationFrame(animloop);
        render();
    })();
}

EDIT: The problem is to make the actual image. I cannot render it onto a cached canvas because Images in HD will be simply to big. I am developing this for iPhone, Retina Displays.

I render the UI before i run these if statements below, and simply take a picture of a "part" of it. I use the main canvas to keep the quality of the image. If I use drawImage...what image do I use? I do not want to use a file, but I wont mind any examples of that? :)

if (x === 414 && y === 736) {
    imgData = ctx.getImageData(0,0,3*x,3*365*Yf); // iPhone 6+ and 6
} else if (x === 768 && y === 1024) {
    if (a_canvas.width === 2 * x) {
        imgData = ctx.getImageData(0,0,2*x,2*365*Yf); // normal retina
    } else {
        imgData = ctx.getImageData(0,0,x,365*Yf); // non-Retina displays
    }
} else {
    imgData = ctx.getImageData(0,0,2*x,2*365*Yf); // normal retina
}
  • 2
    I don't have time to review the 700 lines of code in your repository, but there's one huge performance killer in the code shown in your question. `putImageData` yields verrrrrry poor performance--it's not even GPU accelerated. Refactor your code to avoid getImageData & putImageData. – markE Jan 03 '15 at 04:45
  • It does look like there's only one putImageData, when the animation ends... – gengkev Jan 03 '15 at 05:35
  • @gengkev. Yes, but...`turnEvent` (and therefore putImageData) is being run for **every** rectangle on the game board **every** 400-450ms. It quickly adds up to quite a few putImageData's. I'm really trying to avoid diving into 700 lines of code :-0 so please help the questioner if you have time... :-) – markE Jan 03 '15 at 06:19
  • Perhaps I could use `createImagedata` when I create the image? It is much faster than `getImageData` http://jsperf.com/canvas-createimagedata-vs-getimagedata – Guðni Már Gilbert Jan 03 '15 at 09:20
  • use drawImage to store some image on a separate canvas, then replace your putImageData by a drawImage from this canvas. Huge speed boost. – GameAlchemist Jan 03 '15 at 13:22

0 Answers0