{"__v":5,"_id":"568be56983d2061900d86ce9","category":{"__v":8,"_id":"5666dac5d784a70d00397bcb","pages":["5666dda06819320d000c2e19","567856b9e5da8d0d00591c66","567861b8e5da8d0d00591c7c","568bdbf9e3e30f0d0021e07c","568be56983d2061900d86ce9","5697f7a60b09a41900b245d7","56abb8b49327b30d00f7c234","56d088977c2e100b000af8f0"],"project":"564de2dbfe07a81700b5c3a5","version":"564de2dbfe07a81700b5c3a8","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-12-08T13:27:33.662Z","from_sync":false,"order":5,"slug":"tutorials","title":"Tutorials"},"parentDoc":null,"project":"564de2dbfe07a81700b5c3a5","user":"564de2b4fe07a81700b5c3a4","version":{"__v":8,"_id":"564de2dbfe07a81700b5c3a8","project":"564de2dbfe07a81700b5c3a5","createdAt":"2015-11-19T14:55:23.838Z","releaseDate":"2015-11-19T14:55:23.837Z","categories":["564de2ddfe07a81700b5c3a9","564df317826645210097a890","564df3217c8f372b00b934df","564e5227c3553e0d003e53ba","5666dac5d784a70d00397bcb","56cd08ddd98d851d00c0c3bd","56e9a50946bfd60e008840a7","5718e37bf8f7de1900683fad"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-01-05T15:46:49.652Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":4,"body":"When loading images, best practice is to show a placeholder instead of a broken image. A great place to see this in action is on Pinterest. They achieve this by showing the dominant color of the image as the background placeholder, only showing the image when it has been loaded.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/CfJlAogoTIiaZka74hqW_pinterest_loading.gif\",\n        \"pinterest_loading.gif\",\n        \"500\",\n        \"500\",\n        \"#ecebec\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\nOur goal is to recreate this effect using Embedly's API. Below is a demo utilizing Extract and Display to recreate this image loading technique.\n[block:embed]\n{\n  \"html\": \"<iframe class=\\\"embedly-embed\\\" src=\\\"//cdn.embedly.com/widgets/media.html?url=https%3A%2F%2Fjsfiddle.net%2Fo4v70f7f%2F1%2F%3Fheight%3D550%26tabs%3Dresult%2Cjs%2Chtml%2Ccss&src=https%3A%2F%2Fjsfiddle.net%2Fo4v70f7f%2F1%2Fembedded%2Fresult%2Cjs%2Chtml%2Ccss%2F&type=text%2Fhtml&key=02466f963b9b4bb8845a05b53d3235d7&schema=jsfiddle\\\" width=\\\"600\\\" height=\\\"550\\\" scrolling=\\\"no\\\" frameborder=\\\"0\\\" allowfullscreen></iframe>\",\n  \"url\": \"https://jsfiddle.net/o4v70f7f/1/?height=550&tabs=result,js,html,css\",\n  \"title\": \"Edit fiddle - JSFiddle\",\n  \"favicon\": \"https://jsfiddle.net/favicon.png\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"function rgbToHex(r, g, b) {\\n  return \\\"#\\\" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);\\n}\\n\\nfunction displayImage(img, color, provider) {\\n\\n  var width = 400,\\n    height = Math.ceil(img.height * (width / img.width));\\n\\n  // Add the block to the results section.\\n  $('.results').html(\\n    '<div class=\\\"background-wrapper\\\"><div id=\\\"backgroundBlock\\\"><span>' +\\n    provider + '</span></div></div>');\\n\\n  // Create a wrapper.\\n  $('.background-wrapper').css({\\n    width: width + 20,\\n    height: height + 20\\n  });\\n  // Build the actually placeholder.\\n  $('#backgroundBlock').css({\\n    background: color,\\n    width: width,\\n    height: height\\n  });\\n  // Center the text.\\n  $('#backgroundBlock span').css('padding-top', Math.floor(height / 2) - 10);\\n\\n  // Animate the fade in.\\n  $('.background-wrapper').animate({\\n    opacity: 1\\n  }, 500);\\n\\n  // Use Embedly display to resize the image, we add a timestamp in the demo o\\n  var src = $.embedly.display.resize(img.url, {\\n      query: {\\n        width: width,\\n        height: height\\n      },\\n      key: $.embedly.defaults.key\\n    }) +\\n    '&_' + Math.floor(Math.random() * 1000);\\n\\n  // Create the img element\\n  var elem = document.createElement('img');\\n\\n  // Set up the onload handler.\\n  var loaded = false;\\n  var handler = function() {\\n    if (loaded) {\\n      return;\\n    }\\n    loaded = true;\\n    // Fade the image into view.\\n    $('#backgroundBlock img').animate({\\n      opacity: 1\\n    }, 1000);\\n  };\\n\\n  // Add the attributes to the image.\\n  elem.onload = handler;\\n  elem.src = src;\\n  elem.style.display = 'block';\\n  // Place the image in the correct ID.\\n  document.getElementById('backgroundBlock').appendChild(elem);\\n\\n  // If the image is already in the browsers cache call the handler.\\n  if (elem.complete) {\\n    handler();\\n  }\\n};\\n\\n$('.collapse .button').on('click', function() {\\n  // Grab the input.\\n  var url = $('input').val();\\n  // Make the call to Embedly Extract\\n  $.embedly.extract(url)\\n    .progress(function(obj) {\\n      // Grab images and create the colors.\\n      var img = obj.images[0],\\n        hex = rgbToHex(img.colors[0].color[0], img.colors[0].color[1], img.colors[0].color[2]);\\n      // Display the image inline.\\n      displayImage(img, hex, obj.provider_display);\\n    });\\n});\",\n      \"language\": \"javascript\"\n    },\n    {\n      \"code\": \".background-wrapper {\\n  margin: 0 auto;\\n}\\n\\n#backgroundBlock {\\n  position: relative;\\n}\\n\\n#backgroundBlock span {\\n  color: #fff;\\n  font-weight: bold;\\n  margin: 0 auto;\\n  text-align: center;\\n  display: block;\\n}\\n\\n#backgroundBlock img {\\n  position: absolute;\\n  top: 0;\\n  left: 0;\\n  opacity: 0;\\n}\",\n      \"language\": \"css\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"color-background-placeholders-for-loading-images","type":"basic","title":"Color Background Placeholders for Loading Images"}

Color Background Placeholders for Loading Images


When loading images, best practice is to show a placeholder instead of a broken image. A great place to see this in action is on Pinterest. They achieve this by showing the dominant color of the image as the background placeholder, only showing the image when it has been loaded. [block:image] { "images": [ { "image": [ "https://files.readme.io/CfJlAogoTIiaZka74hqW_pinterest_loading.gif", "pinterest_loading.gif", "500", "500", "#ecebec", "" ] } ] } [/block] Our goal is to recreate this effect using Embedly's API. Below is a demo utilizing Extract and Display to recreate this image loading technique. [block:embed] { "html": "<iframe class=\"embedly-embed\" src=\"//cdn.embedly.com/widgets/media.html?url=https%3A%2F%2Fjsfiddle.net%2Fo4v70f7f%2F1%2F%3Fheight%3D550%26tabs%3Dresult%2Cjs%2Chtml%2Ccss&src=https%3A%2F%2Fjsfiddle.net%2Fo4v70f7f%2F1%2Fembedded%2Fresult%2Cjs%2Chtml%2Ccss%2F&type=text%2Fhtml&key=02466f963b9b4bb8845a05b53d3235d7&schema=jsfiddle\" width=\"600\" height=\"550\" scrolling=\"no\" frameborder=\"0\" allowfullscreen></iframe>", "url": "https://jsfiddle.net/o4v70f7f/1/?height=550&tabs=result,js,html,css", "title": "Edit fiddle - JSFiddle", "favicon": "https://jsfiddle.net/favicon.png" } [/block] [block:code] { "codes": [ { "code": "function rgbToHex(r, g, b) {\n return \"#\" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);\n}\n\nfunction displayImage(img, color, provider) {\n\n var width = 400,\n height = Math.ceil(img.height * (width / img.width));\n\n // Add the block to the results section.\n $('.results').html(\n '<div class=\"background-wrapper\"><div id=\"backgroundBlock\"><span>' +\n provider + '</span></div></div>');\n\n // Create a wrapper.\n $('.background-wrapper').css({\n width: width + 20,\n height: height + 20\n });\n // Build the actually placeholder.\n $('#backgroundBlock').css({\n background: color,\n width: width,\n height: height\n });\n // Center the text.\n $('#backgroundBlock span').css('padding-top', Math.floor(height / 2) - 10);\n\n // Animate the fade in.\n $('.background-wrapper').animate({\n opacity: 1\n }, 500);\n\n // Use Embedly display to resize the image, we add a timestamp in the demo o\n var src = $.embedly.display.resize(img.url, {\n query: {\n width: width,\n height: height\n },\n key: $.embedly.defaults.key\n }) +\n '&_' + Math.floor(Math.random() * 1000);\n\n // Create the img element\n var elem = document.createElement('img');\n\n // Set up the onload handler.\n var loaded = false;\n var handler = function() {\n if (loaded) {\n return;\n }\n loaded = true;\n // Fade the image into view.\n $('#backgroundBlock img').animate({\n opacity: 1\n }, 1000);\n };\n\n // Add the attributes to the image.\n elem.onload = handler;\n elem.src = src;\n elem.style.display = 'block';\n // Place the image in the correct ID.\n document.getElementById('backgroundBlock').appendChild(elem);\n\n // If the image is already in the browsers cache call the handler.\n if (elem.complete) {\n handler();\n }\n};\n\n$('.collapse .button').on('click', function() {\n // Grab the input.\n var url = $('input').val();\n // Make the call to Embedly Extract\n $.embedly.extract(url)\n .progress(function(obj) {\n // Grab images and create the colors.\n var img = obj.images[0],\n hex = rgbToHex(img.colors[0].color[0], img.colors[0].color[1], img.colors[0].color[2]);\n // Display the image inline.\n displayImage(img, hex, obj.provider_display);\n });\n});", "language": "javascript" }, { "code": ".background-wrapper {\n margin: 0 auto;\n}\n\n#backgroundBlock {\n position: relative;\n}\n\n#backgroundBlock span {\n color: #fff;\n font-weight: bold;\n margin: 0 auto;\n text-align: center;\n display: block;\n}\n\n#backgroundBlock img {\n position: absolute;\n top: 0;\n left: 0;\n opacity: 0;\n}", "language": "css" } ] } [/block]