|
8 | 8 | // Function to load sprite manifest |
9 | 9 | async function loadSpriteManifest() { |
10 | 10 | try { |
11 | | - var response = await fetch("build/sprite-manifest.json"); |
| 11 | + const response = await fetch("build/sprite-manifest.json"); |
12 | 12 | if (!response.ok) { |
13 | 13 | throw new Error("Failed to load sprite manifest: " + response.status); |
14 | 14 | } |
15 | | - var manifest = await response.json(); |
| 15 | + const manifest = await response.json(); |
16 | 16 | return manifest.sprites || []; |
17 | 17 | } catch (error) { |
18 | 18 | throw new Error("Invalid sprite manifest: " + error.message); |
|
21 | 21 |
|
22 | 22 | // Function to load and inject a single sprite |
23 | 23 | async function loadSprite(spriteUrl) { |
24 | | - var response = await fetch(spriteUrl); |
| 24 | + const response = await fetch(spriteUrl); |
25 | 25 | if (!response.ok) { |
26 | 26 | throw new Error( |
27 | 27 | "Failed to load sprite: " + spriteUrl + " (" + response.status + ")" |
28 | 28 | ); |
29 | 29 | } |
30 | 30 |
|
31 | | - var svgContent = await response.text(); |
| 31 | + const svgContent = await response.text(); |
32 | 32 |
|
33 | | - // Create a div to hold the sprite |
34 | | - var spriteContainer = document.createElement("div"); |
35 | | - spriteContainer.style.display = "none"; |
| 33 | + const existingContainer = document.getElementById("svg-sprites"); |
| 34 | + const spriteContainer = document.createElement("div"); |
36 | 35 | spriteContainer.innerHTML = svgContent; |
37 | 36 |
|
38 | | - // Insert at the beginning of body |
39 | | - document.body.insertBefore(spriteContainer, document.body.firstChild); |
| 37 | + existingContainer.appendChild(spriteContainer.firstChild); |
40 | 38 |
|
41 | 39 | console.log("SVG sprite loaded:", spriteUrl); |
42 | 40 | } |
43 | 41 |
|
44 | 42 | // Function to load all sprites |
45 | 43 | async function loadAllSprites(spritePaths) { |
46 | | - var promises = spritePaths.map(function (spritePath) { |
| 44 | + const promises = spritePaths.map(function (spritePath) { |
47 | 45 | return loadSprite("build/" + spritePath); |
48 | 46 | }); |
49 | 47 |
|
|
58 | 56 | return; |
59 | 57 | } |
60 | 58 |
|
61 | | - console.log("Initializing sprite loader..."); |
62 | | - |
63 | 59 | try { |
64 | | - var spritePaths = await loadSpriteManifest(); |
65 | | - console.log("Found", spritePaths.length, "sprite(s) to load"); |
| 60 | + const spritePaths = await loadSpriteManifest(); |
66 | 61 | await loadAllSprites(spritePaths); |
67 | | - console.log("All sprites loaded successfully"); |
68 | 62 | } catch (error) { |
69 | 63 | console.warn("Sprite loader failed:", error.message); |
70 | 64 | } |
|
0 commit comments