Skip to content
CefSwift
Esc
↑↓navigate↵open⌘Jpreview
On this page

Animation

Pages under SwiftUI springs, transforms, matched geometry and transitions, in both renderings.

struct Card: View {
    let page: CefPage
    @State private var isExpanded = false

    var body: some View {
        CefWebView(page)
            .frame(width: isExpanded ? 900 : 320, height: isExpanded ? 600 : 220)
            .clipShape(.rect(cornerRadius: isExpanded ? 0 : 16))
            .onTapGesture { withAnimation(.spring) { isExpanded.toggle() } }
    }
}

Gallery: AnimationDemo.

Animating a page

A page animates with SwiftUI’s springs and transitions and AppKit’s animator like any view: SwiftUI sets the view’s frame and its containers’ transforms, opacity, masks and filters on every display refresh, and the page follows. What differs between the renderings is what the page can do while that happens (how it works).

Animation Offscreen Windowed
Size (frame, a split view’s divider, a window’s live resize) Lays out once per refresh by default; the last frame, stretched, covers the view until the next arrives Lays out live, as WKWebView does; Chromium’s frame arrives a few refreshes behind a spring, and until it does the page’s canvas shows where it has not reached: white, or the dark canvas of a page that opts into a dark color scheme (give an animated page color-scheme: light and a background of its own to keep that area light)
scaleEffect, rotationEffect, offset Exact; clicks land where the page shows them Applied to Chromium’s layers by the window server; clicks land (macOS 27, which converts them through the transform)
rotation3DEffect Exact; clicks that reach the view land on the page point under them Applied to Chromium’s layers; clicks land at the untransformed point
opacity, clipShape, corner radius, shadow, blur Exact, on the app’s own layer Applied to Chromium’s layers by the window server
Moving between containers (matchedGeometryEffect), .transition Arrives showing its last frame; the view it leaves keeps that frame while it animates out Chromium’s view moves to the new view and shows there at once; the view it leaves shows the canvas color while it animates out
SwiftUI content over the page Drawn above it, and takes its clicks Takes its clicks; placed above the page’s layers, which Chromium’s GPU process draws

Which rendering for which interface:

Interface Rendering
A page that fills a window or a pane, an app whose interface is web content Windowed: no copy, Chromium’s own input and accessibility
A page under transforms, 3D, blur, masks, or SwiftUI effects you need exact Offscreen
Thumbnails, an overview grid, live previews, a zoomable canvas of pages Offscreen, with offscreenPageSize
A card that expands to fill the window, a page that moves between layouts Offscreen, so it never shows blank and can lay out once at the final size
A page whose transparent parts show the app behind it Offscreen with drawsBackground = false

While an offscreen page’s size animates, offscreenResizePolicy says when it lays out. .live, the default, lays out at every size, once per run-loop pass. .scaleThenSettle keeps the layout and stretches the last frame while the size keeps changing, and lays out once it has been still for a few refreshes (and at most four times a second until then): the choice for heavy pages, many pages animating at once, or a size that springs back and forth.

struct ExpandingPage: View {
    @State private var page: CefPage = {
        var configuration = CefPage.Configuration()
        configuration.rendering = .offscreen
        return CefPage(url: URL(string: "https://swift.org")!, configuration: configuration)
    }()
    @State private var expanded = false

    var body: some View {
        CefWebView(page)
            .offscreenResizePolicy(.scaleThenSettle)
            .frame(width: expanded ? 720 : 360, height: expanded ? 520 : 240)
            .clipShape(.rect(cornerRadius: 16, style: .continuous))
            .onTapGesture { withAnimation(.spring(duration: 0.5)) { expanded.toggle() } }
    }
}

offscreenPageSize lays the page out at a size of its own, whatever its view’s size, and offscreenFramePlacement places the frame in the view: .stretch (the default), .aspectFit, .aspectFill or .topLeading. Clicks, scrolling, the IME candidate window, <select> popups, context menus and accessibility frames all follow the placed frame. A grid of live previews lays every page out at a desktop size and fits it to its card:

CefWebView(page)
    .offscreenPageSize(CGSize(width: 1280, height: 800))
    .offscreenFramePlacement(.aspectFit)
    .aspectRatio(1.6, contentMode: .fit)
    .clipShape(.rect(cornerRadius: 12, style: .continuous))

For a card that expands to the full window, set the page size to the final size and the placement to .topLeading before the animation: the page lays out once, and the growing card reveals it. Clear both when it ends. The Gallery’s Animation demo shows each of these, with the policy to toggle and a graph of the time between refreshes.

In AppKit the same properties are on CefOffscreenPageView, and the view’s layer takes a corner radius and a continuous corner curve like any layer-backed view’s:

import AppKit
import CefKit

@MainActor
func makePreview(of page: CefPage) -> NSView {
    let view = CefOffscreenPageView(page: page)
    view.pageSize = CGSize(width: 1280, height: 800)
    view.framePlacement = .aspectFit
    view.resizePolicy = .scaleThenSettle
    view.layer?.cornerRadius = 12
    view.layer?.cornerCurve = .continuous
    return view
}

A page has one host. When an offscreen page moves to another CefOffscreenPageView, the new view shows the frame the old one showed until the page paints there; with keepsLastFrameWhenMoved (which CefWebView turns on) the old view keeps that frame too, inert, until it is given another browser, so a cross-fade between the two never shows a blank view. A windowed page’s frames cannot be kept: its view moves.

A windowed page shows the page’s backgroundColor wherever Chromium’s frame does not yet cover its view, as when the view grows faster than Chromium lays out. Set it to the colour of your page’s background, as a dark web interface would, and that edge does not show.

Animating pages

Four parties take part when a page animates, each on its own clock.

SwiftUI and AppKit, on the main thread. SwiftUI interpolates its animations itself, once per display refresh, and applies each step to the platform view as AppKit state; it hands nothing to Core Animation to interpolate. Measured with a withAnimation(.spring) on a page’s frame: every refresh sets the NSViewRepresentable’s frame, about three times per refresh at a fractional size, and the presentation layer always equals the model layer. updateNSView is not called during the animation, so a representable cannot read the animation from its transaction. The other modifiers act on SwiftUI’s own container views around the representable, again per refresh: scaleEffect and rotationEffect set an affine transform on the layer of the platform-view host, rotation3DEffect a 3D transform on an _NSProjectionView’s layer, opacity the host layer’s opacity, clipShape(.rect(cornerRadius:)) a corner radius with masksToBounds on a container’s layer, blur a gaussianBlur filter, and offset the origin of a container’s frame. A matched-geometry move between two containers and an inserted page each make a new view. AppKit’s animator() and NSAnimationContext.animate(_:) also set the frame on every refresh. And AppKit calls viewDidChangeBackingProperties on the view on every refresh of a scaleEffect or rotationEffect, a matched-geometry move and a scale transition, though neither the scale nor the screen changes.

Core Animation’s render server, out of process, composites the window’s layer tree, including Chromium’s, and would interpolate implicit layer animations; neither SwiftUI nor AppKit uses them for these.

Chromium. A windowed page is Chromium’s own view: its frames come from the GPU process as a CALayerHost (a layer whose content another process draws), inside a layer Chromium sizes to the frame it shows and a masking layer at the view’s size. Chromium’s compositor produces frames on its own begin-frame source, and a resize reaches it through AppKit: its view’s frame changes, it lays the page out at the new size, rasters and presents, and the new frame shows a few milliseconds to a refresh or two later. An offscreen page is told of a size by was_resized, asks get_view_rect, lays out and rasters, and its frames arrive through on_accelerated_paint on the main thread, timed by Chromium’s own timer (see Frame timing), where CefOffscreenPageView copies each into a surface of its own (see Frame ownership) and makes it its content layer’s contents with implicit actions off.

Why a size animation is expensive and late. At each size the page lays out, rasters and composites, so a spring that changes the size on every refresh asks Chromium for a new layout on every refresh, and the frame of each size arrives after the view already has the next one. The animation benchmark below measures it: during a 0.6 s spring an offscreen page presents about 20 frames in 1.2 s instead of 72 refreshes, and 30 of those refreshes show a frame of an earlier size. The capture pipeline of an offscreen page makes the lag visible: while a resize is in flight viz’s FrameSinkVideoCapturer captures the page, still at its old size, into a surface of the new size and letterboxes it, filling the rest with black; 10 of the 24 frames presented during a spring were letterboxed. Each frame carries the part its content fills (content_rect in cef_accelerated_paint_info_common_t), and CefOffscreenPageView shows only that part, placed as framePlacement says (stretched by default), so a resize shows the last frame stretched rather than a black band. The frame also carries the size of what was captured (source_size): the page’s size in the frame, which is not the surface’s when Chromium scales the capture, as it does for a page laid out larger than its view with pageSize (a 1200×800 page shown in a 300×400 view arrived in 600×800-pixel surfaces). Input maps through that size.

