CSS Side Borders for a HTML5 Game
It's time to created a HTML game with a more efficient background. As it stands I have already created an effective way to display my icy landscape with the actual game load on top of the canvas. Making it so I don't have to clear the whole canvas to create animations.This is a great improvement verse what I had before, it was a much bigger image and had everything inside it. So moving from there I separated the side borders and split them into two images. Using a Div around my canvas tag I set the left image as the background and the second image on the canvas tag itself. This was not enough I now wanted to see if I could use a single image flipped horizontally for the other side.
The Setup
First Attempt
Doing some research I found a way to flip background image. The problem was the flip effecting both images and trying to get the images to face the right way. So I tried to flip one and another in the opposite direction but this did not work. Then I tried to make the two images on the same side face inward and only flipping one side. This worked, but as you read you will find out that this does not work completely.
Bugs I faced
I noticed for my game that all the coordinates were revised, but not making the connection I had not clue why. I soon made the connection that the canvas itself was being reflected and all my input captures were in the opposite direction! So now what was I to do? Rewrite all my javascript code in the opposite direction? Even if I wanted to do this, it would not be possible because of how I was capturing the input. So I would have to go back to the CSS and find a way to revise the flip back but keep the two background images flipped. As I said earlier flipping one and flipping it back with the other does not work but flipping both the same way does!
Final Solution
The Setup
<div id="game"> <canvas id="window">Sorry but Canvas is not Supported</canvas> </div>
First Attempt
Doing some research I found a way to flip background image. The problem was the flip effecting both images and trying to get the images to face the right way. So I tried to flip one and another in the opposite direction but this did not work. Then I tried to make the two images on the same side face inward and only flipping one side. This worked, but as you read you will find out that this does not work completely.
Bugs I faced
I noticed for my game that all the coordinates were revised, but not making the connection I had not clue why. I soon made the connection that the canvas itself was being reflected and all my input captures were in the opposite direction! So now what was I to do? Rewrite all my javascript code in the opposite direction? Even if I wanted to do this, it would not be possible because of how I was capturing the input. So I would have to go back to the CSS and find a way to revise the flip back but keep the two background images flipped. As I said earlier flipping one and flipping it back with the other does not work but flipping both the same way does!
Final Solution
canvas {
display:block;
-moz-transform: scaleX(-1);
-webkit-transform: scaleX(-1);
-o-transform: scaleX(-1);
transform: scaleX(-1);
-ms-filter:fliph;
filter: fliph;
background: url("LandScapeSide.jpg") left repeat-y;
}
#game {
-moz-transform: scaleX(-1);
-webkit-transform: scaleX(-1);
-o-transform: scaleX(-1);
transform: scaleX(-1);
-ms-filter:fliph;
filter: fliph;
background: url("LandScapeSide.jpg") left repeat-y;
}
Why this works
What is happening is that before the flip the background image would appear on the left facing inward, and with the flip it appears on the right still facing inward. Now the second background image comes in on the left. With the div and the canvas flipped and having the game background image on the right as well as the div's image on the left the two flips put you back to where you started. Its like the div's background image never moved and everything inside the canvas remains the same.
Results
Checkout Penguins Rising!
Just for fun
I wanted to compare the amount of memory I would be saving by going this route just to give myself a little more reason behind it all. Since it was a little more work to do the research and get everything to work with the proper values. Comparing the css line of code with the 9KB image.
UTF-8 string length & byte counter |