electron/docs/tutorial/offscreen-rendering.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

86 lines
2.6 KiB
Markdown
Raw Normal View History

2016-07-30 22:47:08 +00:00
# Offscreen Rendering
## Overview
2016-07-30 22:47:08 +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
_Notes_:
2016-12-21 22:15:39 +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 240 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
docs: add window customization guide (#31054) * docs: add window customization guide * fixes * Update docs/api/browser-window.md Co-authored-by: Mark Lee <malept@users.noreply.github.com> * Update docs/api/browser-window.md Co-authored-by: Mark Lee <malept@users.noreply.github.com> * Update docs/fiddles/windows/manage-windows/frameless-window/index.html Co-authored-by: Mark Lee <malept@users.noreply.github.com> * Update docs/tutorial/window-customization.md Co-authored-by: Mark Lee <malept@users.noreply.github.com> * Update docs/tutorial/window-customization.md Co-authored-by: Mark Lee <malept@users.noreply.github.com> * Update docs/tutorial/window-customization.md Co-authored-by: Mark Lee <malept@users.noreply.github.com> * Update docs/tutorial/window-customization.md Co-authored-by: Mark Lee <malept@users.noreply.github.com> * Update docs/tutorial/window-customization.md Co-authored-by: Mark Lee <malept@users.noreply.github.com> * Update docs/tutorial/window-customization.md Co-authored-by: Mark Lee <malept@users.noreply.github.com> * Update docs/tutorial/window-customization.md Co-authored-by: Mark Lee <malept@users.noreply.github.com> * Update docs/tutorial/window-customization.md Co-authored-by: Mark Lee <malept@users.noreply.github.com> * Update docs/tutorial/window-customization.md Co-authored-by: Mark Lee <malept@users.noreply.github.com> * Update docs/tutorial/window-customization.md Co-authored-by: Mark Lee <malept@users.noreply.github.com> * Update docs/tutorial/window-customization.md Co-authored-by: Mark Lee <malept@users.noreply.github.com> * fix code fence Co-authored-by: Mark Lee <malept@users.noreply.github.com>
2021-10-19 00:58:35 +00:00
[Frameless Window](../tutorial/window-customization.md)..
2016-07-30 22:47:08 +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
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.
2016-07-30 22:47:08 +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
generation is much faster. As a result, this mode is preferred over the GPU
accelerated one.
2016-08-03 01:15:38 +00:00
To enable this mode, GPU acceleration has to be disabled by calling the
[`app.disableHardwareAcceleration()`][disablehardwareacceleration] API.
2016-07-30 22:47:08 +00:00
## Example
```fiddle docs/fiddles/features/offscreen-rendering
const { app, BrowserWindow } = require('electron/main')
const fs = require('node:fs')
const path = require('node:path')
2016-07-30 22:47:08 +00:00
app.disableHardwareAcceleration()
2016-07-30 22:47:08 +00:00
function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
offscreen: true
}
})
2018-02-19 23:50:26 +00:00
win.loadURL('https://github.com')
win.webContents.on('paint', (event, dirty, image) => {
fs.writeFileSync('ex.png', image.toPNG())
})
win.webContents.setFrameRate(60)
console.log(`The screenshot has been successfully saved to ${path.join(process.cwd(), 'ex.png')}`)
}
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
2016-08-03 01:15:38 +00:00
})
2016-07-30 22:47:08 +00:00
```
After launching the Electron application, navigate to your application's
working folder, where you'll find the rendered image.
[disablehardwareacceleration]: ../api/app.md#appdisablehardwareacceleration