Skip to content
This repository has been archived by the owner on May 10, 2018. It is now read-only.

Screenshot only works for visible area #24

Closed
uuf6429 opened this issue May 2, 2017 · 0 comments
Closed

Screenshot only works for visible area #24

uuf6429 opened this issue May 2, 2017 · 0 comments
Labels

Comments

@uuf6429
Copy link
Owner

uuf6429 commented May 2, 2017

This seems to be a known limitation of Electron.

Potential solution:

  • create browser windows with some special settings:
    options.x = 0;
    options.y = 0;
    options.enableLargerThanScreen = true;
  • find content size (document.body.scroll<S>)
  • resize the window (content size + window.outer<S> - window.inner<S>)
  • take screenshot
  • resize back to original size

Some tentative code:

                Logger.debug('getScreenshot()');

                screenshotResponse = null;

                currWindow.webContents
                    .executeJavaScript('({' +
                        'cw: document.body.clientWidth,' +
                        'ch: document.body.clientHeight,' +
                        'pw: window.outerWidth - window.innerWidth,' +
                        'ph: window.outerHeight - window.innerHeight' +
                    '})', false, function (s) {
                        const scaleFactor = Electron.screen.getPrimaryDisplay().scaleFactor,
                            origSize = currWindow.getSize();

                        Logger.alert('Setting window size to %dx%d...', s.cw + s.pw, s.ch + s.ph);
                        currWindow.setSize(s.cw + s.pw, s.ch + s.ph, false);
                        Logger.alert('Window size is now: %j', currWindow.getSize());

                        const tryTakingScreenshot = function (tries) {
                            currWindow.capturePage(
                                /*{
                                    x: 0,
                                    y: 0,
                                    width: +s.cw * scaleFactor,
                                    height: +s.ch * scaleFactor
                                },*/
                                function (image) {
                                    const data = image.toPNG().toString('base64');

                                    if (data) {
                                        screenshotResponse = {'base64data': data};
                                        currWindow.setSize(origSize[0], origSize[1], false);
                                    } else if (tries > 0) {
                                        Logger.warn('Failed to take screen shot, trying again (try %d).', tries);
                                        setTimeout(function () {
                                            tryTakingScreenshot(tries - 1);
                                        }, 200);
                                    } else {
                                        screenshotResponse = {'error': 'Gave up trying to take screen shot after several tries.'};
                                        currWindow.setSize(origSize[0], origSize[1], false);
                                    }
                                }
                            );
                        };

                        tryTakingScreenshot(5);
                    });

                cb();

Also see: https://github.com/FWeinb/electron-screenshot-app/blob/6c165089797d30ad66a0b19398ca52446e0d3e88/index.js

@uuf6429 uuf6429 added the bug label May 2, 2017
@uuf6429 uuf6429 closed this as completed in 9b7b64a May 2, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant