Skip to content

Commit 9000025

Browse files
committed
set background from mobile camera/file
1 parent c066464 commit 9000025

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

angular-ui-scribble.js

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ angular.module('angular-ui-scribble',[])
44
scope:{},
55
template:
66
`<div class="scribble">
7-
<div class="scribble-canvas">
8-
<canvas height="300" width="600"></canvas>
7+
<ul class="scribble-actions">
8+
<li><button ng-click="clearSignature()">Clear</button></li>
9+
<li ng-if='mode!="erase"'><button ng-click="setMode('erase')">Erase</button></li>
10+
<li ng-if='mode=="erase"'><button ng-click="setMode('pen')">Pen</button></li>
11+
<li><input id="selectBackground" type="file" accept="image/*" capture="camera"></lis>
12+
</ul>
13+
<div class="scribble-canvas" height="200" width="350">
14+
<canvas height="200" width="380" style="z-index:2;"></canvas>
15+
<canvas id="scribble-background" height="200" width="380" style="z-index:1;"></canvas>
916
</div>
10-
<button ng-click="clearSignature()">Clear</button>
11-
<button ng-click="setMode('erase')">Erase</button>
12-
<button ng-click="setMode('pen')">Pen</button>
1317
</div>`,
1418
controller: function($scope, $element){
1519
$scope.mode = 'pen';
@@ -18,6 +22,8 @@ angular.module('angular-ui-scribble',[])
1822
var ctx = canvas.getContext('2d');
1923
signaturePad = new SignaturePad(canvas);
2024

25+
$scope.isMobile = true;
26+
2127
$scope.clearSignature = function(){
2228
signaturePad.clear();
2329
};
@@ -42,6 +48,37 @@ angular.module('angular-ui-scribble',[])
4248
signaturePad.maxWidth = $scope.oldStroke.maxWidth;
4349
}
4450
});
51+
52+
// Background {{{
53+
54+
var selectBackground = document.getElementById('selectBackground');
55+
var reader;
56+
57+
selectBackground.addEventListener('change', function(e){
58+
var backgroundSrc = selectBackground.files[0];
59+
reader = new FileReader();
60+
61+
reader.onload = function(event){
62+
loadBackground(event.target.result);
63+
};
64+
65+
reader.readAsDataURL(backgroundSrc);
66+
});
67+
68+
function loadBackground(dataUrl){
69+
var background = document.getElementById('scribble-background');
70+
var backgroundCtx = background.getContext('2d');
71+
var image = new Image();
72+
var ratio = window.devicePixelRatio || 1;
73+
var width = background.width;
74+
var height = background.height;
75+
76+
image.src = dataUrl;
77+
image.onload = function () {
78+
backgroundCtx.drawImage(image, 0, 0, width, height);
79+
};
80+
}
81+
// }}}
4582
}
4683
}
4784
});

demo/index.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
<body ng-app="app">
1111
<div ng-controller="scribbleExampleCtrl">
1212

13-
<h2>Scribble 1</h2>
14-
<ui-scribble></ui-scribble>
15-
<h2>Scribble 2</h2>
13+
<h2>Scribble</h2>
1614
<ui-scribble></ui-scribble>
1715
<script>
1816
var app = angular.module("app", [

demo/style.css

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/* Demo css*/
12
.scribble-canvas canvas{
23
border: 1px solid #f4f4f4;
34
}
5+
/* Add to package css*/
6+
.scribble-canvas {
7+
position: relative;
8+
overflow: auto;
9+
height: 100%;
10+
}
11+
.scribble-canvas canvas{
12+
position: absolute;
13+
top: 0;
14+
left: 0;
15+
}
16+
.scribble-actions li{
17+
list-style: none;
18+
display: inline;
19+
width: 100px;
20+
}
21+
.scribble-actions button{
22+
width: 70px;
23+
}

0 commit comments

Comments
 (0)