Smooth PDF Pinch Zoom and Scrolling on an iPad
EtherPK (https://app.etherpk.com) opens PDFs in a tab beside your notes, and since the 0.50.0 release that tab is much smoother on a tablet. A pinch follows your fingers and the page stays where you leave it. Zoomed in, a flick glides.
The reader is built on pdf.js, which draws the pages, and EtherPK handles the gestures. Until this week, on my iPad, the pinch moved in little steps, the page often skipped somewhere else as I let go, and once I'd zoomed in, scrolling stuck. Each fault had a different cause. The last one took fifteen rounds of testing to find, mostly because I was counting the wrong thing.
I worked on this with Claude Code. The agent wrote the recorders, the test builds and the fixes, and I did the pinching.
The clip is the finished reader in Chromium at an iPad's size, with a script sending the touches.
A pinch that moved in steps
The pinch used to ask pdf.js for a new zoom once a frame, and pdf.js rounds each zoom to a hundredth:
newScale = Math.round(newScale * scaleFactor * 100) / 100;
A hundredth sounds small, but it's several pixels of a page's width. I measured a slow, even pinch in Chromium at iPad size. The touches asked for an even 5.5 px a step, and the page grew in steps of 6 to 8. Each step also laid out every page in the document. With the CPU slowed four times, four frames took between 63 and 70 ms.
So the reader no longer asks pdf.js for anything during the gesture. While two fingers are down, the whole viewer is drawn under one CSS transform:
pinch.placed = placePinch(pinch.start, origin, zoom)
const { x, y, zoom: held } = pinch.placed
viewerElement.style.transform = `translate(${x}px, ${y}px) scale(${held})`
placePinch is pure geometry. It keeps the place under your fingers under them, within what the scroller will be able to show once the pages are laid out again: a page narrower than the view is centred, and a wider one covers the view. Without that limit the page would jump into bounds as you let go.
pdf.js lays the pages out once, when the fingers lift. The long frames went, and moving the fingers half a pixel at a time, the page's width stays within 2 px of where they put it. The price is that text is soft until you lift, and the page can settle by up to half a percent, because that one final zoom is still rounded.
The zoom is measured from where the fingers took hold, since adding up rounded steps drifts. And the page changes at most once a frame, because a screen can report several touch moves for each frame it draws.
A page that skipped on release
The next morning the pinch was smooth, but the page often skipped when I let go. I plugged an Android phone in over USB and recorded what happened. Of 43 lifts, 19 moved the page by more than 20 px. Two of those moved it by about 10,000.
pdf.js will zoom about a point if you give it one, and that was the problem. It scrolls to the zoomed corner of the view first. That scroll stops at the ends of what can scroll, and pdf.js then works out the finger position from wherever it stopped. Near the edge of a page, that's the wrong place.
The reader now puts the place back itself. It notes which point of the page is under the fingers, lets pdf.js lay the pages out, then measures where that point went and scrolls by the difference:
const drawn = boxOf(page)
const place = {
across: (x - drawn.left) / drawn.width,
down: (y - drawn.top) / drawn.height,
}
// …take the transform off…
viewer.updateScale({ drawingDelay: 0, scaleFactor: pinch.placed.zoom })
const laid = boxOf(page)
container.scrollLeft += laid.left + place.across * laid.width - x
container.scrollTop += laid.top + place.down * laid.height - y
The 10,000 px skips had a cause of their own. pdf.js notes its position as a scroll reaches it, and a move at the very end of a document has nowhere to scroll. pdf.js kept the old position and used it in the new layout at the next zoom, pages away. Calling viewer.update() after every lift brings it up to date.
There was a vertical drift too, of up to 37 px. pdf.js keeps the gap between pages at 10 px whatever the zoom, but a transform scales the gaps along with the pages, so the pages landed somewhere slightly different from where they'd been drawn. The gap now zooms with the pages, which is one line of CSS:
margin-block: 0 calc(10px * var(--scale-factor, 1));
With the first round of fixes on the phone, one lift of 38 moved the page by more than 10 px. My fingers had been in the gap between two pages, so the reader now holds the page nearest the fingers.
A ghost behind the pages
On the iPad, zooming out showed an old copy of the page behind the one I was pinching.
The first fix was for the wrong fault. Zoomed in, pdf.js draws part of the page a second time at full size (more on that below), and in Chromium at iPad size that part was 2.5 to 3 px out of line. It's now within 0.2 px, but it wasn't what I was seeing. A screenshot showed the old zoomed-in page still painted around the new one. The likely cause is that WebKit leaves behind part of what the scroller had painted when the pages move into a layer of their own. A box in the scroller's background colour now sits under the pages for the length of the gesture and covers whatever is left.
I reloaded to try that fix and the ghost was still there. So was the old gap between pages, which made me doubt the build. The iPad had been off the network for about 40 minutes, and EtherPK works offline, so it had loaded an older build from its own storage. From then on I tested on an address the iPad had never opened, with no service worker, and the server's log had to show the iPad's request before a result counted.
A flick that stopped dead
Scrolling with one finger still stuck, on the iPad only. Android was fine.
I had no debugger on the iPad that I could reach from Linux, so the page measured itself. A small proxy in front of the test build added a script that posted positions and timings back to my desktop, and none of the document's text. It also added a row of buttons, each of which reloaded the page with one thing changed. I flicked, and the agent read the numbers.
A lot of suspects were cleared that way: scroll anchoring, touch listeners that make the browser wait, the text layer, text selection, the app's own response to a press, the request to keep the screen on, and the layout around the pages. A bare page with big canvases, the same touch rules and a text layer never stalled in 33 scrolls. A build meant to hold back all page drawing still stalled and felt no better, though its hold turned out to leak.
Through all of this I was counting drags that stalled under my finger. Those were between a quarter and a third of scrolls, and the rate wandered from one run to the next, so every small sample suggested a cause that the next one took away.
I should have started by asking whether the fault was new. The release from the day before was just as sticky, so nothing changed since then had caused it. Comparing the versions also meant looking at what happened after the finger lifted. In that release, 24 of 25 glides stopped dead, typically 0.12 seconds after I let go. That was the stickiness. The pages that had looked clean were clean because their glides ran on, so my next touch caught a moving page.
Sorting every flick by zoom gave the answer:
| Zoom | Canvases | Glides that stopped dead | Typical glide | Canvases of 4 megapixels or more, per flick |
|---|---|---|---|---|
| Fit width or smaller | Software | 23 of 47 | 1.0 s | 0 |
| Zoomed in | Software | 306 of 326 | 0.1 s | 5.4 |
| Zoomed in | Hardware | 4 of 11 | 1.5 s | 14 |
On a phone or tablet, pdf.js limits a page's canvas to about five million pixels. Past that limit it draws the whole page at a lower resolution, then draws the part in view again at full size on top. When the view moves a little off that sharp part, pdf.js draws another. During a scroll on the iPad that meant a five-megapixel canvas every 50 ms.
I can't tell you what WebKit does with them. Swapping canvases on a frozen copy of the pages, 771 times in the middle of glides, didn't stop one, so the swap isn't enough by itself. But in one count 187 of 252 dead stops came within 40 ms of a new canvas, and only zoomed-in pages had this flood of large ones.
Why not hardware canvases
pdf.js has an option, enableHWA, for hardware-accelerated canvases. With that one line the glide survived. But pdf.js then drew even more of the large canvases, 14 for each flick, and a pinch after a flick would sometimes do nothing for a moment. I left it off and went after the drawing.
Draw the sharp part at rest
The sharp part now waits. While a finger is on the PDF or the page is moving, the last one stays as it is. Once both have stopped for 120 ms, pdf.js draws one new sharp part. The hold wraps a single method on each page:
const update = page.updateVisibleArea.bind(page)
page.updateVisibleArea = (visibleArea) => {
if (visibleArea && !through && rest.held) return
update(visibleArea)
}
A glide on an iPad ends in a crawl of several seconds that you can't see. Waiting for it to finish would leave the text soft long after the page looked still, so anything slower than 0.1 px per millisecond doesn't put the rest off. The end of a pinch skips the wait, because what's on screen then belongs to the old zoom.
In Chromium, posing as an iPad, six quick drags drew the sharp part 41 times before the change. Now they draw it zero times, and then once when the page stops.
On the iPad I tried this with and without hardware canvases and couldn't feel a difference, so the version without them shipped. I chose it by feel: I have no count of glides on the iPad for that build.
Keeping it that way
Sixteen new browser tests cover the end of a pinch, the gap, the ghost and the sharp part, and each one fails if its fix is taken out. They run in Chromium with synthetic touches, so they hold the mechanism in place, and WebKit still needs a person with an iPad.
Two property tests look for the cases I didn't think of. One makes up views, page counts and gaps and checks where a pinch puts the pages. The other makes up runs of scrolls, touches and waits, and checks that the sharp part is never drawn under a finger or within 120 ms of a fast move, and is always drawn once everything stops. Each ran 20,000 times without finding a fault, and the second fails for each of five ways of breaking the hold.
What's still wrong
- Some pinches on the iPad are missed. Nothing happens, and a moment later a pinch works. Every pinch that reached the page was handled, so the missed ones never arrived, and I don't know why.
- Ctrl with the mouse wheel, the zoom buttons and a double tap still zoom pdf.js's way, so zooming out with them near an edge can shift the page.
- While a zoomed-in page moves, the text outside the last sharp part is soft. That's the cost of the glide.
Try it
Open the demo graph on a tablet and tap the PDF link near the top of today's journal. Zoom well in, then flick. PDF Reading, Annotation and Signing Inside Your Markdown Notes covers the rest of the reader, and the PDF docs cover every control.