image.html 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>RequireJS image plugin</title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. </head>
  8. <body>
  9. <div id="wrapper">
  10. <h1>RequireJS image plugin</h1>
  11. <p>Note that <code>waitSeconds</code> should be large enough to load all the images, otherwise module may timeout.</p>
  12. <p>Use this plugin only on specific cases and make sure you set a large <a href="http://requirejs.org/docs/api.html#config">waitSeconds</a> (default is 7 seconds).</p>
  13. <h2>Notes:</h2>
  14. <ul>
  15. <li>Image paths are relative to the HTML file by default.</li>
  16. <li>Support absolute paths as well.</li>
  17. <li>Appending <code>!bust</code> to the file name will avoid caching the image.</li>
  18. <li>Appending <code>!rel</code> to the file name will load image realtive to baseUrl or module path.</li>
  19. <li>It will always return the same image object unless you use the <code>!bust</code> flag, so you may need to clone the image element before inserting it multiple times into the same document.</li>
  20. </ul>
  21. <hr />
  22. </div>
  23. <script src="../lib/require.js"></script>
  24. <script>
  25. require.config({
  26. waitSeconds : 45, //should be enough to load images
  27. paths : {
  28. image : '../src/image' //alias to plugin
  29. }
  30. });
  31. require([
  32. 'image!img/lol_cat.jpg',
  33. 'image!http://30.media.tumblr.com/tumblr_lgd1neNYSL1qbwkzvo1_500.jpg',
  34. 'image!img/bike.jpg!bust',
  35. 'image!img/bike.jpg!bust',
  36. 'image!img/lol_cat.jpg',
  37. 'img/relativePath.js'
  38. ], function(cat, awesome, bike1, bike2, sameCat, relative){
  39. var wrapper = document.getElementById('wrapper');
  40. //add loaded images to the document!
  41. //returns an Image object..
  42. wrapper.appendChild(awesome);
  43. wrapper.appendChild(cat);
  44. //requireJS will return same image object unless you use `!bust`
  45. var sameBike = document.createElement('div');
  46. sameBike.innerHTML = 'Is same bike cat? : '+ (bike1 === bike2);
  47. wrapper.appendChild(sameBike);
  48. wrapper.appendChild(bike1);
  49. wrapper.appendChild(bike2);
  50. var sameLol = document.createElement('div');
  51. sameLol.innerHTML = 'Is same lol cat? : '+ (cat === sameCat);
  52. wrapper.appendChild(sameLol);
  53. //so we need to "deep-clone" the Element to be able
  54. //to insert it multiple times into the same document
  55. //wrapper.appendChild(sameCat.cloneNode(true)); //insert a clone of the image
  56. wrapper.appendChild(sameCat);//swap image position
  57. relative.init(wrapper);
  58. });
  59. </script>
  60. </body>
  61. </html>