What each rendering can do:

Offscreen (CefOffscreenPageView) Windowed (CefWindowedPageView)
Who draws The app’s layer, with Chromium’s frame copied into it Chromium’s layer tree, from the GPU process
Transforms, opacity, masks, corner radius, blur, shadows Applied to the app’s layer: exact, interpolated by SwiftUI or Core Animation like any layer Applied by the render server to Chromium’s layers; nothing in the process can read the result
Input under a transform Converted through the layers, 2D and 3D, so a click lands on the page point it shows Chromium converts clicks through AppKit’s view geometry, which applies a 2D transform on this macOS and no 3D one
A frame of another size than the view Placed by framePlacement, with input mapped back Anchored at the top left by Chromium; while the view grows, Chromium’s view is sized ahead of it (below)
Laying out at another size (pageSize) Yes: thumbnails and previews with live input No: Chromium sizes the page to its view
When the size animates resizePolicy: live, once per run-loop pass, or scale, then settle Live: Chromium resizes with its view
Moving to another view Shows the last frame at once; the old view can keep it Chromium’s view moves; it has no frame to keep
Cost A GPU copy per frame on the main thread (1.6–3.4 ms) None in the app

SwiftUI content over a page is not the “airspace” problem of a native child window on other platforms: a page is a layer subtree of the window like any view’s, and AppKit hit-tests SwiftUI’s overlay first. The animation checks click an overlay over each kind of page, and the click goes to SwiftUI’s hosting view, not to the page. What a windowed page cannot do is be read back: its pixels are the GPU process’s, so effects that need them in the app’s process (sampling, snapshots of what is on screen, compositing it into an image) need offscreen rendering, and nothing in the process can check what the window server drew over it.

Four things were wrong and are fixed (more in Presenting pages, below):

  • Per-refresh screen changes under a transform. AppKit’s viewDidChangeBackingProperties on every refresh of a scaleEffect or rotationEffect made CefOffscreenPageView send notify_screen_info_changed and was_resized 30 to 45 times in 1.2 s, and the main thread hitched for up to 50 ms. The view now tells Chromium only when the backing scale or the screen changes.
  • Black bands while resizing: letterboxed frames, above.
  • A windowed page closed when its view left the hierarchy. CEF’s CefPageHostView closes its page when it is deallocated, and nothing else held Chromium’s view once stopHosting(in:) took it out of its host, as happens when SwiftUI removes a CefWebView, with or without a transition. CefPage now keeps the view while no host shows it, and lets it go when the page closes.
  • A closed windowed page closed only at the next event. Chromium finishes closing a page (on_before_close) when its view is deallocated, and hiding a view, which the lifecycle coordinator did to a closing page, autoreleases it into the pool AppKit’s event loop drains when the next event arrives. With no event, as in an unattended run, the page stayed live until one came (15 s in SelfTest). A closing page’s view is no longer touched, and CefKit posts an empty event once it is out, so the pool drains at once.

CefOffscreenPageView also tells Chromium of a size once per pass of the main run loop rather than on each of SwiftUI’s frame changes, halving was_resized during a spring, and a page moving to another offscreen view arrives with the frame it last showed, so it is never blank there.

Presenting pages

