Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select a region of desktop screen with Electron

I'm trying to write an application that allow the user to select a region of the screen (like selecting to take a screen shot).

folders in home

Is that even possible?

like image 315
Med Abida Avatar asked Feb 28 '17 22:02

Med Abida


2 Answers

To specifically take a full screen shot, use the following code (example pulled from Electron Demo App). You can build off of this example, and use the screen, desktopCapturer and rectangle modules in the electron api to customize the code to get a specific screen/display, or select a specific bounding box (x/y coordinates and pixel area).

const electron = require('electron')
const desktopCapturer = electron.desktopCapturer
const electronScreen = electron.screen
const shell = electron.shell

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', function (event) {
  screenshotMsg.textContent = 'Gathering screens...'
  const thumbSize = determineScreenShotSize()
  let options = { types: ['screen'], thumbnailSize: thumbSize }

  desktopCapturer.getSources(options, function (error, sources) {
    if (error) return console.log(error)

    sources.forEach(function (source) {
      if (source.name === 'Entire screen' || source.name === 'Screen 1') {
        const screenshotPath = path.join(os.tmpdir(), 'screenshot.png')

        fs.writeFile(screenshotPath, source.thumbnail.toPng(), function (error) {
          if (error) return console.log(error)
          shell.openExternal('file://' + screenshotPath)
          const message = `Saved screenshot to: ${screenshotPath}`
          screenshotMsg.textContent = message
        })
      }
    })
  })
})

function determineScreenShotSize () {
  const screenSize = electronScreen.getPrimaryDisplay().workAreaSize
  const maxDimension = Math.max(screenSize.width, screenSize.height)
  return {
    width: maxDimension * window.devicePixelRatio,
    height: maxDimension * window.devicePixelRatio
  }
}

Other ways you could go about this are:

  1. Use object.getClientRects() in the DOM to specify specific elements you want to capture, although this would require foreknowledge of what they are.
  2. Add event listeners in your view to 'draw' the shape of what you want with mouseClick, mouseMove, etc. This stack overflow question has answers which could be adapted to fit what you want to do.
like image 143
unseen_damage Avatar answered Oct 24 '22 11:10

unseen_damage


I doubt you are still looking for a solution to this, but after digging i have found a way to do it using a combination of shelljs and clipboard.

  const userDataPath = (app).getPath(
    'userData'
  )
  const useP = path.join(userDataPath, 'uploads')
  let randomTmpfile = uniqueFilename(useP, 'prefix')
  shelljs.exec(`screencapture -ic ${randomTmpfile}.png`, function (res) {
    const image = clipboard.readImage('png').toDataURL()
  })
like image 26
Devon Ray Avatar answered Oct 24 '22 12:10

Devon Ray