Skip to content

Instantly share code, notes, and snippets.

View selimbat's full-sized avatar
🦇

selimbat

🦇
View GitHub Profile
@selimbat
selimbat / Array.prototype.at.polyfill.js
Created June 26, 2022 12:02
This is a polyfill for Array.prototype.at, that landed in the spec in 2021 and that are not yet implemented by all browsers
if (!Array.prototype.at) {
Array.prototype.at = function (index) {
if (!this.length) return undefined;
let number = Number(index) || 0; // if NaN, affect 0
let intIndex = Math.sign(number) * Math.floor(Math.abs(number));
let relativeIndex = intIndex >= 0 ? intIndex : this.length + intIndex;
if (relativeIndex < 0 || relativeIndex >= this.length) return undefined;
return this[relativeIndex];
}
}
@selimbat
selimbat / persistentCamera.js
Last active October 14, 2022 10:11
This is a quick setup to get a camera in Three.js to persist its position when the website hot-reloads
// When creating the camera
const storedCamera = JSON.parse(localStorage.getItem('camera'));
if (storedCamera) {
camera.position.set(
storedCamera.position.x,
storedCamera.position.y,
storedCamera.position.z,
);
controls.target.set(
storedCamera.target.x,