Skip to content

Commit de946c1

Browse files
authored
Merge pull request #9 from timwhite06/component_a_rise
comeponent-a-rise
2 parents 3197051 + 95d7351 commit de946c1

File tree

4 files changed

+134
-68
lines changed

4 files changed

+134
-68
lines changed

src/App.vue

Lines changed: 32 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import PlayButton from '@/components/icons/PlayButton.vue'
55
import PauseButton from '@/components/icons/PauseButton.vue'
66
import RangeSlider from '@/components/RangeSlider.vue'
77
import ChartControlButton from '@/components/ChartControlButton.vue'
8-
import GithubIcon from '@/components/icons/GithubIcon.vue'
98
import { useTimer } from '@/hooks/useTimer'
9+
import AppHeader from './components/AppHeader.vue'
10+
import SpeedControls from './components/SpeedControls.vue'
1011
1112
const sliderValue = ref(30)
1213
const 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-
123105
onMounted(() => {
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+
146151
watch(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>

src/components/AppHeader.vue

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script setup>
2+
defineOptions({
3+
name: 'AppHeader',
4+
})
5+
import GithubIcon from './icons/GithubIcon.vue'
6+
</script>
7+
8+
<template>
9+
<header>
10+
<img
11+
alt="Vue logo"
12+
class="logo"
13+
src="./../assets/logo.svg"
14+
width="30"
15+
height="30"
16+
/>
17+
<p>Timothy White - Vue JS Algorithm Sorting Project</p>
18+
<a href="https://github.com/timwhite06/vuejs-sorting-algorithms">
19+
<GithubIcon :width="32" :height="32" />
20+
</a>
21+
</header>
22+
</template>

src/components/SpeedControls.vue

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<script>
2+
export default {
3+
props: {
4+
playbackSpeed: {
5+
type: Number,
6+
required: true,
7+
},
8+
isPlaying: {
9+
type: Boolean,
10+
required: true,
11+
},
12+
},
13+
data() {
14+
return {
15+
// Speed presets
16+
speedPresets: [
17+
{ label: 'Slow', value: 250 },
18+
{ label: 'Medium', value: 100 },
19+
{ label: 'Fast', value: 25 },
20+
{ label: 'Very Fast', value: 5 },
21+
],
22+
}
23+
},
24+
methods: {
25+
// Update playback speed method
26+
updatePlaybackSpeed(speed) {
27+
this.$emit('update:playbackSpeed', speed)
28+
},
29+
},
30+
}
31+
</script>
32+
33+
<template>
34+
<div id="speed-controls">
35+
<p>Speed per frame: {{ playbackSpeed }}ms</p>
36+
<div class="speed-buttons">
37+
<button
38+
v-for="preset in speedPresets"
39+
:key="preset.value"
40+
class="speed-button"
41+
:class="{ active: playbackSpeed === preset.value }"
42+
@click="updatePlaybackSpeed(preset.value)"
43+
:disabled="isPlaying && playbackSpeed === preset.value"
44+
>
45+
{{ preset.label }}
46+
</button>
47+
</div>
48+
</div>
49+
</template>
50+
51+
<style scoped>
52+
#speed-controls {
53+
width: 100%;
54+
}
55+
56+
.speed-buttons {
57+
display: flex;
58+
gap: 10px;
59+
}
60+
61+
.speed-button {
62+
width: 100%;
63+
background-color: rgb(19, 90, 74);
64+
color: white;
65+
padding: 7px;
66+
border: none;
67+
border-radius: 5px;
68+
cursor: pointer;
69+
}
70+
71+
.speed-button.active {
72+
background-color: rgb(91, 236, 255);
73+
color: black;
74+
}
75+
76+
.speed-button:disabled {
77+
opacity: 0.6;
78+
cursor: not-allowed;
79+
}
80+
</style>

src/hooks/useSessionStorage.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)