2016-07-30 22:47:08 +00:00
|
|
|
# Offscreen Rendering
|
|
|
|
|
2020-12-01 01:47:09 +00:00
|
|
|
## Overview
|
2016-07-30 22:47:08 +00:00
|
|
|
|
2020-12-01 01:47:09 +00:00
|
|
|
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](https://bitbucket.org/chromiumembedded/cef)
|
|
|
|
project.
|
2016-07-30 22:47:08 +00:00
|
|
|
|
2020-12-01 01:47:09 +00:00
|
|
|
*Notes*:
|
2016-12-21 22:15:39 +00:00
|
|
|
|
2020-12-01 01:47:09 +00:00
|
|
|
* There are two rendering modes that can be used (see the section below) and only
|
|
|
|
the dirty area is passed to the `paint` event to be more efficient.
|
|
|
|
* You can stop/continue the rendering as well as set the frame rate.
|
|
|
|
* The maximum frame rate is 60 because greater values bring only performance
|
|
|
|
losses with no benefits.
|
|
|
|
* When nothing is happening on a webpage, no frames are generated.
|
|
|
|
* An offscreen window is always created as a
|
|
|
|
[Frameless Window](../api/frameless-window.md).
|
2016-07-30 22:47:08 +00:00
|
|
|
|
2020-12-01 01:47:09 +00:00
|
|
|
### Rendering Modes
|
|
|
|
|
|
|
|
#### GPU accelerated
|
2016-07-30 22:47:08 +00:00
|
|
|
|
|
|
|
GPU accelerated rendering means that the GPU is used for composition. Because of
|
2020-12-01 01:47:09 +00:00
|
|
|
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
|
2020-01-27 06:48:56 +00:00
|
|
|
mode is that WebGL and 3D CSS animations are supported.
|
2016-07-30 22:47:08 +00:00
|
|
|
|
2020-12-01 01:47:09 +00:00
|
|
|
#### Software output device
|
2016-07-30 22:47:08 +00:00
|
|
|
|
|
|
|
This mode uses a software output device for rendering in the CPU, so the frame
|
2020-12-01 01:47:09 +00:00
|
|
|
generation is much faster. As a result, this mode is preferred over the GPU
|
|
|
|
accelerated one.
|
2016-08-03 01:15:38 +00:00
|
|
|
|
2020-12-01 01:47:09 +00:00
|
|
|
To enable this mode, GPU acceleration has to be disabled by calling the
|
2016-08-03 01:32:29 +00:00
|
|
|
[`app.disableHardwareAcceleration()`][disablehardwareacceleration] API.
|
2016-07-30 22:47:08 +00:00
|
|
|
|
2020-12-01 01:47:09 +00:00
|
|
|
## Example
|
|
|
|
|
|
|
|
Starting with a working application from the
|
|
|
|
[Quick Start Guide](quick-start.md), add the following lines to the
|
|
|
|
`main.js` file:
|
2016-07-30 22:47:08 +00:00
|
|
|
|
2020-11-30 07:48:39 +00:00
|
|
|
```javascript fiddle='docs/fiddles/features/offscreen-rendering'
|
2018-02-19 23:50:26 +00:00
|
|
|
const { app, BrowserWindow } = require('electron')
|
2020-12-01 01:47:09 +00:00
|
|
|
const fs = require('fs')
|
2016-07-30 22:47:08 +00:00
|
|
|
|
2016-08-03 01:32:29 +00:00
|
|
|
app.disableHardwareAcceleration()
|
2016-07-30 22:47:08 +00:00
|
|
|
|
2016-08-04 04:47:52 +00:00
|
|
|
let win
|
2018-02-19 23:50:26 +00:00
|
|
|
|
2020-02-03 22:43:22 +00:00
|
|
|
app.whenReady().then(() => {
|
2020-12-01 01:47:09 +00:00
|
|
|
win = new BrowserWindow({ webPreferences: { offscreen: true } })
|
2018-02-19 23:50:26 +00:00
|
|
|
|
2020-12-01 01:47:09 +00:00
|
|
|
win.loadURL('https://github.com')
|
2016-08-04 04:47:52 +00:00
|
|
|
win.webContents.on('paint', (event, dirty, image) => {
|
2020-12-01 01:47:09 +00:00
|
|
|
fs.writeFileSync('ex.png', image.toPNG())
|
2016-08-04 04:47:52 +00:00
|
|
|
})
|
2020-12-01 01:47:09 +00:00
|
|
|
win.webContents.setFrameRate(60)
|
2016-08-03 01:15:38 +00:00
|
|
|
})
|
2016-07-30 22:47:08 +00:00
|
|
|
```
|
2016-08-03 01:32:29 +00:00
|
|
|
|
2020-12-01 01:47:09 +00:00
|
|
|
After launching the Electron application, navigate to your application's
|
|
|
|
working folder.
|
2016-08-03 01:32:29 +00:00
|
|
|
[disablehardwareacceleration]: ../api/app.md#appdisablehardwareacceleration
|