Offscreen rendering lets you obtain the content of a BrowserWindow
in a
bitmap, so it can be rendered anywhere, for example, on texture in a 3D scene.
The offscreen rendering in Electron uses a similar approach to that of the
Chromium Embedded Framework
project.
Notes:
paint
event to be more efficient.GPU accelerated rendering means that the GPU is used for composition. Because of that, the frame has to be copied from the GPU which requires more resources, thus this mode is slower than the Software output device. The benefit of this mode is that WebGL and 3D CSS animations are supported.
This mode uses a software output device for rendering in the CPU, so the frame generation is much faster. As a result, this mode is preferred over the GPU accelerated one.
To enable this mode, GPU acceleration has to be disabled by calling the
app.disableHardwareAcceleration()
API.
```javascript fiddle=’docs/fiddles/features/offscreen-rendering’ const { app, BrowserWindow } = require(‘electron’) const fs = require(‘fs’)
app.disableHardwareAcceleration()
let win
app.whenReady().then(() => { win = new BrowserWindow({ webPreferences: { offscreen: true } })
win.loadURL(‘https://github.com’) win.webContents.on(‘paint’, (event, dirty, image) => { fs.writeFileSync(‘ex.png’, image.toPNG()) }) win.webContents.setFrameRate(60) }) ```
After launching the Electron application, navigate to your application’s working folder, where you’ll find the rendered image.