@@ -5,8 +5,9 @@ import PlayButton from '@/components/icons/PlayButton.vue'
5
5
import PauseButton from ' @/components/icons/PauseButton.vue'
6
6
import RangeSlider from ' @/components/RangeSlider.vue'
7
7
import ChartControlButton from ' @/components/ChartControlButton.vue'
8
- import GithubIcon from ' @/components/icons/GithubIcon.vue'
9
8
import { useTimer } from ' @/hooks/useTimer'
9
+ import AppHeader from ' ./components/AppHeader.vue'
10
+ import SpeedControls from ' ./components/SpeedControls.vue'
10
11
11
12
const sliderValue = ref (30 )
12
13
const algorithmSelection = ref (' bubble' )
@@ -101,25 +102,6 @@ const resetChart = async () => {
101
102
}
102
103
}
103
104
104
- // Added function to update playback speed
105
- const updatePlaybackSpeed = (speed ) => {
106
- playbackSpeed .value = speed
107
-
108
- // If currently playing, restart the interval with new speed
109
- if (isPlaying .value && intervalId) {
110
- clearInterval (intervalId)
111
- playAlgorithm ()
112
- }
113
- }
114
-
115
- // Added speed presets
116
- const speedPresets = [
117
- { label: ' Slow' , value: 250 },
118
- { label: ' Medium' , value: 100 },
119
- { label: ' Fast' , value: 25 },
120
- { label: ' Very Fast' , value: 5 },
121
- ]
122
-
123
105
onMounted (() => {
124
106
const sortingAlgo = algorithmSelection .value
125
107
const button = document .getElementById (` ${ sortingAlgo} -button` )
@@ -143,6 +125,29 @@ watch([frame, totalFrames], () => {
143
125
}
144
126
})
145
127
128
+ const handlePlaybackSpeedUpdate = (newSpeed ) => {
129
+ playbackSpeed .value = newSpeed
130
+
131
+ // If currently playing, restart the interval with new speed
132
+ if (isPlaying .value && intervalId) {
133
+ clearInterval (intervalId)
134
+ intervalId = null
135
+
136
+ // Restart the interval with the new speed
137
+ let i = frame .value
138
+ intervalId = setInterval (() => {
139
+ frame .value = i
140
+ i++
141
+ if (i > totalFrames .value ) {
142
+ clearInterval (intervalId)
143
+ intervalId = null
144
+ isPlaying .value = false
145
+ timer .pause ()
146
+ }
147
+ }, playbackSpeed .value )
148
+ }
149
+ }
150
+
146
151
watch (sliderValue, () => {
147
152
// Reset the elapsed frames and timer when the slider value changes
148
153
frame .value = 0
@@ -155,19 +160,7 @@ watch(algorithmSelection, () => {
155
160
</script >
156
161
157
162
<template >
158
- <header >
159
- <img
160
- alt =" Vue logo"
161
- class =" logo"
162
- src =" ./assets/logo.svg"
163
- width =" 30"
164
- height =" 30"
165
- />
166
- <p >Timothy White - Vue JS Algorithm Sorting Project</p >
167
- <a href =" https://github.com/timwhite06/vuejs-sorting-algorithms" >
168
- <GithubIcon :width =" 32" :height =" 32" />
169
- </a >
170
- </header >
163
+ <AppHeader />
171
164
172
165
<main id =" main-app" >
173
166
<div id =" chart-container" >
@@ -267,21 +260,12 @@ watch(algorithmSelection, () => {
267
260
:step =" 1"
268
261
:disabled =" isPlaying"
269
262
/>
270
- <div id =" speed-controls" >
271
- <p >Speed per frame: {{ playbackSpeed }}ms</p >
272
- <div class =" speed-buttons" >
273
- <button
274
- v-for =" preset in speedPresets"
275
- :key =" preset.value"
276
- class =" speed-button"
277
- :class =" { active: playbackSpeed === preset.value }"
278
- @click =" updatePlaybackSpeed(preset.value)"
279
- :disabled =" isPlaying && playbackSpeed === preset.value"
280
- >
281
- {{ preset.label }}
282
- </button >
283
- </div >
284
- </div >
263
+
264
+ <SpeedControls
265
+ :playbackSpeed =" playbackSpeed"
266
+ :isPlaying =" isPlaying"
267
+ @update:playbackSpeed =" handlePlaybackSpeedUpdate"
268
+ />
285
269
</div >
286
270
</main >
287
271
</template >
@@ -382,24 +366,4 @@ header {
382
366
gap : 10px ;
383
367
width : 100% ;
384
368
}
385
-
386
- .speed-buttons {
387
- display : flex ;
388
- gap : 10px ;
389
- }
390
-
391
- .speed-button {
392
- width : 100% ;
393
- background-color : rgb (19 , 90 , 74 );
394
- color : white ;
395
- padding : 7px ;
396
- border : none ;
397
- border-radius : 5px ;
398
- cursor : pointer ;
399
- }
400
-
401
- .speed-button.active {
402
- background-color : rgb (91 , 236 , 255 );
403
- color : black ;
404
- }
405
369
</style >
0 commit comments