@@ -5,8 +5,9 @@ import PlayButton from '@/components/icons/PlayButton.vue'
55import PauseButton from ' @/components/icons/PauseButton.vue'
66import RangeSlider from ' @/components/RangeSlider.vue'
77import ChartControlButton from ' @/components/ChartControlButton.vue'
8- import GithubIcon from ' @/components/icons/GithubIcon.vue'
98import { useTimer } from ' @/hooks/useTimer'
9+ import AppHeader from ' ./components/AppHeader.vue'
10+ import SpeedControls from ' ./components/SpeedControls.vue'
1011
1112const sliderValue = ref (30 )
1213const algorithmSelection = ref (' bubble' )
@@ -101,25 +102,6 @@ const resetChart = async () => {
101102 }
102103}
103104
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-
123105onMounted (() => {
124106 const sortingAlgo = algorithmSelection .value
125107 const button = document .getElementById (` ${ sortingAlgo} -button` )
@@ -143,6 +125,29 @@ watch([frame, totalFrames], () => {
143125 }
144126})
145127
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+
146151watch (sliderValue, () => {
147152 // Reset the elapsed frames and timer when the slider value changes
148153 frame .value = 0
@@ -155,19 +160,7 @@ watch(algorithmSelection, () => {
155160 </script >
156161
157162<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 />
171164
172165 <main id =" main-app" >
173166 <div id =" chart-container" >
@@ -267,21 +260,12 @@ watch(algorithmSelection, () => {
267260 :step =" 1"
268261 :disabled =" isPlaying"
269262 />
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+ />
285269 </div >
286270 </main >
287271</template >
@@ -382,24 +366,4 @@ header {
382366 gap : 10px ;
383367 width : 100% ;
384368}
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- }
405369 </style >
0 commit comments