Skip to content

Commit a32e295

Browse files
committed
fix: set clearRect to avoid styling issues caused by canvas redraws
1 parent 9aa43ad commit a32e295

File tree

8 files changed

+108
-21
lines changed

8 files changed

+108
-21
lines changed

example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"dependencies": {
1313
"1llest-waveform-vue": "^1.0.3",
14+
"@vueuse/core": "^9.13.0",
1415
"nanoid": "^3.3.6",
1516
"sass": "^1.60.0",
1617
"vue": "^3.2.45"

example/pnpm-lock.yaml

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/src/App.vue

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,55 @@
11
<script setup lang="ts">
2-
import { reactive } from "vue"
3-
import { nanoid } from "nanoid"
4-
import Demo from "./components/Demo.vue"
5-
import type { IllestWaveformProps } from "1llest-waveform-vue"
2+
import { reactive } from 'vue'
3+
import { useDark, useToggle } from '@vueuse/core'
4+
import { nanoid } from 'nanoid'
5+
import Demo from './components/Demo.vue'
6+
// import type { IllestWaveformProps } from '1llest-waveform-vue'
7+
import type { IllestWaveformProps } from '../../src/types/waveform'
68
79
type Props = {
810
id: string
911
} & IllestWaveformProps
1012
13+
const isDark = useDark()
14+
const toggleDark = useToggle(isDark)
15+
1116
const items = reactive<Props[]>([
1217
{
1318
id: nanoid(),
14-
url:`${import.meta.env.BASE_URL}audios/loop-1.mp3`
19+
url: `${import.meta.env.BASE_URL}audios/loop-1.mp3`,
1520
},
1621
{
1722
id: nanoid(),
18-
url:`${import.meta.env.BASE_URL}audios/loop-2.mp3`
23+
url: `${import.meta.env.BASE_URL}audios/loop-2.mp3`,
1924
},
2025
{
2126
id: nanoid(),
22-
url:`${import.meta.env.BASE_URL}audios/loop-3.mp3`
27+
url: `${import.meta.env.BASE_URL}audios/loop-3.mp3`,
2328
},
2429
{
2530
id: nanoid(),
26-
url:`${import.meta.env.BASE_URL}audios/loop-4.mp3`
31+
url: `${import.meta.env.BASE_URL}audios/loop-4.mp3`,
2732
},
2833
{
2934
id: nanoid(),
30-
url:`${import.meta.env.BASE_URL}audios/loop-5.mp3`
35+
url: `${import.meta.env.BASE_URL}audios/loop-5.mp3`,
3136
},
3237
{
3338
id: nanoid(),
34-
url:`${import.meta.env.BASE_URL}audios/loop-6.mp3`
39+
url: `${import.meta.env.BASE_URL}audios/loop-6.mp3`,
3540
},
3641
{
3742
id: nanoid(),
38-
url:`${import.meta.env.BASE_URL}audios/loop-7.mp3`
43+
url: `${import.meta.env.BASE_URL}audios/loop-7.mp3`,
3944
},
4045
{
4146
id: nanoid(),
42-
url:`${import.meta.env.BASE_URL}audios/loop-8.mp3`
47+
url: `${import.meta.env.BASE_URL}audios/loop-8.mp3`,
4348
},
4449
{
4550
id: nanoid(),
46-
url:`${import.meta.env.BASE_URL}audios/loop-9.mp3`
47-
}
51+
url: `${import.meta.env.BASE_URL}audios/loop-9.mp3`,
52+
},
4853
])
4954
</script>
5055

