@@ -22,47 +22,66 @@ canvas.height = height;
22
22
23
23
24
24
let isDrawing = false ;
25
- let lastPositionX = null ;
25
+ let lastPosition = { x : null , y : null } ;
26
26
canvas . addEventListener ( 'mousemove' , e => {
27
27
if ( isDrawing && sortingTimeout === null ) {
28
- saveValue ( e . offsetX , e . offsetY , lastPositionX ) ;
28
+ saveValue ( e . offsetX , e . offsetY , lastPosition . x , lastPosition . y ) ;
29
29
}
30
- lastPositionX = e . offsetX ;
30
+ lastPosition . x = e . offsetX ;
31
+ lastPosition . y = e . offsetY ;
31
32
} ) ;
32
33
window . addEventListener ( 'mousedown' , ( ) => {
33
34
isDrawing = true ;
34
35
} ) ;
35
36
window . addEventListener ( 'mouseup' , ( ) => {
36
37
isDrawing = false ;
37
- lastPositionX = null ;
38
+ lastPosition . x = null ;
39
+ lastPosition . y = null ;
38
40
} ) ;
39
41
40
- function saveValue ( x1 , y1 , x2 ) {
41
- x1 = Math . max ( Math . min ( width , x1 ) , 0 )
42
+ function saveValue ( x1 , y1 , x2 , y2 ) {
43
+ x1 = Math . max ( Math . min ( width , x1 ) , 0 ) ;
42
44
if ( x2 == null ) {
43
- x2 = x1
45
+ x2 = x1 ;
44
46
} else {
45
- x2 = Math . max ( Math . min ( width , x2 ) , 0 )
47
+ x2 = Math . max ( Math . min ( width , x2 ) , 0 ) ;
46
48
}
47
- y1 = Math . max ( Math . min ( height , y1 ) , 0 )
48
49
49
- let index1 = Math . min ( Math . floor ( ( x1 / width ) * sizeSlider . value ) , sizeSlider . value - 1 )
50
+ y1 = Math . max ( Math . min ( height , y1 ) , 0 ) ;
51
+ if ( y2 == null ) {
52
+ y2 = y1 ;
53
+ } else {
54
+ y2 = Math . max ( Math . min ( height , y2 ) , 0 ) ;
55
+ }
56
+
57
+ let index1 = Math . min ( Math . floor ( ( x1 / width ) * sizeSlider . value ) , sizeSlider . value - 1 ) ;
50
58
let index2 = Math . min ( Math . floor ( ( x2 / width ) * sizeSlider . value ) , sizeSlider . value - 1 ) ;
51
59
if ( index1 > index2 ) {
52
60
let saveValue = index1 ;
53
61
index1 = index2 ;
54
62
index2 = saveValue ;
63
+ saveValue = y2 ;
64
+ y2 = y1 ;
65
+ y1 = saveValue ;
55
66
}
56
- let value = y1 / height ;
57
- for ( ; index1 <= index2 ; index1 ++ ) {
58
- arrayToSort [ index1 ] = Math . min ( 1 , value + Math . random ( ) / 500 ) ;
67
+
68
+ if ( index1 === index2 ) {
69
+ arrayToSort [ index1 ] = randomiseValue ( y1 / height ) ;
70
+ } else {
71
+ for ( let i = index1 ; i <= index2 ; i ++ ) {
72
+ arrayToSort [ i ] = randomiseValue ( map_range ( i , index1 , index2 , y1 , y2 ) / height ) ;
73
+ }
59
74
}
60
- draw ( )
75
+ draw ( ) ;
61
76
if ( sortIndex != null ) {
62
77
updateSortMethod ( sortIndex ) ;
63
78
}
64
79
}
65
80
81
+ function randomiseValue ( value , divider = 500 ) {
82
+ return Math . max ( 0.0001 , Math . min ( 0.999999 , value + Math . random ( ) / divider ) - Math . random ( ) / divider ) ;
83
+ }
84
+
66
85
67
86
const cnt = canvas . getContext ( "2d" ) ;
68
87
cnt . font = "30px Arial" ;
0 commit comments