I'm building a game in Html and Javascript using the <canvas>
element.
As I need to flip an image horizontally, in adition to the principal canvas I create a secondary one that isn't visible for flipping the images (to avoid downloading another image for the other position)
Then I draw the result of the secondary canvas in the principal one.
This method works without any problem in Firefox 27, while in Chrome, Chrome mobile, Opera mobile and Firefox mobile for an unknown reason it doesn't.
The code for the secondary canvas is the following:
var flip = function (image) {
'use strict';
var offscreenCanvas = document.createElement('canvas'),
offscreenCtx = offscreenCanvas.getContext('2d');
offscreenCanvas.width = image.width;
offscreenCtx.translate(image.width, 0);
offscreenCtx.scale(-1, 1);
offscreenCtx.drawImage(image, 0, 0);
return offscreenCanvas;
};
And the code for drawing the result in the principal canvas:
var flippedCharImg = flip(charImg);
ctx.drawImage(flippedCharImg, character.xpos, character.ypos, character.stopped.width, character.stopped.height);
Any idea of why it doesn't work on those browsers and how to make it work?
Sorry for the syntax!