@@ -58,6 +63,10 @@ const items = reactive<Props[]>([
5863
</span>
5964
</h1>
6065

66+
<div>
67+
<button @click="toggleDark()">theme</button>
68+
</div>
69+
6170
<Demo v-for="item in items" :key="item.id" :props="item" />
6271
</section>
6372
</template>

example/src/components/Demo.vue

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
11
<script setup lang="ts">
22
import { onMounted, reactive, ref, watchEffect, defineProps } from 'vue'
33
import type { Ref } from 'vue'
4-
import { IllestWaveform } from '1llest-waveform-vue'
5-
// import IllestWaveform from '../../../src/components/IllestWaveform.vue'
6-
import type { IllestWaveformProps } from '1llest-waveform-vue'
4+
import { useDark } from '@vueuse/core'
5+
// import { IllestWaveform } from '1llest-waveform-vue'
6+
// import type { IllestWaveformProps } from '1llest-waveform-vue'
7+
import IllestWaveform from '../../../src/components/IllestWaveform.vue'
8+
import type { IllestWaveformProps } from '../../../src/types/waveform'
79
import '1llest-waveform-vue/dist/style.css'
810
import PlayIcon from './icons/Play.vue'
911
import PauseIcon from './icons/Pause.vue'
1012
import ReplayIcon from './icons/Replay.vue'
1113
1214
const { props } = defineProps(['props'])
1315
16+
const darkMode = useDark()
17+
1418
const waveOptions = reactive<IllestWaveformProps>({
1519
url: props.url,
1620
})
1721
22+
watchEffect(() => {
23+
if (darkMode.value) {
24+
waveOptions.lineColor = '#5e5e5e'
25+
waveOptions.maskColor = '#fff'
26+
waveOptions.skeletonColor = '#232323'
27+
waveOptions.cursorColor = '#fff'
28+
} else {
29+
waveOptions.lineColor = '#a1a1aa'
30+
waveOptions.maskColor = '#000'
31+
waveOptions.skeletonColor = '#f3f4f6'
32+
waveOptions.cursorColor = '#000'
33+
}
34+
})
35+
1836
const waveformRef = ref<typeof IllestWaveform | null>(null)
1937
2038
onMounted(() => {
@@ -121,7 +139,7 @@ const getDuration = () => {
121139

122140
<style scoped lang="scss">
123141
.btn {
124-
@apply flex items-center bg-neutral-700 px-5 py-1 rounded-sm;
142+
@apply flex items-center bg-neutral-200 dark:bg-neutral-700 px-5 py-1 rounded-sm;
125143
126144
& div {
127145
@apply ml-1 font-bold -mb-0.5;

example/src/style.css

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22
@tailwind components;
33
@tailwind utilities;
44

5+
:root {
6+
--dark-main: #1e1e1e;
7+
}
8+
59
html {
6-
color: rgb(213, 213, 213);
7-
background-color: #1e1e1e;
10+
@apply text-slate-900 bg-white;
811
font-family: "JetBrains Mono";
12+
transition: background-color,color ease-in 0.2s
13+
}
14+
15+
.dark {
16+
@apply text-white bg-[var(--dark-main)];
917
}
1018

1119

example/tailwind.config.cjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/** @type {import('tailwindcss').Config} */
22

33
module.exports = {
4-
content: ["./index.html", "./src/**/*.{vue,js,ts,jsx,tsx}"],
4+
content: ['./index.html', './src/**/*.{vue,js,ts,jsx,tsx}'],
5+
darkMode: 'class',
56
theme: {
67
extend: {},
78
},

src/modules/wave/WaveMask.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ export default class WaveMask extends Wave {
1818
this.canvas.width = this.waveCanvas.width
1919
this.canvas.height = this.waveCanvas.height
2020
this.canvas.style.opacity = '1'
21+
this.canvasCtx.fillRect(0, 0, this.canvas.width, this.canvas.height)
2122
}
2223

2324
public setCanvasStyle(): void {
25+
this.canvasCtx.clearRect(0, 0, this.canvas.width, this.canvas.height)
2426
this.canvasCtx.lineWidth = this.props.lineWidth as number
2527
this.canvasCtx.lineCap = this.props.lineCap as CanvasLineCap
2628
this.canvasCtx.strokeStyle = this.props.maskColor as string

src/modules/wave/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export default class Wave {
3636
this.canvas.width = this.canvas.offsetWidth
3737
this.canvas.height = this.canvas.offsetHeight
3838
this.canvas.style.opacity = '1'
39+
this.canvasCtx.fillRect(0, 0, this.canvas.width, this.canvas.height)
3940
}
4041

4142
private translateCanvasCtx(): void {
@@ -62,6 +63,7 @@ export default class Wave {
6263
}
6364

6465
public setCanvasStyle(): void {
66+
this.canvasCtx.clearRect(0, 0, this.canvas.width, this.canvas.height)
6567
this.canvasCtx.lineWidth = this.props.lineWidth as number
6668
this.canvasCtx.lineCap = this.props.lineCap as CanvasLineCap
6769
this.canvasCtx.strokeStyle = this.props.lineColor as string

0 commit comments

Comments
 (0)