shards.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * Shards jQuery plug-in
  3. * Multilayered gradient background effect
  4. *
  5. * @homepage http://bite-software.co.uk/shards/
  6. * @version 1.1
  7. * @license MIT http://opensource.org/licenses/MIT
  8. */
  9. (function($) {
  10. var Plugin = function(me,c1,c2,sh,steps,wheel,light,alf,fs){
  11. this.el = me;
  12. this.sharp = true;
  13. this.fs = fs;
  14. this.filter = '';
  15. this.colours = {
  16. c1 : c1,
  17. c2 : c2,
  18. c3 : c2,
  19. shade : sh,
  20. alpha : alf,
  21. steps : steps,
  22. wheel : wheel,
  23. light : ~~(light)
  24. }
  25. this.init();
  26. }
  27. Plugin.prototype.init = function(){
  28. this.cssPrefix = false;
  29. var ua = navigator.userAgent;
  30. if(/Chrome\/(\S+)/.test(ua)|| /AppleWebKit\/(\S+)/.test(ua)) {
  31. this.cssPrefix = '-webkit';
  32. }
  33. else if(/Firefox\/(\S+)/.test(ua)) {
  34. this.cssPrefix = '-moz';
  35. }else if(window.opera) {
  36. this.cssPrefix = '-o';
  37. }else if(/MSIE ([^;]+)/.test(ua)) {
  38. this.cssPrefix = '-ms';
  39. };
  40. if(this.cssPrefix){
  41. var steps = this.colours.steps;
  42. while( steps > 0){
  43. this.percents = this.percentage();
  44. this.stringBuilder();
  45. this.colourFilter();
  46. steps -= 1;
  47. if(steps > 0) this.filter += ', ';
  48. }
  49. this.el.css('background-image',this.filter);
  50. if(this.fs) this.fit();
  51. }else{
  52. console.log('sorry bro, your browser isnt supported.');
  53. }
  54. }
  55. Plugin.prototype.stringBuilder = function(){
  56. var col = this.colours,
  57. c1 = this.catcol(col.c1),
  58. c2 = this.catcol(col.c2),
  59. c3 = this.catcol(col.c3),
  60. shade = this.catcol(col.shade),
  61. deg = ~~(Math.random()*360);
  62. this.filter += this.cssPrefix+'-linear-gradient('+deg+'deg,'+c1+' '+this.percents.a+' ,'+shade+' '+this.percents.b+', '+c2+' '+this.percents.c+', '+c3+' '+this.percents.d+')';
  63. }
  64. Plugin.prototype.catcol = function(col){
  65. var beg = 'rgba(',
  66. end = ')',
  67. part = col.toString();
  68. return beg.concat(part).concat(end);
  69. }
  70. Plugin.prototype.fit = function(){
  71. this.el.css({
  72. 'width':'100%',
  73. 'height': window.innerHeight
  74. })
  75. }
  76. Plugin.prototype.percentage = function(){
  77. var p1 = ~~(Math.random()*85),
  78. p2 = p1 + ~~(Math.random()*15),
  79. p3 = p2,
  80. p4 = 100-p2 + ~~(Math.random()*p2);
  81. var percents = {
  82. a:p1+'%',
  83. b:p2+'%',
  84. c:p3+'%',
  85. d:p4+'%'
  86. }
  87. return percents;
  88. }
  89. Plugin.prototype.colourFilter = function(){
  90. var col = this.colours;
  91. col.c1 = this.colstep(col.c1);
  92. col.c1.push(col.alpha);
  93. col.c2 = this.colstep(col.c2);
  94. col.c2.push(col.alpha);
  95. col.c3 = this.colstep(col.c2);
  96. col.c3.push(col.alpha);
  97. col.shade = this.colstep(col.shade);
  98. col.shade.push(col.alpha);
  99. }
  100. Plugin.prototype.colstep = function(col){
  101. var hsl = this.hsl(col),
  102. wheel = this.colours.wheel,
  103. hue = (360 * wheel);
  104. if(this.colours.light>3) this.colours.light = 3;
  105. hsl[0] = hsl[0] - (~~(Math.random()*hue/2)) + (~~(Math.random()*hue/2));
  106. hsl[1] = wheel * 100;
  107. hsl[2] = 30 * this.colours.light;
  108. return this.rgb(hsl);
  109. }
  110. Plugin.prototype.hsl = function(rgb){
  111. var r1 = rgb[0] / 255;
  112. var g1 = rgb[1] / 255;
  113. var b1 = rgb[2] / 255;
  114. var maxColor = Math.max(r1,g1,b1);
  115. var minColor = Math.min(r1,g1,b1);
  116. //Calculate L:
  117. var L = (maxColor + minColor) / 2 ;
  118. var S = 0;
  119. var H = 0;
  120. if(maxColor != minColor){
  121. //Calculate S:
  122. if(L < 0.5){
  123. S = (maxColor - minColor) / (maxColor + minColor);
  124. }else{
  125. S = (maxColor - minColor) / (2.0 - maxColor - minColor);
  126. }
  127. //Calculate H:
  128. if(r1 == maxColor){
  129. H = (g1-b1) / (maxColor - minColor);
  130. }else if(g1 == maxColor){
  131. H = 2.0 + (b1 - r1) / (maxColor - minColor);
  132. }else{
  133. H = 4.0 + (r1 - g1) / (maxColor - minColor);
  134. }
  135. }
  136. L = L * 100;
  137. S = S * 100;
  138. H = H * 60;
  139. if(H<0){
  140. H += 360;
  141. }
  142. var result = [H, S, L];
  143. return result;
  144. }
  145. Plugin.prototype.rgb = function(hsl){
  146. var h = hsl[0];
  147. var s = hsl[1];
  148. var l = hsl[2];
  149. var m1, m2, hue;
  150. var r, g, b;
  151. s /=100;
  152. l /= 100;
  153. if (s == 0)
  154. r = g = b = (l * 255);
  155. else {
  156. if (l <= 0.5)
  157. m2 = l * (s + 1);
  158. else
  159. m2 = l + s - l * s;
  160. m1 = l * 2 - m2;
  161. hue = h / 360;
  162. r = this.hue2rgb(m1, m2, hue + 1/3);
  163. g = this.hue2rgb(m1, m2, hue);
  164. b = this.hue2rgb(m1, m2, hue - 1/3);
  165. }
  166. return [Math.round(r), Math.round(g), Math.round(b)];
  167. }
  168. Plugin.prototype.hue2rgb = function(m1, m2, hue) {
  169. var v;
  170. if (hue < 0)
  171. hue += 1;
  172. else if (hue > 1)
  173. hue -= 1;
  174. if (6 * hue < 1)
  175. v = m1 + (m2 - m1) * hue * 6;
  176. else if (2 * hue < 1)
  177. v = m2;
  178. else if (3 * hue < 2)
  179. v = m1 + (m2 - m1) * (2/3 - hue) * 6;
  180. else
  181. v = m1;
  182. return 255 * v;
  183. };
  184. $.fn.shards = function(colour1, colour2, shadeColour, steps, wheel, lightness, alpha, fullscreen){
  185. var el = $(this);
  186. var shards = new Plugin(el,colour1,colour2,shadeColour,steps,wheel,lightness,alpha,fullscreen);
  187. if(fullscreen){
  188. $(window).resize( function(){
  189. shards.fit();
  190. });
  191. }
  192. return this.el;
  193. }
  194. })(jQuery);