11const canvas = document . getElementById ( 'cvs' )
2+ const stopButton = document . getElementById ( 'stopbutton' )
23const ctx = canvas . getContext ( '2d' )
34const CH = ( canvas . height = 300 )
45const CW = ( canvas . width = 600 )
@@ -18,10 +19,14 @@ const BubbleSort = {
1819 ctx . clearRect ( 0 , 0 , CH , CW )
1920 const lineSpacing = 10
2021 ctx . lineWidth = 5
22+ << < << << HEAD
23+ for ( let j = 1 ; j < this . seedValue . length ; j ++ ) {
24+ === === =
2125
2226 for ( let j = 0 ; j < this . seedValue . length ; j ++ ) {
27+ >>> >>> > 141 ad495338db7854c07ee1ae9ffeeb6ebc6f429
2328 const y = j * lineSpacing
24- ctx . beginPath ( )
29+ ctx . beginPath ( )
2530 ctx . moveTo ( y , CH )
2631 ctx . lineTo ( y , CH - this . seedValue [ j ] )
2732 ctx . closePath ( )
@@ -37,14 +42,15 @@ const BubbleSort = {
3742 }
3843 } ,
3944 update ( ) {
45+
4046 if ( this . seedValue [ this . j ] > this . seedValue [ this . j + 1 ] ) {
4147 ; [ this . seedValue [ this . j ] , this . seedValue [ this . j + 1 ] ] = [
4248 this . seedValue [ this . j + 1 ] ,
4349 this . seedValue [ this . j ] ,
4450 ]
4551 this . sortedLine = this . seedValue [ this . j + 1 ]
4652 }
47- if ( this . i < this . seedValue . length ) {
53+ if ( this . i < this . seedValue . length || isClick ) {
4854 this . j ++
4955 if ( this . j >= this . seedValue . length - this . i - 1 ) {
5056 this . color = 'red'
@@ -53,7 +59,9 @@ const BubbleSort = {
5359 }
5460 } else {
5561 this . isLoop = false
62+ clearInterval ( )
5663 }
64+
5765 this . draw ( )
5866 } ,
5967 startup ( ) {
@@ -63,18 +71,31 @@ const BubbleSort = {
6371 this . seedValue [ this . i ] = Math . random ( ) * ( CH - 0 + 1 ) + 0
6472 }
6573 this . i = 0
74+
75+ // Hendling Evnets.
6676 } ,
77+ stopLoop ( ) {
78+ clearInterval ( ) ;
79+ } ,
6780}
6881
6982// You could use window.requestAnimationFrame as well. but, for simplicity i'm using setInterval.
70-
83+ let cleaer ;
84+ stopButton . addEventListener ( 'click' , ( ) => {
85+ // p
86+ // clearInterval(cleaer);
87+ } )
7188function render ( ) {
7289 BubbleSort . startup ( )
73- if ( BubbleSort . isLoop ) {
74- setInterval ( ( ) => {
90+ if ( BubbleSort . isLoop ) {
91+
92+ cleaer = setInterval ( ( ) => {
7593 BubbleSort . update ( BubbleSort . seedValue )
94+
7695 } , BubbleSort . loopSpeed )
77- }
96+ } else {
97+ console . log ( "final" ) ;
98+ }
7899}
79100
80101render ( ) // calling the main functions.
0 commit comments