Spaces:
Running on Zero
Running on Zero
Prevent missing images from black-screening canvas games
Browse files
app.py
CHANGED
|
@@ -1236,6 +1236,24 @@ def embed_assets(html_code: str, assets: dict[str, str], specs: list[AssetSpec])
|
|
| 1236 |
HTMLImageElement.prototype.__generatedAssetMapper = true;
|
| 1237 |
}}
|
| 1238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1239 |
window.addEventListener("DOMContentLoaded", function () {{
|
| 1240 |
document.querySelectorAll("img").forEach(function (img) {{
|
| 1241 |
var mapped = pickAsset(img.getAttribute("src") || img.src);
|
|
|
|
| 1236 |
HTMLImageElement.prototype.__generatedAssetMapper = true;
|
| 1237 |
}}
|
| 1238 |
|
| 1239 |
+
var originalDrawImage = CanvasRenderingContext2D.prototype.drawImage;
|
| 1240 |
+
if (!CanvasRenderingContext2D.prototype.__generatedAssetDrawGuard) {{
|
| 1241 |
+
CanvasRenderingContext2D.prototype.drawImage = function (image) {{
|
| 1242 |
+
try {{
|
| 1243 |
+
if (image instanceof HTMLImageElement) {{
|
| 1244 |
+
var current = image.getAttribute("src") || image.src || "";
|
| 1245 |
+
var mapped = pickAsset(current);
|
| 1246 |
+
if (mapped && mapped !== current) image.src = mapped;
|
| 1247 |
+
if (!image.complete || image.naturalWidth === 0 || image.naturalHeight === 0) return;
|
| 1248 |
+
}}
|
| 1249 |
+
return originalDrawImage.apply(this, arguments);
|
| 1250 |
+
}} catch (error) {{
|
| 1251 |
+
return;
|
| 1252 |
+
}}
|
| 1253 |
+
}};
|
| 1254 |
+
CanvasRenderingContext2D.prototype.__generatedAssetDrawGuard = true;
|
| 1255 |
+
}}
|
| 1256 |
+
|
| 1257 |
window.addEventListener("DOMContentLoaded", function () {{
|
| 1258 |
document.querySelectorAll("img").forEach(function (img) {{
|
| 1259 |
var mapped = pickAsset(img.getAttribute("src") || img.src);
|