Skip to content

Instantly share code, notes, and snippets.

@yuristrelets
Created September 11, 2018 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuristrelets/c09e832b018a33a671c71485116a0188 to your computer and use it in GitHub Desktop.
Save yuristrelets/c09e832b018a33a671c71485116a0188 to your computer and use it in GitHub Desktop.
Electron Fiddle Gist
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<canvas></canvas>
<script>
require('./renderer.js')
</script>
</body>
</html>
// Access information about media sources that can be used to capture audio
// and video from the desktop using the navigator.mediaDevices.getUserMedia API.
//
// For more info, see:
// https://electronjs.org/docs/api/desktop-capturer
const { app, BrowserWindow } = require('electron')
let mainWindow = null
app.on('ready', () => {
mainWindow = new BrowserWindow({ height: 800, width: 1200 })
mainWindow.loadFile('index.html')
})
const { desktopCapturer, screen } = require('electron')
const displays = screen.getAllDisplays();
console.log(displays);
const thumbnailSize = displays.reduce((result, { size }) => {
return {
width: size.width > result.width ? size.width : result.width,
height: size.height > result.height ? size.height : result.height,
};
}, { width: 0, height: 0 });
console.log(thumbnailSize);
desktopCapturer.getSources({
types: ['screen'],
thumbnailSize,
}, (error, sources) => {
if (error) throw error;
for (let i = 0; i < sources.length; ++i) {
console.log(sources[i]);
console.log(sources[i].thumbnail.getSize());
drawImage(sources[i].thumbnail);
break;
}
})
function drawImage(nativeImage) {
const { width, height } = nativeImage.getSize();
const canvas = document.querySelector('canvas');
const context = canvas.getContext('2d');
const image = new Image();
canvas.width = width;
canvas.height = height;
image.onload = () => {
context.drawImage(image, 0, 0, width, height);
}
image.src = nativeImage.toDataURL();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment