docs: add example for taking screenshot (#20531)
This commit is contained in:
parent
c73e34fb9e
commit
db91673f23
3 changed files with 93 additions and 0 deletions
43
docs/fiddles/media/screenshot/take-screenshot/renderer.js
Normal file
43
docs/fiddles/media/screenshot/take-screenshot/renderer.js
Normal file
|
@ -0,0 +1,43 @@
|
|||
const { desktopCapturer } = require('electron')
|
||||
const { screen, shell } = require('electron').remote
|
||||
|
||||
const fs = require('fs')
|
||||
const os = require('os')
|
||||
const path = require('path')
|
||||
|
||||
const screenshot = document.getElementById('screen-shot')
|
||||
const screenshotMsg = document.getElementById('screenshot-path')
|
||||
|
||||
screenshot.addEventListener('click', (event) => {
|
||||
screenshotMsg.textContent = 'Gathering screens...'
|
||||
const thumbSize = determineScreenShotSize()
|
||||
const options = { types: ['screen'], thumbnailSize: thumbSize }
|
||||
|
||||
desktopCapturer.getSources(options, (error, sources) => {
|
||||
if (error) return console.log(error)
|
||||
|
||||
sources.forEach((source) => {
|
||||
const sourceName = source.name.toLowerCase()
|
||||
if (sourceName === 'entire screen' || sourceName === 'screen 1') {
|
||||
const screenshotPath = path.join(os.tmpdir(), 'screenshot.png')
|
||||
|
||||
fs.writeFile(screenshotPath, source.thumbnail.toPNG(), (error) => {
|
||||
if (error) return console.log(error)
|
||||
shell.openExternal(`file://${screenshotPath}`)
|
||||
|
||||
const message = `Saved screenshot to: ${screenshotPath}`
|
||||
screenshotMsg.textContent = message
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
function determineScreenShotSize () {
|
||||
const screenSize = screen.getPrimaryDisplay().workAreaSize
|
||||
const maxDimension = Math.max(screenSize.width, screenSize.height)
|
||||
return {
|
||||
width: maxDimension * window.devicePixelRatio,
|
||||
height: maxDimension * window.devicePixelRatio
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue