Spaces:
Running
Running
Upload 50 files
Browse files- js/aurora2d.js +12 -1
js/aurora2d.js
CHANGED
|
@@ -120,7 +120,10 @@ class AuroraCanvas {
|
|
| 120 |
this.stars = Array.from({ length: 70 }, () => [Math.random(), Math.random(), Math.random() * 0.8 + 0.2]);
|
| 121 |
this.planet = planet;
|
| 122 |
this.img = new Image();
|
|
|
|
|
|
|
| 123 |
this.img.onload = () => { this.polar = null; };
|
|
|
|
| 124 |
this.img.src = `textures/${planet}.jpg`;
|
| 125 |
new ResizeObserver(() => this.resize()).observe(canvas);
|
| 126 |
new IntersectionObserver(es => {
|
|
@@ -139,11 +142,19 @@ class AuroraCanvas {
|
|
| 139 |
}
|
| 140 |
// Orthographic view of the north pole, sampled from the equirectangular texture (built once per size).
|
| 141 |
buildPolar() {
|
|
|
|
| 142 |
if (!this.img.complete || !this.img.naturalWidth) return null;
|
| 143 |
const iw = this.img.naturalWidth, ih = this.img.naturalHeight;
|
| 144 |
const src = document.createElement('canvas'); src.width = iw; src.height = ih;
|
| 145 |
const sg = src.getContext('2d'); sg.drawImage(this.img, 0, 0);
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
const size = Math.round(this.R * 2), out = document.createElement('canvas'); out.width = out.height = size;
|
| 148 |
const og = out.getContext('2d'), od = og.createImageData(size, size);
|
| 149 |
for (let y = 0; y < size; y++) for (let x = 0; x < size; x++) {
|
|
|
|
| 120 |
this.stars = Array.from({ length: 70 }, () => [Math.random(), Math.random(), Math.random() * 0.8 + 0.2]);
|
| 121 |
this.planet = planet;
|
| 122 |
this.img = new Image();
|
| 123 |
+
// Request with CORS so getImageData() works when textures load from another origin (e.g. a CDN).
|
| 124 |
+
this.img.crossOrigin = 'anonymous';
|
| 125 |
this.img.onload = () => { this.polar = null; };
|
| 126 |
+
this.img.onerror = () => { this.imgFailed = true; };
|
| 127 |
this.img.src = `textures/${planet}.jpg`;
|
| 128 |
new ResizeObserver(() => this.resize()).observe(canvas);
|
| 129 |
new IntersectionObserver(es => {
|
|
|
|
| 142 |
}
|
| 143 |
// Orthographic view of the north pole, sampled from the equirectangular texture (built once per size).
|
| 144 |
buildPolar() {
|
| 145 |
+
if (this.polarFailed || this.imgFailed) return null;
|
| 146 |
if (!this.img.complete || !this.img.naturalWidth) return null;
|
| 147 |
const iw = this.img.naturalWidth, ih = this.img.naturalHeight;
|
| 148 |
const src = document.createElement('canvas'); src.width = iw; src.height = ih;
|
| 149 |
const sg = src.getContext('2d'); sg.drawImage(this.img, 0, 0);
|
| 150 |
+
let sd;
|
| 151 |
+
try {
|
| 152 |
+
sd = sg.getImageData(0, 0, iw, ih).data;
|
| 153 |
+
} catch (e) {
|
| 154 |
+
// Cross-origin texture without CORS taints the canvas; fall back to the flat disc.
|
| 155 |
+
this.polarFailed = true;
|
| 156 |
+
return null;
|
| 157 |
+
}
|
| 158 |
const size = Math.round(this.R * 2), out = document.createElement('canvas'); out.width = out.height = size;
|
| 159 |
const og = out.getContext('2d'), od = og.createImageData(size, size);
|
| 160 |
for (let y = 0; y < size; y++) for (let x = 0; x < size; x++) {
|