-
-
Save Galkon/18fcba517464f6c1f09d79ce083c898a to your computer and use it in GitHub Desktop.
desktopCapturer thumbnail resolution bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP --> | |
<meta http-equiv="Content-Security-Policy" content="script-src 'self'"> | |
<title>desktopCapturer thumbnail resolution bug</title> | |
</head> | |
<body> | |
<button id="screenshot" style="margin-bottom: 16px">Capture Screenshot</button> | |
<img id="preview" src="" style="display: none; max-width: 100vw; max-height: 100vh;"></img> | |
<!-- You can also require other files to run in this process --> | |
<script src="./renderer.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Modules to control application life and create native browser window | |
const { app, ipcMain, BrowserWindow, desktopCapturer, screen } = require('electron') | |
const path = require('path') | |
const getActiveDisplayBounds = () => { | |
const cursorPoint = screen.getCursorScreenPoint() | |
const display = screen.getDisplayNearestPoint(cursorPoint) | |
return { | |
display, | |
bounds: { | |
x: display.workArea.x + 1, | |
y: display.workArea.y + 1, | |
width: display.workArea.width - 2, | |
height: display.workArea.height - 2 | |
} | |
} | |
} | |
const getScreenshotByDisplayId = async () => { | |
const {display, bounds} = getActiveDisplayBounds() | |
const thumbnailSize = { | |
width: bounds.width, | |
height: bounds.height | |
} | |
console.log('Target thumbnail size:', thumbnailSize) | |
const sources = await desktopCapturer.getSources({ | |
types: ['screen'], | |
thumbnailSize: thumbnailSize | |
}) | |
const source = sources.find(s => s.display_id == display.id) | |
const thumbnail = source.thumbnail | |
console.log('Thumbnail size:', thumbnail.getSize()) | |
return thumbnail.toDataURL() | |
} | |
function createWindow () { | |
// Create the browser window. | |
const mainWindow = new BrowserWindow({ | |
webPreferences: { | |
preload: path.join(__dirname, 'preload.js') | |
} | |
}) | |
mainWindow.loadFile('index.html') | |
} | |
// This method will be called when Electron has finished | |
// initialization and is ready to create browser windows. | |
// Some APIs can only be used after this event occurs. | |
app.whenReady().then(() => { | |
createWindow() | |
app.on('activate', function () { | |
// On macOS it's common to re-create a window in the app when the | |
// dock icon is clicked and there are no other windows open. | |
if (BrowserWindow.getAllWindows().length === 0) createWindow() | |
}) | |
}) | |
// Quit when all windows are closed, except on macOS. There, it's common | |
// for applications and their menu bar to stay active until the user quits | |
// explicitly with Cmd + Q. | |
app.on('window-all-closed', function () { | |
if (process.platform !== 'darwin') app.quit() | |
}) | |
ipcMain.handle('screenshot', async () => { | |
return await getScreenshotByDisplayId() | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "jumbled-swing-trouble-bw6qw", | |
"productName": "jumbled-swing-trouble-bw6qw", | |
"description": "My Electron application description", | |
"keywords": [], | |
"main": "./main.js", | |
"version": "1.0.0", | |
"author": "Galkon", | |
"scripts": { | |
"start": "electron ." | |
}, | |
"dependencies": {}, | |
"devDependencies": { | |
"electron": "29.3.2" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { contextBridge, ipcRenderer} = require('electron') | |
contextBridge.exposeInMainWorld('screenshot', async () => { | |
return ipcRenderer.invoke('screenshot') | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This file is loaded via the <script> tag in the index.html file and will | |
* be executed in the renderer process for that window. No Node.js APIs are | |
* available in this process because `nodeIntegration` is turned off and | |
* `contextIsolation` is turned on. Use the contextBridge API in `preload.js` | |
* to expose Node.js functionality from the main process. | |
*/ | |
const captureScreenshot = async () => { | |
const dataUrl = await window.screenshot() | |
const img = document.getElementById("preview") | |
img.src = dataUrl | |
img.style.display = "flex" | |
return dataUrl | |
} | |
window.addEventListener('DOMContentLoaded', () => { | |
const btn1 = document.getElementById('screenshot') | |
btn1.onclick = () => captureScreenshot() | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* styles.css */ | |
/* Add styles here to customize the appearance of your app */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment