Skip to content

Commit b85775d

Browse files
committed
perf: using the mask instead WaveMask class
1 parent 726ab0a commit b85775d

File tree

4 files changed

+12
-70
lines changed

4 files changed

+12
-70
lines changed

example/src/components/Demo.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const finishHandler = (v: boolean) => {
6868
}
6969
7070
const clickHandler = (el: Ref<HTMLElement>) => {
71-
console.log(el)
71+
// do something
7272
}
7373
7474
const play = () => {

src/components/IllestWaveform.vue

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import { ref, onMounted, watchEffect, onUnmounted } from 'vue'
33
import WebAudioController from '../modules/WebAudio/Controller'
44
import Wave from '../modules/Wave/index'
5-
import WaveMask from '../modules/Wave/WaveMask'
65
import { formatSecond } from '../utils/format-time'
76
import {
87
lazyLoader,
@@ -70,19 +69,17 @@ function lazyLoadHandler() {
7069
7170
// initialize
7271
const waveRef = ref<HTMLCanvasElement | null>(null)
73-
const maskRef = ref<HTMLCanvasElement | null>(null)
7472
const ready = ref<boolean>(false)
7573
7674
let webAudioController: WebAudioController
7775
let wave: Wave
78-
let waveMask: WaveMask
7976
8077
// initialize waveform
8178
async function init(): Promise<void> {
8279
if (ready.value) return
8380
emits('onInit', true)
8481
await initAudio()
85-
await Promise.all([initWave(), initWaveMask()])
82+
await Promise.all([initWave()])
8683
ready.value = true
8784
emits('onReady', ready.value)
8885
}
@@ -104,22 +101,7 @@ async function initWave(): Promise<void> {
104101
wave.setupCanvas()
105102
watchEffect(() => {
106103
wave._props = props
107-
wave.setCanvasStyle()
108-
})
109-
}
110-
111-
// initialize wave mask canvas
112-
async function initWaveMask(): Promise<void> {
113-
waveMask = new WaveMask(
114-
maskRef.value as HTMLCanvasElement,
115-
props,
116-
await webAudioController._filterData,
117-
wave._canvas
118-
)
119-
waveMask.setupCanvas()
120-
watchEffect(() => {
121-
waveMask._props = props
122-
waveMask.setCanvasStyle()
104+
wave.setCanvasStyle(maskWidth.value)
123105
})
124106
}
125107
@@ -237,10 +219,6 @@ defineExpose({
237219

238220
<canvas id="ill-wave" ref="waveRef" />
239221

240-
<div id="ill-waveMask-container" :style="`width:${maskWidth}px;`">
241-
<canvas id="ill-waveMask" ref="maskRef" />
242-
</div>
243-
244222
<div
245223
v-show="ready && props.interact"
246224
id="ill-cursor"
@@ -286,19 +264,6 @@ defineExpose({
286264
opacity: 0;
287265
}
288266
289-
#ill-wave-container > #ill-waveMask-container {
290-
position: absolute;
291-
top: 0;
292-
left: 0;
293-
height: inherit;
294-
overflow: hidden;
295-
}
296-
297-
#ill-wave-container > #ill-waveMask-container > #ill-waveMask {
298-
opacity: 0;
299-
transition: opacity 0.5s ease-in-out;
300-
}
301-
302267
#ill-wave-container > #ill-cursor {
303268
position: absolute;
304269
height: inherit;

src/modules/wave/WaveMask.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/modules/wave/index.ts

Lines changed: 9 additions & 1 deletion
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.fillStyle = 'transparent'
3940
this.canvasCtx.fillRect(0, 0, this.canvas.width, this.canvas.height)
4041
}
4142

@@ -62,8 +63,15 @@ export default class Wave {
6263
})
6364
}
6465

65-
public setCanvasStyle(): void {
66+
private drawMask(maskWidth: number): void {
67+
this.canvasCtx.globalCompositeOperation = 'destination-atop'
68+
this.canvasCtx.fillStyle = this.props.maskColor as string
69+
this.canvasCtx.fillRect(0, 0, maskWidth, this.canvas.height)
70+
}
71+
72+
public setCanvasStyle(maskWidth: number): void {
6673
this.canvasCtx.clearRect(0, 0, this.canvas.width, this.canvas.height)
74+
this.drawMask(maskWidth)
6775
this.canvasCtx.lineWidth = this.props.lineWidth as number
6876
this.canvasCtx.lineCap = this.props.lineCap as CanvasLineCap
6977
this.canvasCtx.strokeStyle = this.props.lineColor as string

0 commit comments

Comments
 (0)