What Chromium renders and what the window shows are different things, and the checks once looked only at the first: loads, Chromium’s own screenshots (Page.captureScreenshot, rendered on demand) and the app’s own layers all looked right while windowed views, CefWindow and CefChromeWindow showed no page at all. SelfTest’s visibility group now takes a picture of its own window as the window server composites it (the examples’ OwnWindowCapture: ScreenCaptureKit, filtered to the app’s own window, which needs Screen Recording permission; without it the on-screen checks skip, as .environment) and checks a two-colour page fills its view edge to edge on every hosting path. Three defects showed, and one that was the picture’s:

  • Frames handed over through the remote Core Animation API. With Chromium’s RemoteCoreAnimationAPI feature on, its default, the GPU process draws each page into a Core Animation context of its own, which the browser process shows with a CALayerHost, as Chrome does. A picture a process takes of its own window (CGWindowListCreateImage) leaves out what another process draws into it, so those pictures had no page on any windowed path, and CefKit turned the feature off on the strength of them. The screen had the page all along: screencapture -l of the same windows, and of Chrome for Testing’s, shows it (rechecked on 2026-11-29 with CEF 158 on macOS 27.2.1; the visibility group passes 18 of 18 with the feature on and with it off). CefKit leaves the feature at Chromium’s default; disabledFeatures can still turn it off. Every picture the examples take of their own windows (SelfTest’s visibility and resizeEdges, SELFTEST_FILM, the Browser’s -screenshot and -railScreenshots, the Gallery’s -animationShots, Compat’s -capture) now comes from OwnWindowCapture, which looks the window up among ScreenCaptureKit’s, checks this process owns it, and captures that window alone (SCContentFilter(desktopIndependentWindow:); stills with SCScreenshotManager, frames at the display’s rate with SCStream), never the display or another app’s window.

  • Chromium’s frame layer drifting from its view. Chromium keeps the layer that holds its frames (inside RenderWidgetHostViewCocoa’s layer) sized with Core Animation’s autoresizing, which rounds each change of size. SwiftUI gives a view a fractional size on every refresh of an animation, and the rounding added up: after a 0.6 s spring the layer was 7 points larger than the view, after a few springs 30, and the page showed shifted up by that much, cut off at the top, its canvas along the bottom. CefWindowedPageView now sizes Chromium’s view in whole points (rounded up, the fraction clipped), which Core Animation resizes exactly.

  • CefChromeWindow covering its own page. With showsChromeUI off, the opaque view behind the app’s top bar and sidebar was sized to the union of the two areas; with no sidebar (an empty rectangle at the leading edge) or with one, that union is the whole window, so the window’s background covered the page. Each area now has a view of its own.

  • White bars along a growing windowed page. Chromium sizes the frame it presents to its view at once, and where the renderer has not drawn the new size yet it fills the frame with the page’s background color (white, or #121212 for a page that opts into a dark color scheme). A frame at a new size comes back 40 to 60 ms after the view has the size, so while a spring grew a CefWindowedPageView, a quarter of the pictures of the window showed white bars along the page’s trailing and bottom edges, up to 90 points wide (SelfTest resizeEdges, a picture of the window about every 16 ms; 2026-11-29, M2 MacBook Air, macOS 27.2.1, a 60 Hz display, CEF 158.0.6). The energy policy and the message pump made no difference (the same counts with EnergyPolicy.disabled); the layout that shows windowed pages at all on macOS 27.2, RemoteCoreAnimationAPI off (above), is the same in every build since. CefWindowedPageView now sizes Chromium’s view to where the view will be 150 ms later at the rate and acceleration it grows at (clipped to the view, at least 4 points while it grows), and to the view exactly once its size has held for 100 ms, so the page settles at the view’s size. The first refreshes of a growth come before any larger frame can: for 120 ms CefWindowedGrowthCover shows the frame Chromium had at rest stretched over the view, as an offscreen page shows its last frame. In the 550 pictures of a run’s twelve springs, anchored at either corner, no white run is wider than one point (two pixels, at the slow end of a bounce), against 140 pictures with bars before. Offscreen pages showed none before or after.

    With the remote Core Animation API on (above), measured again with resizeEdges filming the window through ScreenCaptureKit at the display’s rate (2026-11-29, same Mac, CEF 158.0.6; about 270 frames per corner per run): without the lead, a windowed page showed bars on 66 to 71 frames, up to 177 px, as many as with the feature off (64 to 69, up to 204 px), so Chromium’s frame still trails a growing view and the lead stays. With the lead, 2 to 4 frames per corner still showed a bar (16 to 53 px; three runs), against none with the feature off: the growth cover takes the frame from the surface in Chromium’s layer, which a page presented through the remote API does not have (its layer is a CALayerHost of the GPU process’s context). In its place CefWindowedPageView now stretches Chromium’s own layers for the cover’s 120 ms: until frames at the new size arrive, Chromium’s frame keeps the at-rest size’s content in its top-left corner and the page’s background color beyond it, and a sublayerTransform scales that corner over the view, which is what the cover shows. In six runs since, five showed no bar at either corner; one showed one frame with a bar of 3 px (1.5 points, half a point over the allowance) at the bottom-right anchor. ux.py’s resizing and scrolling frame intervals stayed as they were (99th percentile 18.6 and 18.7 ms, no frame dropped in two runs).

Measurements

-benchmark animation in SelfTest runs each animation there and back on a windowed page, an offscreen page under each resize policy, an offscreen page that resizes on every frame change (the earlier design), and WKWebView, on an M3 MacBook Air with a 60 Hz display, two runs each, 1.2 s per run (72 refreshes). The page is a gradient with text. Hitches are refreshes the main thread’s display link missed. A trailing refresh shows a frame of an earlier size. Before is the design before this change; after is now:

Animation Host Hitches (longest) was_resized Page resize events Frames presented Letterboxed Trailing refreshes Main thread busy
Spring on the width Windowed 0 (16.7 ms) – 29–32 – – – 17–26%
Offscreen, before (every frame change) 0 (16.7 ms) 73–127 8–10 15–20 6–8, shown as black bands 25–30 16–19%
Offscreen, .live 0 (16.7 ms) 38–41 9–12 18–24 9–11, cropped 29–32 16–22%
Offscreen, .scaleThenSettle 0 (16.7 ms) 4 3 6 3, cropped 30–33 8–9%
WKWebView 0 (16.7 ms) – 28–30 – – – 6–7%
animator() on the container’s frame Windowed 0 (16.7 ms) – 31–32 – – – 24–29%
Offscreen, .live 0–1 (33.9 ms) 32–33 9–11 17–21 7–9, cropped 30–34 14–21%
Offscreen, .scaleThenSettle 0 (16.7 ms) 4 4 8 3, cropped 30–33 10–12%
WKWebView 0 (16.7 ms) – 26–32 – – – 5–7%
Animated window resize (setFrame(_:display:animate:)) Windowed, offscreen, WKWebView 0–1 (24–29 ms) 1–3 1–3 3–6 0–2 1–5 18–22%
scaleEffect Offscreen, before 0–2 (50.3 ms) 41–45 0 – – – 14–35%
Offscreen, after 0 (16.7 ms) 1 0 – – – 5–7%
Windowed; WKWebView 0 (16.7 ms) – 0–1 – – – 1–8%
rotationEffect Offscreen, before 0–1 (47.7 ms) 28–32 0 – – – 24–30%
Offscreen, after 0 (16.7 ms) 1 0 – – – 4%
Matched-geometry move Offscreen, before 0–1 (46.3 ms) 66–74 0 – – – 21–30%
Offscreen, after 0 (16.7 ms) 3 0 – – – 3–9%
Inserted with .transition(.scale) Offscreen, before 0–1 (51.7 ms) 50–67 0 – – – 22–30%
Offscreen, after 0 (16.7 ms) 1–3 0 – – – 2–4%
rotation3DEffect, opacity, clipShape, blur, offset Every host 0 (16.7 ms) 0–1 0 – – – 1–8%

An animated window resize changes the page’s frame about 15 times, but every host’s page lays out only once or twice, whatever it is told, as if Chromium’s work waited for the end of AppKit’s resize animation, which runs the main run loop itself (inferred from the counts, not traced). A windowed page’s frames come from another process: the benchmark compares the layer Chromium shows them in with the view, which Chromium sizes to the view at once, so it cannot see a frame that trails. Pictures of the window can: see the white bars under Presenting pages.

WKWebView does what a windowed Chromium page does: it lays the page out live, firing resize 29 to 31 times per spring, with no hitches. Neither rendering can do better than live layout and stay sharp; an offscreen page under .scaleThenSettle lays out 4 times instead of 40 and halves the main thread’s work, at the price of a stretched frame until it settles.

Display-link-driven begin frames were measured for animation before (see Frame timing): they match the timer’s rate and add latency, so offscreen pages keep Chromium’s timer during animations too. The display had no 120 Hz mode to measure ProMotion with.

Was this page helpful?