@@ -9,6 +9,12 @@ export function initialize(dotNetAdapter, element, elementId, options) {
9
9
if ( ! element )
10
10
return ;
11
11
12
+ // we need to match the canvas size ans size in styles so that we can properly calculate scaling based on screen size
13
+ if ( element . width && element . height ) {
14
+ element . style . width = `${ element . width } px` ;
15
+ element . style . height = `${ element . height } px` ;
16
+ }
17
+
12
18
const sigpad = new SignaturePad ( element , {
13
19
minWidth : options . minLineWidth || 0.5 ,
14
20
maxWidth : options . maxLineWidth || 2.5 ,
@@ -31,11 +37,14 @@ export function initialize(dotNetAdapter, element, elementId, options) {
31
37
const instance = {
32
38
options : options ,
33
39
sigpad : sigpad ,
34
- dotNetAdapter : dotNetAdapter
40
+ dotNetAdapter : dotNetAdapter ,
41
+ element : element
35
42
} ;
36
43
37
44
registerToEvents ( dotNetAdapter , instance ) ;
38
45
46
+ resizeCanvas ( sigpad , element ) ;
47
+
39
48
_instances [ elementId ] = instance ;
40
49
}
41
50
@@ -176,4 +185,38 @@ function getImageDataURL(sigpad, options) {
176
185
}
177
186
178
187
return sigpad . toDataURL ( "image/png" , options . imageQuality || 1 ) ;
188
+ }
189
+
190
+ window . onresize = resizeAllCanvas ;
191
+
192
+ function resizeAllCanvas ( ) {
193
+ if ( _instances ) {
194
+ for ( let i = 0 ; i < _instances . length ; i ++ ) {
195
+ const instance = _instances [ i ] ;
196
+
197
+ if ( instance ) {
198
+ resizeCanvas ( instance . sigpad , instance . element ) ;
199
+ }
200
+ }
201
+ }
202
+ }
203
+
204
+ function resizeCanvas ( sigpad , canvas ) {
205
+ if ( ! sigpad || ! canvas ) {
206
+ return null ;
207
+ }
208
+
209
+ // When zoomed out to less than 100%, for some very strange reason,
210
+ // some browsers report devicePixelRatio as less than 1
211
+ // and only part of the canvas is cleared then.
212
+ const ratio = Math . max ( window . devicePixelRatio || 1 , 1 ) ;
213
+
214
+ const context = canvas . getContext ( "2d" ) ;
215
+
216
+ // This part causes the canvas to be cleared
217
+ canvas . width = canvas . offsetWidth * ratio ;
218
+ canvas . height = canvas . offsetHeight * ratio ;
219
+ context . scale ( ratio , ratio ) ;
220
+
221
+ sigpad . fromData ( sigpad . toData ( ) ) ;
179
222
}
0 commit comments