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