holder.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. Holder - 2.0 - client side image placeholders
  3. (c) 2012-2013 Ivan Malopinsky / http://imsky.co
  4. Provided under the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0
  5. Commercial use requires attribution.
  6. */
  7. var Holder = Holder || {};
  8. (function (app, win) {
  9. var preempted = false,
  10. fallback = false,
  11. canvas = document.createElement('canvas');
  12. //getElementsByClassName polyfill
  13. document.getElementsByClassName||(document.getElementsByClassName=function(e){var t=document,n,r,i,s=[];if(t.querySelectorAll)return t.querySelectorAll("."+e);if(t.evaluate){r=".//*[contains(concat(' ', @class, ' '), ' "+e+" ')]",n=t.evaluate(r,t,null,0,null);while(i=n.iterateNext())s.push(i)}else{n=t.getElementsByTagName("*"),r=new RegExp("(^|\\s)"+e+"(\\s|$)");for(i=0;i<n.length;i++)r.test(n[i].className)&&s.push(n[i])}return s})
  14. //getComputedStyle polyfill
  15. window.getComputedStyle||(window.getComputedStyle=function(e,t){return this.el=e,this.getPropertyValue=function(t){var n=/(\-([a-z]){1})/g;return t=="float"&&(t="styleFloat"),n.test(t)&&(t=t.replace(n,function(){return arguments[2].toUpperCase()})),e.currentStyle[t]?e.currentStyle[t]:null},this})
  16. //http://javascript.nwbox.com/ContentLoaded by Diego Perini with modifications
  17. function contentLoaded(n,t){var l="complete",s="readystatechange",u=!1,h=u,c=!0,i=n.document,a=i.documentElement,e=i.addEventListener?"addEventListener":"attachEvent",v=i.addEventListener?"removeEventListener":"detachEvent",f=i.addEventListener?"":"on",r=function(e){(e.type!=s||i.readyState==l)&&((e.type=="load"?n:i)[v](f+e.type,r,u),!h&&(h=!0)&&t.call(n,null))},o=function(){try{a.doScroll("left")}catch(n){setTimeout(o,50);return}r("poll")};if(i.readyState==l)t.call(n,"lazy");else{if(i.createEventObject&&a.doScroll){try{c=!n.frameElement}catch(y){}c&&o()}i[e](f+"DOMContentLoaded",r,u),i[e](f+s,r,u),n[e](f+"load",r,u)}};
  18. //https://gist.github.com/991057 by Jed Schmidt with modifications
  19. function selector(a){
  20. a=a.match(/^(\W)?(.*)/);var b=document["getElement"+(a[1]?a[1]=="#"?"ById":"sByClassName":"sByTagName")](a[2]);
  21. var ret=[]; b!=null&&(b.length?ret=b:b.length==0?ret=b:ret=[b]); return ret;
  22. }
  23. //shallow object property extend
  24. function extend(a,b){var c={};for(var d in a)c[d]=a[d];for(var e in b)c[e]=b[e];return c}
  25. //hasOwnProperty polyfill
  26. if (!Object.prototype.hasOwnProperty)
  27. Object.prototype.hasOwnProperty = function(prop) {
  28. var proto = this.__proto__ || this.constructor.prototype;
  29. return (prop in this) && (!(prop in proto) || proto[prop] !== this[prop]);
  30. }
  31. function text_size(width, height, template) {
  32. height = parseInt(height,10);
  33. width = parseInt(width,10);
  34. var bigSide = Math.max(height, width)
  35. var smallSide = Math.min(height, width)
  36. var scale = 1 / 12;
  37. var newHeight = Math.min(smallSide * 0.75, 0.75 * bigSide * scale);
  38. return {
  39. height: Math.round(Math.max(template.size, newHeight))
  40. }
  41. }
  42. function draw(ctx, dimensions, template, ratio) {
  43. var ts = text_size(dimensions.width, dimensions.height, template);
  44. var text_height = ts.height;
  45. var width = dimensions.width * ratio,
  46. height = dimensions.height * ratio;
  47. var font = template.font ? template.font : "sans-serif";
  48. canvas.width = width;
  49. canvas.height = height;
  50. ctx.textAlign = "center";
  51. ctx.textBaseline = "middle";
  52. ctx.fillStyle = template.background;
  53. ctx.fillRect(0, 0, width, height);
  54. ctx.fillStyle = template.foreground;
  55. ctx.font = "bold " + text_height + "px " + font;
  56. var text = template.text ? template.text : (Math.floor(dimensions.width) + "x" + Math.floor(dimensions.height));
  57. var text_width = ctx.measureText(text).width;
  58. if (text_width / width >= 0.75) {
  59. text_height = Math.floor(text_height * 0.75 * (width/text_width));
  60. }
  61. //Resetting font size if necessary
  62. ctx.font = "bold " + (text_height * ratio) + "px " + font;
  63. ctx.fillText(text, (width / 2), (height / 2), width);
  64. return canvas.toDataURL("image/png");
  65. }
  66. function render(mode, el, holder, src) {
  67. var dimensions = holder.dimensions,
  68. theme = holder.theme,
  69. text = holder.text ? decodeURIComponent(holder.text) : holder.text;
  70. var dimensions_caption = dimensions.width + "x" + dimensions.height;
  71. theme = (text ? extend(theme, {
  72. text: text
  73. }) : theme);
  74. theme = (holder.font ? extend(theme, {
  75. font: holder.font
  76. }) : theme);
  77. if (mode == "image") {
  78. el.setAttribute("data-src", src);
  79. el.setAttribute("alt", text ? text : theme.text ? theme.text + " [" + dimensions_caption + "]" : dimensions_caption);
  80. if (fallback || !holder.auto) {
  81. el.style.width = dimensions.width + "px";
  82. el.style.height = dimensions.height + "px";
  83. }
  84. if (fallback) {
  85. el.style.backgroundColor = theme.background;
  86. } else {
  87. el.setAttribute("src", draw(ctx, dimensions, theme, ratio));
  88. }
  89. } else if (mode == "background") {
  90. if (!fallback) {
  91. el.style.backgroundImage = "url(" + draw(ctx, dimensions, theme, ratio) + ")";
  92. el.style.backgroundSize = dimensions.width + "px " + dimensions.height + "px";
  93. }
  94. } else if (mode == "fluid") {
  95. el.setAttribute("data-src", src);
  96. el.setAttribute("alt", text ? text : theme.text ? theme.text + " [" + dimensions_caption + "]" : dimensions_caption);
  97. if (dimensions.height.substr(-1) == "%") {
  98. el.style.height = dimensions.height
  99. } else {
  100. el.style.height = dimensions.height + "px"
  101. }
  102. if (dimensions.width.substr(-1) == "%") {
  103. el.style.width = dimensions.width
  104. } else {
  105. el.style.width = dimensions.width + "px"
  106. }
  107. if (el.style.display == "inline" || el.style.display == "") {
  108. el.style.display = "block";
  109. }
  110. if (fallback) {
  111. el.style.backgroundColor = theme.background;
  112. } else {
  113. el.holderData = holder;
  114. fluid_images.push(el);
  115. fluid_update(el);
  116. }
  117. }
  118. };
  119. function fluid_update(element) {
  120. var images;
  121. if (element.nodeType == null) {
  122. images = fluid_images;
  123. } else {
  124. images = [element]
  125. }
  126. for (i in images) {
  127. var el = images[i]
  128. if (el.holderData) {
  129. var holder = el.holderData;
  130. el.setAttribute("src", draw(ctx, {
  131. height: el.clientHeight,
  132. width: el.clientWidth
  133. }, holder.theme, ratio));
  134. }
  135. }
  136. }
  137. function parse_flags(flags, options) {
  138. var ret = {
  139. theme: settings.themes.gray
  140. }, render = false;
  141. for (sl = flags.length, j = 0; j < sl; j++) {
  142. var flag = flags[j];
  143. if (app.flags.dimensions.match(flag)) {
  144. render = true;
  145. ret.dimensions = app.flags.dimensions.output(flag);
  146. } else if (app.flags.fluid.match(flag)) {
  147. render = true;
  148. ret.dimensions = app.flags.fluid.output(flag);
  149. ret.fluid = true;
  150. } else if (app.flags.colors.match(flag)) {
  151. ret.theme = app.flags.colors.output(flag);
  152. } else if (options.themes[flag]) {
  153. //If a theme is specified, it will override custom colors
  154. ret.theme = options.themes[flag];
  155. } else if (app.flags.text.match(flag)) {
  156. ret.text = app.flags.text.output(flag);
  157. } else if (app.flags.font.match(flag)) {
  158. ret.font = app.flags.font.output(flag);
  159. } else if (app.flags.auto.match(flag)) {
  160. ret.auto = true;
  161. }
  162. }
  163. return render ? ret : false;
  164. };
  165. if (!canvas.getContext) {
  166. fallback = true;
  167. } else {
  168. if (canvas.toDataURL("image/png")
  169. .indexOf("data:image/png") < 0) {
  170. //Android doesn't support data URI
  171. fallback = true;
  172. } else {
  173. var ctx = canvas.getContext("2d");
  174. }
  175. }
  176. var dpr = 1, bsr = 1;
  177. if(!fallback){
  178. dpr = window.devicePixelRatio || 1,
  179. bsr = ctx.webkitBackingStorePixelRatio || ctx.mozBackingStorePixelRatio || ctx.msBackingStorePixelRatio || ctx.oBackingStorePixelRatio || ctx.backingStorePixelRatio || 1;
  180. }
  181. var ratio = dpr / bsr;
  182. var fluid_images = [];
  183. var settings = {
  184. domain: "holder.js",
  185. images: "img",
  186. bgnodes: ".holderjs",
  187. themes: {
  188. "gray": {
  189. background: "#eee",
  190. foreground: "#aaa",
  191. size: 12
  192. },
  193. "social": {
  194. background: "#3a5a97",
  195. foreground: "#fff",
  196. size: 12
  197. },
  198. "industrial": {
  199. background: "#434A52",
  200. foreground: "#C2F200",
  201. size: 12
  202. }
  203. },
  204. stylesheet: ".holderjs-fluid {font-size:16px;font-weight:bold;text-align:center;font-family:sans-serif;margin:0}"
  205. };
  206. app.flags = {
  207. dimensions: {
  208. regex: /^(\d+)x(\d+)$/,
  209. output: function (val) {
  210. var exec = this.regex.exec(val);
  211. return {
  212. width: +exec[1],
  213. height: +exec[2]
  214. }
  215. }
  216. },
  217. fluid: {
  218. regex: /^([0-9%]+)x([0-9%]+)$/,
  219. output: function (val) {
  220. var exec = this.regex.exec(val);
  221. return {
  222. width: exec[1],
  223. height: exec[2]
  224. }
  225. }
  226. },
  227. colors: {
  228. regex: /#([0-9a-f]{3,})\:#([0-9a-f]{3,})/i,
  229. output: function (val) {
  230. var exec = this.regex.exec(val);
  231. return {
  232. size: settings.themes.gray.size,
  233. foreground: "#" + exec[2],
  234. background: "#" + exec[1]
  235. }
  236. }
  237. },
  238. text: {
  239. regex: /text\:(.*)/,
  240. output: function (val) {
  241. return this.regex.exec(val)[1];
  242. }
  243. },
  244. font: {
  245. regex: /font\:(.*)/,
  246. output: function (val) {
  247. return this.regex.exec(val)[1];
  248. }
  249. },
  250. auto: {
  251. regex: /^auto$/
  252. }
  253. }
  254. for (var flag in app.flags) {
  255. if (!app.flags.hasOwnProperty(flag)) continue;
  256. app.flags[flag].match = function (val) {
  257. return val.match(this.regex)
  258. }
  259. }
  260. app.add_theme = function (name, theme) {
  261. name != null && theme != null && (settings.themes[name] = theme);
  262. return app;
  263. };
  264. app.add_image = function (src, el) {
  265. var node = selector(el);
  266. if (node.length) {
  267. for (var i = 0, l = node.length; i < l; i++) {
  268. var img = document.createElement("img")
  269. img.setAttribute("data-src", src);
  270. node[i].appendChild(img);
  271. }
  272. }
  273. return app;
  274. };
  275. app.run = function (o) {
  276. var options = extend(settings, o),
  277. images = [], imageNodes = [], bgnodes = [];
  278. if(typeof(options.images) == "string"){
  279. imageNodes = selector(options.images);
  280. }
  281. else if (window.NodeList && options.images instanceof window.NodeList) {
  282. imageNodes = options.images;
  283. } else if (window.Node && options.images instanceof window.Node) {
  284. imageNodes = [options.images];
  285. }
  286. if(typeof(options.bgnodes) == "string"){
  287. bgnodes = selector(options.bgnodes);
  288. } else if (window.NodeList && options.elements instanceof window.NodeList) {
  289. bgnodes = options.bgnodes;
  290. } else if (window.Node && options.bgnodes instanceof window.Node) {
  291. bgnodes = [options.bgnodes];
  292. }
  293. preempted = true;
  294. for (i = 0, l = imageNodes.length; i < l; i++) images.push(imageNodes[i]);
  295. var holdercss = document.getElementById("holderjs-style");
  296. if (!holdercss) {
  297. holdercss = document.createElement("style");
  298. holdercss.setAttribute("id", "holderjs-style");
  299. holdercss.type = "text/css";
  300. document.getElementsByTagName("head")[0].appendChild(holdercss);
  301. }
  302. if (!options.nocss) {
  303. if (holdercss.styleSheet) {
  304. holdercss.styleSheet.cssText += options.stylesheet;
  305. } else {
  306. holdercss.appendChild(document.createTextNode(options.stylesheet));
  307. }
  308. }
  309. var cssregex = new RegExp(options.domain + "\/(.*?)\"?\\)");
  310. for (var l = bgnodes.length, i = 0; i < l; i++) {
  311. var src = window.getComputedStyle(bgnodes[i], null)
  312. .getPropertyValue("background-image");
  313. var flags = src.match(cssregex);
  314. var bgsrc = bgnodes[i].getAttribute("data-background-src");
  315. if (flags) {
  316. var holder = parse_flags(flags[1].split("/"), options);
  317. if (holder) {
  318. render("background", bgnodes[i], holder, src);
  319. }
  320. }
  321. else if(bgsrc != null){
  322. var holder = parse_flags(bgsrc.substr(bgsrc.lastIndexOf(options.domain) + options.domain.length + 1)
  323. .split("/"), options);
  324. if(holder){
  325. render("background", bgnodes[i], holder, src);
  326. }
  327. }
  328. }
  329. for (l = images.length, i = 0; i < l; i++) {
  330. var attr_src = attr_data_src = src = null;
  331. try{
  332. attr_src = images[i].getAttribute("src");
  333. attr_datasrc = images[i].getAttribute("data-src");
  334. }catch(e){}
  335. if (attr_datasrc == null && !! attr_src && attr_src.indexOf(options.domain) >= 0) {
  336. src = attr_src;
  337. } else if ( !! attr_datasrc && attr_datasrc.indexOf(options.domain) >= 0) {
  338. src = attr_datasrc;
  339. }
  340. if (src) {
  341. var holder = parse_flags(src.substr(src.lastIndexOf(options.domain) + options.domain.length + 1)
  342. .split("/"), options);
  343. if (holder) {
  344. if (holder.fluid) {
  345. render("fluid", images[i], holder, src)
  346. } else {
  347. render("image", images[i], holder, src);
  348. }
  349. }
  350. }
  351. }
  352. return app;
  353. };
  354. contentLoaded(win, function () {
  355. if (window.addEventListener) {
  356. window.addEventListener("resize", fluid_update, false);
  357. window.addEventListener("orientationchange", fluid_update, false);
  358. } else {
  359. window.attachEvent("onresize", fluid_update)
  360. }
  361. preempted || app.run();
  362. });
  363. if (typeof define === "function" && define.amd) {
  364. define("Holder", [], function () {
  365. return app;
  366. });
  367. }
  368. })(Holder, window);