Skip to content

Commit

Permalink
Fix crash with Safari 14 or below
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdubbelboer committed Dec 17, 2023
1 parent 2082c7d commit 1a496a5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { default as EventEmitter } from 'eventemitter3';

import { init as initAsteroid } from './asteroid';
import { Sounds } from './sounds';
import { isSafari14OrLower } from './util';

export class Loader extends EventEmitter {
// extension is the best file format extension for the current browser.
Expand All @@ -17,6 +18,12 @@ export class Loader extends EventEmitter {
// Set the default batch size to 10k so things like ropes are properly batched.
PIXI.Mesh.BATCHABLE_SIZE = 10000;

// iOS 14 and lower have a bug in their WebGL implementation that causes
// PixiJS to crash when using the new WebGL2 renderer.
if (isSafari14OrLower()) {
PIXI.settings.PREFER_ENV = PIXI.ENV.WEBGL_LEGACY;
}

this.loadingBar = document.getElementById('loading-bar');
this.loadingText = document.getElementById('loading-text');

Expand Down
13 changes: 13 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,16 @@ export function randomPointInPolygon(polygon) {

return point;
}

export function isSafari14OrLower() {
const userAgent = navigator.userAgent;
const isSafari = userAgent.includes('Safari') && !userAgent.includes('Chrome') && !userAgent.includes('Chromium');
const versionMatch = userAgent.match(/Version\/(\d+\.\d+)/);

if (isSafari && versionMatch) {
const version = parseFloat(versionMatch[1]);
return version <= 14;
}

return false;
}

0 comments on commit 1a496a5

Please sign in to comment.