Fix bird not flying to right location when iOS address bar is minimized

This commit is contained in:
Idrees Hassan
2025-10-28 23:11:46 -04:00
parent 2c0438cc0a
commit 66284b0af8
6 changed files with 106 additions and 87 deletions

62
dist/birb.js vendored
View File

@@ -187,6 +187,27 @@
return layer; return layer;
} }
/**
* The height of the inner browser window
* Will be the same as getFixedWindowHeight() on most browsers
* On iOS, it will vary to be the height excluding the current address bar size (potentially greater than fixed height)
*/
function getWindowHeight() {
// Necessary because iOS 26 Safari is terrible and won't render
// fixed/sticky elements behind the address bar
return window.innerHeight;
}
/**
* The fixed height of the inner browser window
* Will be the same as getWindowHeight() on most browsers
* On iOS, it will always be the height of the window when the address bar is fully expanded
* @returns The true height of the inner browser window
*/
function getFixedWindowHeight() {
return document.documentElement.clientHeight;
}
/** Indicators for parts of the base bird sprite sheet */ /** Indicators for parts of the base bird sprite sheet */
const Sprite = { const Sprite = {
THEME_HIGHLIGHT: "theme-highlight", THEME_HIGHLIGHT: "theme-highlight",
@@ -752,7 +773,8 @@
let bottom; let bottom;
if (this.isAbsolutePositioned) { if (this.isAbsolutePositioned) {
// Position is absolute, convert from fixed // Position is absolute, convert from fixed
bottom = y - window.scrollY; // Account for address bar shrinkage on iOS
bottom = y - window.scrollY - (getWindowHeight() - getFixedWindowHeight());
} else { } else {
// Position is fixed // Position is fixed
bottom = y; bottom = y;
@@ -1652,7 +1674,7 @@
insertModal(`${birdBirb()} Mode`, message); insertModal(`${birdBirb()} Mode`, message);
}), }),
new Separator(), new Separator(),
new MenuItem("2025.10.28.95", () => { alert("Thank you for using Pocket Bird! You are on version: 2025.10.28.95"); }, false), new MenuItem("2025.10.28.152", () => { alert("Thank you for using Pocket Bird! You are on version: 2025.10.28.152"); }, false),
]; ];
const styleElement = document.createElement("style"); const styleElement = document.createElement("style");
@@ -1968,7 +1990,7 @@
targetY = getFocusedY(); targetY = getFocusedY();
// Adjust startY to account for scrolling // Adjust startY to account for scrolling
startY += targetY - oldTargetY; startY += targetY - oldTargetY;
if (targetY < 0 || targetY > window.innerHeight) { if (targetY < 0 || targetY > getWindowHeight()) {
// Fly to another element or the ground if the focused element moves out of bounds // Fly to another element or the ground if the focused element moves out of bounds
flySomewhere(); flySomewhere();
} }
@@ -2098,8 +2120,8 @@
return; return;
} }
const y = parseInt(feather.style.top || "0") + FEATHER_FALL_SPEED; const y = parseInt(feather.style.top || "0") + FEATHER_FALL_SPEED;
feather.style.top = `${Math.min(y, window.innerHeight - feather.offsetHeight)}px`; feather.style.top = `${Math.min(y, getWindowHeight() - feather.offsetHeight)}px`;
if (y < window.innerHeight - feather.offsetHeight) { if (y < getWindowHeight() - feather.offsetHeight) {
feather.style.left = `${Math.sin(3.14 * 2 * (ticks / 120)) * 25}px`; feather.style.left = `${Math.sin(3.14 * 2 * (ticks / 120)) * 25}px`;
} }
} }
@@ -2109,7 +2131,7 @@
*/ */
function centerElement(element) { function centerElement(element) {
element.style.left = `${window.innerWidth / 2 - element.offsetWidth / 2}px`; element.style.left = `${window.innerWidth / 2 - element.offsetWidth / 2}px`;
element.style.top = `${window.innerHeight / 2 - element.offsetHeight / 2}px`; element.style.top = `${getWindowHeight() / 2 - element.offsetHeight / 2}px`;
} }
/** /**
@@ -2141,7 +2163,7 @@
// Right side // Right side
x -= (menu.offsetWidth + offset) * UI_CSS_SCALE; x -= (menu.offsetWidth + offset) * UI_CSS_SCALE;
} }
if (y > window.innerHeight / 2) { if (y > getWindowHeight() / 2) {
// Top side // Top side
y -= (menu.offsetHeight + offset + 10) * UI_CSS_SCALE; y -= (menu.offsetHeight + offset + 10) * UI_CSS_SCALE;
} else { } else {
@@ -2256,7 +2278,7 @@
const dy = targetY - startY; const dy = targetY - startY;
const distance = Math.sqrt(dx * dx + dy * dy); const distance = Math.sqrt(dx * dx + dy * dy);
const time = Date.now() - stateStart; const time = Date.now() - stateStart;
if (distance > Math.max(window.innerWidth, window.innerHeight) / 2) { if (distance > Math.max(window.innerWidth, getWindowHeight()) / 2) {
speed *= 1.3; speed *= 1.3;
} }
const amount = Math.min(1, time / (distance / speed)); const amount = Math.min(1, time / (distance / speed));
@@ -2282,23 +2304,7 @@
} }
function getFocusedY() { function getFocusedY() {
return getFullWindowHeight() - focusedBounds.top; return getWindowHeight() - focusedBounds.top;
}
/**
* @returns The render-safe height of the inner browser window
*/
function getSafeWindowHeight() {
// Necessary because iOS 26 Safari is terrible and won't render
// fixed elements behind the address bar
return window.innerHeight;
}
/**
* @returns The true height of the inner browser window
*/
function getFullWindowHeight() {
return document.documentElement.clientHeight;
} }
/** /**
@@ -2317,7 +2323,7 @@
function focusOnGround() { function focusOnGround() {
focusedElement = null; focusedElement = null;
focusedBounds = { left: 0, right: window.innerWidth, top: getSafeWindowHeight() }; updateFocusedElementBounds();
flyTo(Math.random() * window.innerWidth, 0); flyTo(Math.random() * window.innerWidth, 0);
} }
@@ -2333,7 +2339,7 @@
const elements = document.querySelectorAll("img, video, .birb-sticky-note"); const elements = document.querySelectorAll("img, video, .birb-sticky-note");
const inWindow = Array.from(elements).filter((img) => { const inWindow = Array.from(elements).filter((img) => {
const rect = img.getBoundingClientRect(); const rect = img.getBoundingClientRect();
return rect.left >= 0 && rect.top >= MIN_FOCUS_ELEMENT_TOP && rect.right <= window.innerWidth && rect.top <= window.innerHeight; return rect.left >= 0 && rect.top >= MIN_FOCUS_ELEMENT_TOP && rect.right <= window.innerWidth && rect.top <= getWindowHeight();
}); });
/** @type {HTMLElement[]} */ /** @type {HTMLElement[]} */
// @ts-expect-error // @ts-expect-error
@@ -2371,7 +2377,7 @@
function updateFocusedElementBounds() { function updateFocusedElementBounds() {
if (focusedElement === null) { if (focusedElement === null) {
// Update ground location to bottom of window // Update ground location to bottom of window
focusedBounds = { left: 0, right: window.innerWidth, top: getFullWindowHeight() }; focusedBounds = { left: 0, right: window.innerWidth, top: getWindowHeight() };
return; return;
} }
let { left, right, top } = focusedElement.getBoundingClientRect(); let { left, right, top } = focusedElement.getBoundingClientRect();

64
dist/birb.user.js vendored
View File

@@ -1,7 +1,7 @@
// ==UserScript== // ==UserScript==
// @name Pocket Bird // @name Pocket Bird
// @namespace https://idreesinc.com // @namespace https://idreesinc.com
// @version 2025.10.28.95 // @version 2025.10.28.152
// @description birb // @description birb
// @author Idrees // @author Idrees
// @downloadURL https://github.com/IdreesInc/Pocket-Bird/raw/refs/heads/main/dist/birb.user.js // @downloadURL https://github.com/IdreesInc/Pocket-Bird/raw/refs/heads/main/dist/birb.user.js
@@ -201,6 +201,27 @@
return layer; return layer;
} }
/**
* The height of the inner browser window
* Will be the same as getFixedWindowHeight() on most browsers
* On iOS, it will vary to be the height excluding the current address bar size (potentially greater than fixed height)
*/
function getWindowHeight() {
// Necessary because iOS 26 Safari is terrible and won't render
// fixed/sticky elements behind the address bar
return window.innerHeight;
}
/**
* The fixed height of the inner browser window
* Will be the same as getWindowHeight() on most browsers
* On iOS, it will always be the height of the window when the address bar is fully expanded
* @returns The true height of the inner browser window
*/
function getFixedWindowHeight() {
return document.documentElement.clientHeight;
}
/** Indicators for parts of the base bird sprite sheet */ /** Indicators for parts of the base bird sprite sheet */
const Sprite = { const Sprite = {
THEME_HIGHLIGHT: "theme-highlight", THEME_HIGHLIGHT: "theme-highlight",
@@ -766,7 +787,8 @@
let bottom; let bottom;
if (this.isAbsolutePositioned) { if (this.isAbsolutePositioned) {
// Position is absolute, convert from fixed // Position is absolute, convert from fixed
bottom = y - window.scrollY; // Account for address bar shrinkage on iOS
bottom = y - window.scrollY - (getWindowHeight() - getFixedWindowHeight());
} else { } else {
// Position is fixed // Position is fixed
bottom = y; bottom = y;
@@ -1666,7 +1688,7 @@
insertModal(`${birdBirb()} Mode`, message); insertModal(`${birdBirb()} Mode`, message);
}), }),
new Separator(), new Separator(),
new MenuItem("2025.10.28.95", () => { alert("Thank you for using Pocket Bird! You are on version: 2025.10.28.95"); }, false), new MenuItem("2025.10.28.152", () => { alert("Thank you for using Pocket Bird! You are on version: 2025.10.28.152"); }, false),
]; ];
const styleElement = document.createElement("style"); const styleElement = document.createElement("style");
@@ -1982,7 +2004,7 @@
targetY = getFocusedY(); targetY = getFocusedY();
// Adjust startY to account for scrolling // Adjust startY to account for scrolling
startY += targetY - oldTargetY; startY += targetY - oldTargetY;
if (targetY < 0 || targetY > window.innerHeight) { if (targetY < 0 || targetY > getWindowHeight()) {
// Fly to another element or the ground if the focused element moves out of bounds // Fly to another element or the ground if the focused element moves out of bounds
flySomewhere(); flySomewhere();
} }
@@ -2112,8 +2134,8 @@
return; return;
} }
const y = parseInt(feather.style.top || "0") + FEATHER_FALL_SPEED; const y = parseInt(feather.style.top || "0") + FEATHER_FALL_SPEED;
feather.style.top = `${Math.min(y, window.innerHeight - feather.offsetHeight)}px`; feather.style.top = `${Math.min(y, getWindowHeight() - feather.offsetHeight)}px`;
if (y < window.innerHeight - feather.offsetHeight) { if (y < getWindowHeight() - feather.offsetHeight) {
feather.style.left = `${Math.sin(3.14 * 2 * (ticks / 120)) * 25}px`; feather.style.left = `${Math.sin(3.14 * 2 * (ticks / 120)) * 25}px`;
} }
} }
@@ -2123,7 +2145,7 @@
*/ */
function centerElement(element) { function centerElement(element) {
element.style.left = `${window.innerWidth / 2 - element.offsetWidth / 2}px`; element.style.left = `${window.innerWidth / 2 - element.offsetWidth / 2}px`;
element.style.top = `${window.innerHeight / 2 - element.offsetHeight / 2}px`; element.style.top = `${getWindowHeight() / 2 - element.offsetHeight / 2}px`;
} }
/** /**
@@ -2155,7 +2177,7 @@
// Right side // Right side
x -= (menu.offsetWidth + offset) * UI_CSS_SCALE; x -= (menu.offsetWidth + offset) * UI_CSS_SCALE;
} }
if (y > window.innerHeight / 2) { if (y > getWindowHeight() / 2) {
// Top side // Top side
y -= (menu.offsetHeight + offset + 10) * UI_CSS_SCALE; y -= (menu.offsetHeight + offset + 10) * UI_CSS_SCALE;
} else { } else {
@@ -2270,7 +2292,7 @@
const dy = targetY - startY; const dy = targetY - startY;
const distance = Math.sqrt(dx * dx + dy * dy); const distance = Math.sqrt(dx * dx + dy * dy);
const time = Date.now() - stateStart; const time = Date.now() - stateStart;
if (distance > Math.max(window.innerWidth, window.innerHeight) / 2) { if (distance > Math.max(window.innerWidth, getWindowHeight()) / 2) {
speed *= 1.3; speed *= 1.3;
} }
const amount = Math.min(1, time / (distance / speed)); const amount = Math.min(1, time / (distance / speed));
@@ -2296,23 +2318,7 @@
} }
function getFocusedY() { function getFocusedY() {
return getFullWindowHeight() - focusedBounds.top; return getWindowHeight() - focusedBounds.top;
}
/**
* @returns The render-safe height of the inner browser window
*/
function getSafeWindowHeight() {
// Necessary because iOS 26 Safari is terrible and won't render
// fixed elements behind the address bar
return window.innerHeight;
}
/**
* @returns The true height of the inner browser window
*/
function getFullWindowHeight() {
return document.documentElement.clientHeight;
} }
/** /**
@@ -2331,7 +2337,7 @@
function focusOnGround() { function focusOnGround() {
focusedElement = null; focusedElement = null;
focusedBounds = { left: 0, right: window.innerWidth, top: getSafeWindowHeight() }; updateFocusedElementBounds();
flyTo(Math.random() * window.innerWidth, 0); flyTo(Math.random() * window.innerWidth, 0);
} }
@@ -2347,7 +2353,7 @@
const elements = document.querySelectorAll("img, video, .birb-sticky-note"); const elements = document.querySelectorAll("img, video, .birb-sticky-note");
const inWindow = Array.from(elements).filter((img) => { const inWindow = Array.from(elements).filter((img) => {
const rect = img.getBoundingClientRect(); const rect = img.getBoundingClientRect();
return rect.left >= 0 && rect.top >= MIN_FOCUS_ELEMENT_TOP && rect.right <= window.innerWidth && rect.top <= window.innerHeight; return rect.left >= 0 && rect.top >= MIN_FOCUS_ELEMENT_TOP && rect.right <= window.innerWidth && rect.top <= getWindowHeight();
}); });
/** @type {HTMLElement[]} */ /** @type {HTMLElement[]} */
// @ts-expect-error // @ts-expect-error
@@ -2385,7 +2391,7 @@
function updateFocusedElementBounds() { function updateFocusedElementBounds() {
if (focusedElement === null) { if (focusedElement === null) {
// Update ground location to bottom of window // Update ground location to bottom of window
focusedBounds = { left: 0, right: window.innerWidth, top: getFullWindowHeight() }; focusedBounds = { left: 0, right: window.innerWidth, top: getWindowHeight() };
return; return;
} }
let { left, right, top } = focusedElement.getBoundingClientRect(); let { left, right, top } = focusedElement.getBoundingClientRect();

View File

@@ -2,7 +2,7 @@
"manifest_version": 3, "manifest_version": 3,
"name": "Pocket Bird", "name": "Pocket Bird",
"description": "It's a bird, in your browser. What more could you want?", "description": "It's a bird, in your browser. What more could you want?",
"version": "2025.10.28.95", "version": "2025.10.28.152",
"homepage_url": "https://idreesinc.com", "homepage_url": "https://idreesinc.com",
"icons": { "icons": {
"48": "images/icons/transparent/48x48x1.png", "48": "images/icons/transparent/48x48x1.png",

View File

@@ -15,7 +15,8 @@ import {
log, log,
debug, debug,
error, error,
getLayer getLayer,
getWindowHeight
} from './shared.js'; } from './shared.js';
import { import {
Sprite, Sprite,
@@ -541,7 +542,7 @@ Promise.all([
targetY = getFocusedY(); targetY = getFocusedY();
// Adjust startY to account for scrolling // Adjust startY to account for scrolling
startY += targetY - oldTargetY; startY += targetY - oldTargetY;
if (targetY < 0 || targetY > window.innerHeight) { if (targetY < 0 || targetY > getWindowHeight()) {
// Fly to another element or the ground if the focused element moves out of bounds // Fly to another element or the ground if the focused element moves out of bounds
flySomewhere(); flySomewhere();
} }
@@ -674,8 +675,8 @@ Promise.all([
return; return;
} }
const y = parseInt(feather.style.top || "0") + FEATHER_FALL_SPEED; const y = parseInt(feather.style.top || "0") + FEATHER_FALL_SPEED;
feather.style.top = `${Math.min(y, window.innerHeight - feather.offsetHeight)}px`; feather.style.top = `${Math.min(y, getWindowHeight() - feather.offsetHeight)}px`;
if (y < window.innerHeight - feather.offsetHeight) { if (y < getWindowHeight() - feather.offsetHeight) {
feather.style.left = `${Math.sin(3.14 * 2 * (ticks / 120)) * 25}px`; feather.style.left = `${Math.sin(3.14 * 2 * (ticks / 120)) * 25}px`;
} }
} }
@@ -685,7 +686,7 @@ Promise.all([
*/ */
function centerElement(element) { function centerElement(element) {
element.style.left = `${window.innerWidth / 2 - element.offsetWidth / 2}px`; element.style.left = `${window.innerWidth / 2 - element.offsetWidth / 2}px`;
element.style.top = `${window.innerHeight / 2 - element.offsetHeight / 2}px`; element.style.top = `${getWindowHeight() / 2 - element.offsetHeight / 2}px`;
} }
/** /**
@@ -717,7 +718,7 @@ Promise.all([
// Right side // Right side
x -= (menu.offsetWidth + offset) * UI_CSS_SCALE; x -= (menu.offsetWidth + offset) * UI_CSS_SCALE;
} }
if (y > window.innerHeight / 2) { if (y > getWindowHeight() / 2) {
// Top side // Top side
y -= (menu.offsetHeight + offset + 10) * UI_CSS_SCALE; y -= (menu.offsetHeight + offset + 10) * UI_CSS_SCALE;
} else { } else {
@@ -833,7 +834,7 @@ Promise.all([
const dy = targetY - startY; const dy = targetY - startY;
const distance = Math.sqrt(dx * dx + dy * dy); const distance = Math.sqrt(dx * dx + dy * dy);
const time = Date.now() - stateStart; const time = Date.now() - stateStart;
if (distance > Math.max(window.innerWidth, window.innerHeight) / 2) { if (distance > Math.max(window.innerWidth, getWindowHeight()) / 2) {
speed *= 1.3; speed *= 1.3;
} }
const amount = Math.min(1, time / (distance / speed)); const amount = Math.min(1, time / (distance / speed));
@@ -859,23 +860,7 @@ Promise.all([
} }
function getFocusedY() { function getFocusedY() {
return getFullWindowHeight() - focusedBounds.top; return getWindowHeight() - focusedBounds.top;
}
/**
* @returns The render-safe height of the inner browser window
*/
function getSafeWindowHeight() {
// Necessary because iOS 26 Safari is terrible and won't render
// fixed elements behind the address bar
return window.innerHeight;
}
/**
* @returns The true height of the inner browser window
*/
function getFullWindowHeight() {
return document.documentElement.clientHeight;
} }
/** /**
@@ -894,7 +879,7 @@ Promise.all([
function focusOnGround() { function focusOnGround() {
focusedElement = null; focusedElement = null;
focusedBounds = { left: 0, right: window.innerWidth, top: getSafeWindowHeight() }; updateFocusedElementBounds();
flyTo(Math.random() * window.innerWidth, 0); flyTo(Math.random() * window.innerWidth, 0);
} }
@@ -910,7 +895,7 @@ Promise.all([
const elements = document.querySelectorAll("img, video, .birb-sticky-note"); const elements = document.querySelectorAll("img, video, .birb-sticky-note");
const inWindow = Array.from(elements).filter((img) => { const inWindow = Array.from(elements).filter((img) => {
const rect = img.getBoundingClientRect(); const rect = img.getBoundingClientRect();
return rect.left >= 0 && rect.top >= MIN_FOCUS_ELEMENT_TOP && rect.right <= window.innerWidth && rect.top <= window.innerHeight; return rect.left >= 0 && rect.top >= MIN_FOCUS_ELEMENT_TOP && rect.right <= window.innerWidth && rect.top <= getWindowHeight();
}); });
/** @type {HTMLElement[]} */ /** @type {HTMLElement[]} */
// @ts-expect-error // @ts-expect-error
@@ -948,7 +933,7 @@ Promise.all([
function updateFocusedElementBounds() { function updateFocusedElementBounds() {
if (focusedElement === null) { if (focusedElement === null) {
// Update ground location to bottom of window // Update ground location to bottom of window
focusedBounds = { left: 0, right: window.innerWidth, top: getFullWindowHeight() }; focusedBounds = { left: 0, right: window.innerWidth, top: getWindowHeight() };
return; return;
} }
let { left, right, top } = focusedElement.getBoundingClientRect(); let { left, right, top } = focusedElement.getBoundingClientRect();

View File

@@ -1,4 +1,4 @@
import { Directions, getLayer } from './shared.js'; import { Directions, getLayer, getWindowHeight, getFixedWindowHeight } from './shared.js';
import Layer from './layer.js'; import Layer from './layer.js';
import Frame from './frame.js'; import Frame from './frame.js';
import Anim from './anim.js'; import Anim from './anim.js';
@@ -201,7 +201,8 @@ export class Birb {
let bottom; let bottom;
if (this.isAbsolutePositioned) { if (this.isAbsolutePositioned) {
// Position is absolute, convert from fixed // Position is absolute, convert from fixed
bottom = y - window.scrollY; // Account for address bar shrinkage on iOS
bottom = y - window.scrollY - (getWindowHeight() - getFixedWindowHeight());
} else { } else {
// Position is fixed // Position is fixed
bottom = y; bottom = y;

View File

@@ -182,4 +182,25 @@ export function getLayer(spriteSheet, spriteIndex, width) {
layer.push(spriteSheet[y].slice(spriteIndex * width, (spriteIndex + 1) * width)); layer.push(spriteSheet[y].slice(spriteIndex * width, (spriteIndex + 1) * width));
} }
return layer; return layer;
}
/**
* The height of the inner browser window
* Will be the same as getFixedWindowHeight() on most browsers
* On iOS, it will vary to be the height excluding the current address bar size (potentially greater than fixed height)
*/
export function getWindowHeight() {
// Necessary because iOS 26 Safari is terrible and won't render
// fixed/sticky elements behind the address bar
return window.innerHeight;
}
/**
* The fixed height of the inner browser window
* Will be the same as getWindowHeight() on most browsers
* On iOS, it will always be the height of the window when the address bar is fully expanded
* @returns The true height of the inner browser window
*/
export function getFixedWindowHeight() {
return document.documentElement.clientHeight;
} }