Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / resources / feedback / js / take_screenshot.js
blobaa3921f756cdbd069cb3e05b1db0952a461aeb91
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 /**
6  * Function to take the screenshot of the current screen.
7  * @param {function(HTMLCanvasElement)} callback Callback for returning the
8  *                                      canvas with the screenshot on it.
9  */
10 function takeScreenshot(callback) {
11   var screenshotStream = null;
12   var video = document.createElement('video');
14   video.addEventListener('canplay', function(e) {
15     if (screenshotStream) {
16       var canvas = document.createElement('canvas');
17       canvas.setAttribute('width', video.videoWidth);
18       canvas.setAttribute('height', video.videoHeight);
19       canvas.getContext('2d').drawImage(
20           video, 0, 0, video.videoWidth, video.videoHeight);
22       video.pause();
23       video.src = '';
25       screenshotStream.stop();
26       screenshotStream = null;
28       callback(canvas);
29     }
30   }, false);
32   navigator.webkitGetUserMedia(
33     {
34       video: {
35         mandatory: {
36           chromeMediaSource: 'screen',
37           maxWidth: 4096,
38           maxHeight: 2560
39         }
40       }
41     },
42     function(stream) {
43       if (stream) {
44         screenshotStream = stream;
45         video.src = window.URL.createObjectURL(screenshotStream);
46         video.play();
47       }
48     },
49     function(err) {
50       console.error('takeScreenshot failed: ' +
51           err.name + '; ' + err.message + '; ' + err.constraintName);
52     }
53   );