cvi_glossy_lib.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /**
  2. * cvi_glossy_lib.js 1.1 (16-Oct-2007)
  3. * (c) by Christian Effenberger
  4. * All Rights Reserved
  5. * Source: corner.netzgesta.de
  6. * Distributed under NSL
  7. * License permits free of charge
  8. * use on non-commercial and
  9. * private web sites only
  10. * syntax:
  11. cvi_glossy.defaultRadius = 25; //INT 20-50 (%)
  12. cvi_glossy.defaultColor = 0; //STR '#000000'-'#ffffff' or 0
  13. cvi_glossy.defaultColor2 = 0; //STR '#000000'-'#ffffff' or 0
  14. cvi_glossy.defaultGradient = 'v'; //STR 'd|h|v'-'diagonally|horizontal|vertical'
  15. cvi_glossy.defaultShade = 50; //INT 20-66 (% opacity)
  16. cvi_glossy.defaultShadow = 40; //INT 0-100 (% opacity)
  17. cvi_glossy.defaultNoshadow = false; //BOOLEAN
  18. cvi_glossy.defaultNoradius = false; //BOOLEAN
  19. cvi_glossy.remove( image );
  20. cvi_glossy.add( image, options );
  21. cvi_glossy.modify( image, options );
  22. cvi_glossy.add( image, { radius: value, color: value, color2: value, gradient: value, shadow: value, shade: value, noshadow: value, noradius: value } );
  23. cvi_glossy.modify( image, { radius: value, color: value, color2: value, gradient: value, shadow: value, shade: value, noshadow: value, noradius: value } );
  24. *
  25. **/
  26. function roundedRect(ctx,x,y,width,height,radius,nopath){
  27. if (!nopath) ctx.beginPath();
  28. ctx.moveTo(x,y+radius);
  29. ctx.lineTo(x,y+height-radius);
  30. ctx.quadraticCurveTo(x,y+height,x+radius,y+height);
  31. ctx.lineTo(x+width-radius,y+height);
  32. ctx.quadraticCurveTo(x+width,y+height,x+width,y+height-radius);
  33. ctx.lineTo(x+width,y+radius);
  34. ctx.quadraticCurveTo(x+width,y,x+width-radius,y);
  35. ctx.lineTo(x+radius,y);
  36. ctx.quadraticCurveTo(x,y,x,y+radius);
  37. if (!nopath) ctx.closePath();
  38. }
  39. function addRadialStyle(ctx,x1,y1,r1,x2,y2,r2,opacity) {
  40. var tmp = ctx.createRadialGradient(x1,y1,r1,x2,y2,r2);
  41. var opt = Math.min(parseFloat(opacity+0.1),1.0);
  42. tmp.addColorStop(0,'rgba(0,0,0,'+opt+')');
  43. tmp.addColorStop(0.25,'rgba(0,0,0,'+opacity+')');
  44. tmp.addColorStop(1,'rgba(0,0,0,0)');
  45. return tmp;
  46. }
  47. function addLinearStyle(ctx,x,y,w,h,opacity) {
  48. var tmp = ctx.createLinearGradient(x,y,w,h);
  49. var opt = Math.min(parseFloat(opacity+0.1),1.0);
  50. tmp.addColorStop(0,'rgba(0,0,0,'+opt+')');
  51. tmp.addColorStop(0.25,'rgba(0,0,0,'+opacity+')');
  52. tmp.addColorStop(1,'rgba(0,0,0,0)');
  53. return tmp;
  54. }
  55. function addBright(ctx,x,y,width,height,radius,opacity) {
  56. var style = ctx.createLinearGradient(0,y,0,y+height);
  57. style.addColorStop(0,'rgba(254,254,254,'+opacity+')');
  58. style.addColorStop(1,'rgba(254,254,254,0.1)');
  59. ctx.beginPath(); ctx.moveTo(x,y+radius); ctx.lineTo(x,y+height-radius);
  60. ctx.quadraticCurveTo(x,y+height,x+radius,y+height); ctx.lineTo(x+width-radius,y+height);
  61. ctx.quadraticCurveTo(x+width,y+height,x+width,y+height-radius);
  62. ctx.lineTo(x+width,y+radius); ctx.quadraticCurveTo(x+width,y,x+width-radius,y);
  63. ctx.lineTo(x+radius,y); ctx.quadraticCurveTo(x,y,x,y+radius); ctx.closePath();
  64. ctx.fillStyle = style; ctx.fill();
  65. }
  66. function addDark(ctx,x,y,width,height,radius,opacity) {
  67. var style = ctx.createLinearGradient(0,y,0,y+height);
  68. style.addColorStop(0,'rgba(0,0,0,0)');
  69. style.addColorStop(1,'rgba(0,0,0,'+opacity+')');
  70. ctx.beginPath(); ctx.moveTo(x,y); ctx.lineTo(x,y+height-radius);
  71. ctx.quadraticCurveTo(x,y+height,x+radius,y+height); ctx.lineTo(x+width-radius,y+height);
  72. ctx.quadraticCurveTo(x+width,y+height,x+width,y+height-radius);
  73. ctx.lineTo(x+width,y); ctx.lineTo(x,y); ctx.closePath();
  74. ctx.fillStyle = style; ctx.fill();
  75. }
  76. function addFrame(ctx,x,y,width,height,radius,opacity) {
  77. roundedRect(ctx,x,y,width,height,radius);
  78. var style = ctx.createLinearGradient(0,0,0,height);
  79. style.addColorStop(0,'rgba(254,254,254,'+opacity+')');
  80. style.addColorStop(1,'rgba(0,0,0,'+opacity+')');
  81. ctx.lineWidth = (radius+x)/2;
  82. ctx.strokeStyle = style;
  83. ctx.stroke();
  84. }
  85. function glossyShadow(ctx,x,y,width,height,radius,opacity){
  86. var style; var os = radius/2;
  87. ctx.beginPath(); ctx.rect(x+radius,y,width-(radius*2),y+os); ctx.closePath();
  88. style = addLinearStyle(ctx,x+radius,y+os,x+radius,y,opacity);
  89. ctx.fillStyle = style; ctx.fill();
  90. ctx.beginPath(); ctx.rect(x,y,radius,radius); ctx.closePath();
  91. style = addRadialStyle(ctx,x+radius,y+radius,radius-os,x+radius,y+radius,radius,opacity);
  92. ctx.fillStyle = style; ctx.fill();
  93. ctx.beginPath(); ctx.rect(x,y+radius,os,height-(radius*2)); ctx.closePath();
  94. style = addLinearStyle(ctx,x+os,y+radius,x,y+radius,opacity);
  95. ctx.fillStyle = style; ctx.fill();
  96. ctx.beginPath(); ctx.rect(x,y+height-radius,radius,radius); ctx.closePath();
  97. style = addRadialStyle(ctx,x+radius,y+height-radius,radius-os,x+radius,y+height-radius,radius,opacity);
  98. ctx.fillStyle = style; ctx.fill();
  99. ctx.beginPath(); ctx.rect(x+radius,y+height-os,width-(radius*2),os); ctx.closePath();
  100. style = addLinearStyle(ctx,x+radius,y+height-os,x+radius,y+height,opacity);
  101. ctx.fillStyle = style; ctx.fill();
  102. ctx.beginPath(); ctx.rect(x+width-radius,y+height-radius,radius,radius); ctx.closePath();
  103. style = addRadialStyle(ctx,x+width-radius,y+height-radius,radius-os,x+width-radius,y+height-radius,radius,opacity);
  104. ctx.fillStyle = style; ctx.fill();
  105. ctx.beginPath(); ctx.rect(x+width-os,y+radius,os,height-(radius*2)); ctx.closePath();
  106. style = addLinearStyle(ctx,x+width-os,y+radius,x+width,y+radius,opacity);
  107. ctx.fillStyle = style; ctx.fill();
  108. ctx.beginPath(); ctx.rect(x+width-radius,y,radius,radius); ctx.closePath();
  109. style = addRadialStyle(ctx,x+width-radius,y+radius,radius-os,x+width-radius,y+radius,radius,opacity);
  110. ctx.fillStyle = style; ctx.fill();
  111. }
  112. var cvi_glossy = {
  113. defaultRadius : 25,
  114. defaultColor : 0,
  115. defaultColor2 : 0,
  116. defaultGradient : 'v',
  117. defaultShade : 50,
  118. defaultShadow : 40,
  119. defaultNoshadow : false,
  120. defaultNoradius : false,
  121. defaultPressed : false,
  122. defaultHighlight : false,
  123. add: function(image, options) {
  124. if(image.tagName.toUpperCase() == "IMG") {
  125. var defopts = { "radius" : cvi_glossy.defaultRadius, "color" : cvi_glossy.defaultColor, "color2" : cvi_glossy.defaultColor2, "gradient" : cvi_glossy.defaultGradient, "shadow" : cvi_glossy.defaultShadow, "shade" : cvi_glossy.defaultShade, "noshadow" : cvi_glossy.defaultNoshadow, "noradius" : cvi_glossy.defaultNoradius, "pressed" : cvi_glossy.defaultPressed, "highlight" : cvi_glossy.defaultHighlight }
  126. if(options) {
  127. for(var i in defopts) { if(!options[i]) { options[i] = defopts[i]; }}
  128. }else {
  129. options = defopts;
  130. }
  131. // try {
  132. var object = image.parentNode;
  133. if(Ext.isIE) {
  134. if (document.namespaces.length > 0 ){
  135. if(document.namespaces.item('v') == null) {
  136. var stl = document.createStyleSheet();
  137. stl.addRule("v\\:*", "behavior: url(#default#VML);");
  138. document.namespaces.add("v", "urn:schemas-microsoft-com:vml");
  139. }
  140. }
  141. else{
  142. var stl = document.createStyleSheet();
  143. stl.addRule("v\\:*", "behavior: url(#default#VML);");
  144. document.namespaces.add("v", "urn:schemas-microsoft-com:vml");
  145. }
  146. var display = (image.currentStyle.display.toLowerCase()=='block')?'block':'inline-block';
  147. var canvas = document.createElement(['<div style="zoom:1;overflow:hidden;display:'+display+';width:'+image.width+'px;height:'+image.height+'px;padding:0;">'].join(''));
  148. var flt = image.currentStyle.styleFloat.toLowerCase();
  149. display = (flt=='left'||flt=='right')?'inline':display;
  150. canvas.options = options;
  151. canvas.dpl = display;
  152. canvas.id = image.id;
  153. canvas.alt = image.alt;
  154. canvas.title = image.title;
  155. canvas.source = image.src;
  156. canvas.className = image.className;
  157. canvas.style.cssText = image.style.cssText;
  158. canvas.height = image.height;
  159. canvas.width = image.width;
  160. object.replaceChild(canvas,image);
  161. cvi_glossy.modify(canvas, options);
  162. }else {
  163. var canvas = document.createElement('canvas');
  164. if(canvas.getContext("2d")) {
  165. canvas.options = options;
  166. canvas.id = image.id;
  167. canvas.alt = image.alt;
  168. canvas.title = image.title;
  169. canvas.source = image.src;
  170. canvas.className = image.className;
  171. canvas.style.cssText = image.style.cssText;
  172. canvas.style.height = image.height+'px';
  173. canvas.style.width = image.width+'px';
  174. canvas.height = image.height;
  175. canvas.width = image.width;
  176. object.replaceChild(canvas,image);
  177. cvi_glossy.modify(canvas, options);
  178. }
  179. }
  180. // } catch (e) {
  181. // }
  182. }
  183. },
  184. modify: function(canvas, options) {
  185. try {
  186. var iradius = (typeof options['radius']=='number'?options['radius']:canvas.options['radius']); canvas.options['radius'] = iradius;
  187. var color = (typeof options['color']=='string'?options['color']:canvas.options['color']); canvas.options['color']=color;
  188. var color2 = (typeof options['color2']=='string'?options['color2']:canvas.options['color2']); canvas.options['color2']=color2;
  189. var gradient = (typeof options['gradient']=='string'?options['gradient']:canvas.options['gradient']); canvas.options['gradient']=gradient;
  190. var shadow = (typeof options['shadow']=='number'?options['shadow']:canvas.options['shadow']); canvas.options['shadow'] = shadow;
  191. var shade = (typeof options['shade']=='number'?options['shade']:canvas.options['shade']); canvas.options['shade'] = shade;
  192. var noshadow = (typeof options['noshadow']=='boolean'?options['noshadow']:canvas.options['noshadow']); canvas.options['noshadow']=noshadow;
  193. var noradius = (typeof options['noradius']=='boolean'?options['noradius']:canvas.options['noradius']); canvas.options['noradius']=noradius;
  194. var pressed = (typeof options['pressed']=='boolean'?options['pressed']:canvas.options['pressed']); canvas.options['pressed']=pressed;
  195. var highlight = (typeof options['highlight']=='boolean'?options['highlight']:canvas.options['highlight']); canvas.options['highlight']=highlight;
  196. var highlightcolor = '#e7cf04';
  197. var ihighlight = 0; if(isNaN(highlightcolor)) var ihighlight = (highlightcolor.match(/^#[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]$/i)?highlightcolor:0);
  198. var icolor = 0; if(isNaN(color)) var icolor = (color.match(/^#[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]$/i)?color:0);
  199. var icolor2 = 0; if(isNaN(color2)) var icolor2 = (color2.match(/^#[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]$/i)?color2:0);
  200. var igradient = (gradient.match(/^[dhv]/i)?gradient.substr(0,1):'v');
  201. var iw = canvas.width; var ih = canvas.height; var f = 0.25; var ishadow = shadow==0?0.0:shadow/100;
  202. var ishade = shade==0?0.5:Math.max(Math.min(shade,66),20)/100;
  203. var angle = 0, xr = 0, sr, r, os, is, style, head, foot, fill = '', shine = '', tmp = '';
  204. var IE = (document.all&&document.namespaces&&!window.opera?true:false);
  205. f = iradius>0?Math.min(Math.max(iradius,1),50)/100:f;
  206. if(IE==true) {
  207. iradius = Math.round(45*f); xr = Math.round(Math.max(Math.round((Math.min(iw,ih)/2)*f),4)/4)*4;
  208. }else {
  209. iradius = Math.max(Math.round((Math.min(iw,ih)/2)*f),4);
  210. }
  211. if(noshadow==false) {
  212. iradius = (IE==true?iradius:Math.round(iradius/4)*4); os = (IE==true?xr/4:iradius/4); sr = iradius*0.75; is = os; r = sr; sr = r*0.75;
  213. shine = '<v:roundrect arcsize="'+r+'%" strokeweight="0" filled="t" stroked="f" fillcolor="#000000" style="filter:Alpha(opacity='+(ishadow*100)+'), progid:dxImageTransform.Microsoft.Blur(PixelRadius='+is+', MakeShadow=false); zoom:1;margin:0;padding:0;display:block;position:absolute;top:'+is+'px;left:0px;width:'+(iw-(2*is))+'px;height:'+(ih-(3*is))+'px;"><v:fill color="#000000" opacity="1" /></v:roundrect>';
  214. tmp = '<v:rect strokeweight="0" filled="t" stroked="f" fillcolor="#ffffff" style="zoom:1;margin:0;padding:0;display:block;position:absolute;top:0px;left:0px;width:'+iw+'px;height:'+ih+'px;"><v:fill color="#ffffff" opacity="0" /></v:rect>';
  215. }else {
  216. os = (IE==true?xr/4:iradius/4); r = iradius; is = 0; sr = iradius*0.75;
  217. }
  218. if(noradius==true) {r = 0; sr = 0; os = 0; is = 0; shine = ''; tmp = '';}
  219. if(document.all && document.namespaces && !window.opera) {
  220. if(canvas.tagName.toUpperCase() == "DIV") {
  221. if(isNaN(icolor)) {
  222. fill = '<v:roundrect arcsize="'+r+'%" strokeweight="0" filled="t" stroked="f" fillcolor="#ffffff" style="zoom:1;margin:0;padding:0;display:block;position:absolute;top:0px;left:'+is+'px;width:'+(iw-(2*is))+'px;height:'+(ih-(2*is))+'px;">';
  223. if(isNaN(icolor2)) {
  224. if(igradient=='h') {angle = 90;}else if(igradient=='d') {angle = 45;}
  225. fill = fill + '<v:fill method="sigma" type="gradient" angle="'+angle+'" color="'+icolor2+'" color2="'+icolor+'" /></v:roundrect>';
  226. }else {
  227. fill = fill + '<v:fill color="'+icolor+'" /></v:roundrect>';
  228. }
  229. }
  230. head = '<v:group style="zoom:1;display:'+canvas.dpl+';margin:0;padding:0;position:relative;width:'+iw+'px;height:'+ih+'px;" coordsize="'+iw+','+ih+'">' + tmp;
  231. foot = '<v:roundrect arcsize="'+r+'%" strokeweight="0" filled="t" stroked="f" fillcolor="#ffffff" style="zoom:1;margin:0;padding:0;display:block;position:absolute;top:0px;left:'+is+'px;width:'+(iw-(2*is))+'px;height:'+(ih-(2*is))+'px;"><v:fill src="'+canvas.source+'" type="frame" /></v:roundrect><v:roundrect arcsize="'+(sr*2)+'%" strokeweight="0" filled="t" stroked="f" fillcolor="#ffffff" style="zoom:1;margin:0;padding:0;display:block;position:absolute;top:'+os+'px;left:'+(os+is)+'px;width:'+(iw-(2*os)-(2*is))+'px;height:'+((ih/2)-os-is)+'px;"><v:fill method="linear" type="gradient" angle="0" color="#ffffff" opacity="'+(ishade*0.25)+'" color2="#ffffff" o:opacity2="'+(ishade*1.5)+'" /></v:roundrect><v:roundrect arcsize="'+(r*2)+'%" strokeweight="0" filled="t" stroked="f" fillcolor="#000000" style="zoom:1;margin:0;padding:0;display:block;position:absolute;top:'+((ih/2)-is)+'px;left:'+is+'px;width:'+(iw-(2*is))+'px;height:'+((ih/2)-is)+'px;"><v:fill method="sigma" type="gradient" angle="180" color="#000000" opacity="0" color2="#000000" o:opacity2="'+ishade+'" /></v:roundrect></v:group>';
  232. canvas.innerHTML = head+shine+fill+foot;
  233. }
  234. }else {
  235. if(canvas.tagName.toUpperCase() == "CANVAS" && canvas.getContext("2d")) {
  236. var context = canvas.getContext("2d");
  237. var img = new Image();
  238. img.onload = function() {
  239. context.clearRect(0,0,iw,ih);
  240. if(noshadow==false) glossyShadow(context,0,0,iw,ih,iradius,ishadow);
  241. context.save();
  242. roundedRect(context,is,0,iw-(is*2),ih,r);
  243. context.clip();
  244. if(isNaN(icolor)) {
  245. if(isNaN(icolor2)) {
  246. if(igradient=='h') {
  247. style = context.createLinearGradient(0,0,iw,0);
  248. }else if(igradient=='d') {
  249. style = context.createLinearGradient(0,0,iw-(is*2),ih-(is*2));
  250. }else {
  251. style = context.createLinearGradient(0,0,0,ih-(is*2));
  252. }
  253. style.addColorStop(0,icolor);
  254. style.addColorStop(1,icolor2);
  255. context.beginPath();
  256. context.rect(0,0,iw,ih-(is*2));
  257. context.closePath();
  258. context.fillStyle = style;
  259. context.fill();
  260. }else {
  261. if (highlight){
  262. context.fillStyle = ihighlight;
  263. context.fillRect(0,0,iw,ih+(is*2));
  264. context.fillStyle = icolor;
  265. context.fillRect(((os+is)*2),((os+is)*2),iw-(4*(os+is)),ih-(4*(os+is)));
  266. }
  267. else{
  268. context.fillStyle = icolor;
  269. context.fillRect(0,0,iw,ih-(is*2));
  270. }
  271. }
  272. }else {
  273. context.clearRect(0,0,iw,ih);
  274. }
  275. context.drawImage(img,is,0,iw-(is*2),ih-(is*4));
  276. if (pressed){
  277. addBright(context,os+is,os,iw-(2*(os+is)),(ih/2)-os,sr,ishade*1.25);
  278. addDark(context,os+is,(ih/2)-is,iw-(2*(os+is)),(ih/2)-((os+is)),sr,ishade*1.75);
  279. }
  280. else{
  281. addBright(context,os+is,os,iw-(2*(os+is)),(ih/2)-os,sr,ishade*1.5);
  282. addDark(context,os+is,(ih/2)-is,iw-(2*(os+is)),(ih/2)-((os+is)),sr,ishade);
  283. }
  284. if(noradius==false) addFrame(context,is,os,iw-(is*2),ih-(is*2),r,ishade*0.5)
  285. context.restore();
  286. }
  287. img.src = canvas.source;
  288. }
  289. }
  290. } catch (e) {
  291. }
  292. },
  293. replace : function(canvas) {
  294. var object = canvas.parentNode;
  295. var img = document.createElement('img');
  296. img.id = canvas.id;
  297. img.alt = canvas.alt;
  298. img.title = canvas.title;
  299. img.src = canvas.source;
  300. img.className = canvas.className;
  301. img.style.cssText = canvas.style.cssText;
  302. img.style.height = canvas.height+'px';
  303. img.style.width = canvas.width+'px';
  304. object.replaceChild(img,canvas);
  305. },
  306. remove : function(canvas) {
  307. if(document.all && document.namespaces && !window.opera) {
  308. if(canvas.tagName.toUpperCase() == "DIV") {
  309. cvi_glossy.replace(canvas);
  310. }
  311. }else {
  312. if(canvas.tagName.toUpperCase() == "CANVAS") {
  313. cvi_glossy.replace(canvas);
  314. }
  315. }
  316. }
  317. }