Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flip vertically a background-image every time it repeat-y

Tags:

I'm searching for a trick that repeat my background image vertically, but every time the image is repeated I want to flip it vertically.

I tried all the things in my mind with repeat-y but I don't found a solution, even searching for it in google.

Example:

Background Straight background-image

Background flipped vertically Flipped background-image when it repeat-y

Background flipped vertically again And then flipped again to straight

There is a method to obtain that position?

I'll accept the solution in JScript (and library of it) only if there isn't a solution with pure css.

Thank you all!

like image 872
rpasianotto Avatar asked Jul 30 '13 14:07

rpasianotto


People also ask

Which style rule will repeat the background image vertically?

The background-repeat CSS property sets how background images are repeated. A background image can be repeated along the horizontal and vertical axes, or not repeated at all.

How do I repeat the background of a picture horizontally?

background-repeat: repeat-x; Only the background can be repeated using css so if you want two backgrounds, you can apply one background to the body and an horizontal background to the div#content for instance.


2 Answers

This cannot be done using standard CSS3 since there are no CSS3 properties to rotate a background image.

Reference: http://www.w3.org/TR/css3-background

As suggested, combine the two motifs in a single image, much simpler and will always work.

like image 61
Marc Audet Avatar answered Sep 19 '22 14:09

Marc Audet


I would load the background image like so

<style> body{     background-image:url('mybackground');     background-repeat:no-repeat; } </style> 

Background

As the image has already loaded we can do a little JavaScript without the browser doing much extra work. we can check whether the height of the image fills the entire window.

<script> var el      =  $('body')[0]; var src     =  $(el).css('background-image').slice(4, -1); var img     =  new Image();  checkHeight =  function(){         if(img.height < window.innerHeight){        repeatBg();          } } img.onload  =  checkHeight(); img.src = src; </script> 

If the image background does not fill the full height of the window then this background needs repeating/flipping straight away(this will increase load time); otherwise event listeners can be used to trigger the check later on.
The following function draws the image to the canvas element and creates a dataURL. The background-image src is updated to the new dataURL and repeat-background is changed from no-repeat to repeat-y(vertically);
the event listeners are then remove so the function is not called again.

<canvas style="display:none" id="canvas"></canvas>   <script>  repeatBg = function(){       var canvas = $('#canvas')[0];     var ctx = canvas.getContext("2d");     canvas.width = img.width;     canvas.height = img.height *2 ;     ctx.drawImage(img,0,0);     ctx.scale(1, -1);     ctx.translate(0, -img.height);     ctx.drawImage(img, 0, -img.height, img.width, img.height);     var dataURL = canvas.toDataURL();     $(el).css({         'background-image' : 'url('+dataURL+')',         'background-repeat' : 'repeat-y'     });         $(window).off('load, scroll, resize', checkHeight);         } $(window).on('load, scroll, resize', checkHeight);    </script> 

And hey presto

flipped

http://jsfiddle.net/dtjones1988/y49rvu73/6/

like image 34
TarranJones Avatar answered Sep 19 '22 14:09

TarranJones