Skip to content

Commit

Permalink
Add back synced frame method and use it for triggerRepaint. (#4535)
Browse files Browse the repository at this point in the history
* Add back synced frame method and use it for triggerRepaint.

* make sure style is loaded before triggering render to avoid missing projection

* add missing type

* Update CHANGELOG.md

---------

Co-authored-by: Harel M <harel.mazor@gmail.com>
  • Loading branch information
xabbu42 and HarelM authored Jan 28, 2025
1 parent 1d68ddc commit 0b73fe0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### 🐞 Bug fixes

- _...Add new stuff here..._
- Render frame synchronized again in requestAnimationFrame callback ([#4535](https://github.com/maplibre/maplibre-gl-js/pull/4535))

## 5.0.1

Expand Down
24 changes: 15 additions & 9 deletions src/ui/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3379,15 +3379,21 @@ export class Map extends Camera {
triggerRepaint() {
if (this.style && !this._frameRequest) {
this._frameRequest = new AbortController();
browser.frameAsync(this._frameRequest).then((paintStartTimeStamp: number) => {
PerformanceUtils.frame(paintStartTimeStamp);
this._frameRequest = null;
this._render(paintStartTimeStamp);
}).catch((error: Error) => {
if (!isAbortError(error) && !isFramebufferNotCompleteError(error)) {
throw error;
}
});
browser.frame(
this._frameRequest,
(paintStartTimeStamp) => {
PerformanceUtils.frame(paintStartTimeStamp);
this._frameRequest = null;
try {
this._render(paintStartTimeStamp);
} catch(error) {
if (!isAbortError(error) && !isFramebufferNotCompleteError(error)) {
throw error;
}
}
},
() => {}
);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/ui/map_tests/map_events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -990,8 +990,9 @@ describe('map events', () => {
await sourcePromise;
});

test('getZoom on moveend is the same as after the map end moving, with terrain on', () => {
test('getZoom on moveend is the same as after the map end moving, with terrain on', async () => {
const map = createMap({interactive: true, clickTolerance: 4});
await map.once('style.load');
map.terrain = createTerrain();
let actualZoom: number;
map.on('moveend', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/ui/map_tests/map_zoom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ test('throw on maxZoom smaller than minZoom at init with falsey maxZoom', () =>
}).toThrow(new Error('maxZoom must be greater than or equal to minZoom'));
});

test('recalculate zoom is done on the camera update transform', () => {
test('recalculate zoom is done on the camera update transform', async () => {
const map = createMap({
interactive: true,
clickTolerance: 4,
transformCameraUpdate: ({zoom}) => ({zoom: zoom + 0.1})
});
await map.once('style.load');
map.terrain = createTerrain();
const canvas = map.getCanvas();
simulate.dragWithMove(canvas, {x: 100, y: 100}, {x: 100, y: 150});
Expand Down
14 changes: 9 additions & 5 deletions src/util/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ export const browser = {
*/
now,

frame(abortController: AbortController, fn: (paintStartTimestamp: number) => void, reject: (error: Error) => void): void {
const frame = requestAnimationFrame(fn);
abortController.signal.addEventListener('abort', () => {
cancelAnimationFrame(frame);
reject(createAbortError());
});
},

frameAsync(abortController: AbortController): Promise<number> {
return new Promise((resolve, reject) => {
const frame = requestAnimationFrame(resolve);
abortController.signal.addEventListener('abort', () => {
cancelAnimationFrame(frame);
reject(createAbortError());
});
this.frame(abortController, resolve, reject);
});
},

Expand Down

0 comments on commit 0b73fe0

Please sign in to comment.