Skip to content

Commit 5bd8973

Browse files
committed
bugfix: sizes and mirror screenshot
1 parent c271fa9 commit 5bd8973

File tree

2 files changed

+41
-28
lines changed

2 files changed

+41
-28
lines changed

angular-ui-scribble.css

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
.scribble-canvas video {
2+
object-fit: fill;
3+
transform: scaleX(-1);
4+
}
15
.scribble-canvas {
26
position: relative;
3-
overflow: auto;
4-
height: 230px;
5-
/*height: 100%;*/
7+
height: 100%;
8+
width: 100%;
69
}
710
.scribble-canvas canvas {
811
position: absolute;

angular-ui-scribble.js

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,38 @@ angular.module('angular-ui-scribble',[])
1414
'<li ng-if=\'mode=="erase"\'><button ng-click="setMode(\'pen\')">Pen</button></li>'+
1515
'<li ng-if=\'mode!="streaming" && !isMobile\'><button ng-click="setBackground()">Background</button></li>'+
1616
'<li ng-if=\'mode=="streaming" && !isMobile\'><button ng-click="screenshot()">Screenshot</button></li>'+
17-
'<li><input id="selectBackground" type="file" accept="image/*" capture="camera"></lis>'+
17+
'<li><input class="selectBackground" type="file" accept="image/*" capture="camera"></lis>'+
1818
'</ul>'+
19-
'<div class="scribble-canvas" height="200" width="350">'+
20-
'<video ng-show=\'mode=="streaming"\' height="200" width="380" autoplay id="videoFeed" style="z-index:2;"></video>'+
21-
'<canvas height="200" width="380" style="z-index:3;"></canvas>'+
22-
'<canvas ng-show=\'mode!="streaming"\' id="scribble-background" height="200" width="380" style="z-index:1;"></canvas>'+
19+
'<div class="scribble-canvas" height="{{scribbleHeight}}" width="{{scribbleWidth}}">'+
20+
'<video ng-show=\'mode=="streaming"\' height="{{scribbleHeight}}" width="{{scribbleWidth}}" autoplay class="videoFeed" style="z-index:2;"></video>'+
21+
'<canvas class="scribble-board" height="{{scribbleHeight}}" width="{{scribbleWidth}}" style="z-index:3;"></canvas>'+
22+
'<canvas class="scribble-background" ng-show=\'mode!="streaming"\' height="{{scribbleHeight}}" width="{{scribbleWidth}}" style="z-index:1;"></canvas>'+
2323
'<button ng-if="signatureReady" ng-click="callbackBtn({signature: signaturePad.toDataURL()})">Done</button>'+
2424
'</div>'+
2525
'</div>',
2626
controller: function($scope, $element, $attrs){
2727
$scope.isMobile = false; //TODO: detect mobile/desktop version
2828
$scope.mode = 'pen';
2929
$scope.signaturePad;
30+
$scope.scribbleHeight = 400;
31+
$scope.scribbleWidth = 400;
3032

31-
var canvas = $element.find('canvas')[0];
33+
var canvas = $element[0].querySelector('.scribble-board');
3234
var ctx = canvas.getContext('2d');
35+
36+
var canvasBackground = $element[0].querySelector('.scribble-background');
37+
var ctxBackground = canvasBackground.getContext('2d');
38+
39+
// Flip the screenshot {{{
40+
//TODO: not flipping the screenshot
41+
var reversed = false;
42+
// ctxBackground.translate(canvasBackground.width, 0);
43+
// ctxBackground.scale(-1, 1);
44+
// }}}
45+
3346
$scope.signaturePad = new SignaturePad(canvas);
3447

35-
var video = document.querySelector('#videoFeed');
48+
var video = $element[0].querySelector('.videoFeed');
3649
var videoStream;
3750
// check for getUserMedia support
3851
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
@@ -48,7 +61,8 @@ angular.module('angular-ui-scribble',[])
4861

4962
function handleVideo(stream){
5063
video.src = window.URL.createObjectURL(stream);
51-
videoStream = stream.getTracks()[0];
64+
streamOriginal = stream;
65+
videoStream = stream
5266
}
5367

5468
function videoError(){}
@@ -57,17 +71,18 @@ angular.module('angular-ui-scribble',[])
5771
$scope.setMode('pen');
5872
if(video.paused || video.ended) console.log("no video");;
5973
if(video.paused || video.ended) return false;
74+
//TODO: hack to flip context only once {{{
75+
if (!reversed) {
76+
reversed = true;
77+
ctxBackground.translate(canvasBackground.width, 0);
78+
ctxBackground.scale(-1, 1);
79+
}
80+
// }}}
6081

61-
var background = document.getElementById('scribble-background');
62-
var backgroundCtx = background.getContext('2d');
63-
var width = background.width;
64-
var height = background.height;
65-
66-
backgroundCtx.drawImage(video, 0, 0, width, height);
67-
videoStream.stop();
82+
ctxBackground.drawImage(video, 0, 0, $scope.scribbleWidth, $scope.scribbleHeight);
83+
videoStream.getTracks()[0].stop();
6884
};
6985

70-
7186
// Expose original signaturePad object
7287
$scope.config.getSignaturePad = function(){
7388
return signaturePad;
@@ -116,9 +131,8 @@ angular.module('angular-ui-scribble',[])
116131
}
117132
});
118133

119-
// Background {{{
120-
121-
var selectBackground = document.getElementById('selectBackground');
134+
// Background - mobile {{{
135+
var selectBackground = $element[0].querySelector('.selectBackground')
122136
var reader;
123137

124138
selectBackground.addEventListener('change', function(e){
@@ -133,17 +147,13 @@ angular.module('angular-ui-scribble',[])
133147
});
134148

135149
function loadBackground(dataUrl){
136-
var background = document.getElementById('scribble-background');
137-
var backgroundCtx = background.getContext('2d');
138150
var image = new Image();
139151
var ratio = window.devicePixelRatio || 1;
140-
var width = background.width;
141-
var height = background.height;
142152

143153
image.src = dataUrl;
144154
image.onload = function () {
145-
backgroundCtx.clearRect(0, 0, canvas.width, canvas.height);
146-
backgroundCtx.drawImage(image, 0, 0, width, height);
155+
ctxBackground.clearRect(0, 0, canvas.width, canvas.height);
156+
ctxBackground.drawImage(image, 0, 0, canvasBackground.width, canvasBackground.height);
147157
};
148158
}
149159
// }}}

0 commit comments

Comments
 (0)