Skip to content

Commit 3377472

Browse files
committed
style: rewrite Wave class
1 parent 6c8da1f commit 3377472

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

src/components/IllestWaveform.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ async function initWave(): Promise<void> {
100100
wave.setupCanvas()
101101
watchEffect(() => {
102102
wave._props = props
103-
wave.setCanvasStyle(maskWidth.value)
103+
wave.setWaveStyle(maskWidth.value)
104104
})
105105
}
106106

src/modules/Wave.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type { IllestWaveformProps } from '../types/waveform'
22

33
export default class Wave {
4-
protected canvasCtx!: CanvasRenderingContext2D
4+
private canvasCtx!: CanvasRenderingContext2D
55

66
constructor(
7-
protected canvas: HTMLCanvasElement,
8-
protected props: IllestWaveformProps,
9-
protected filteredData: number[][]
7+
private canvas: HTMLCanvasElement,
8+
private props: IllestWaveformProps,
9+
private filteredData: number[][]
1010
) {
1111
this.canvas = canvas
1212
this.canvasCtx = this.canvas?.getContext('2d') as CanvasRenderingContext2D
@@ -32,7 +32,7 @@ export default class Wave {
3232
this.drawCanvasLines()
3333
}
3434

35-
protected setCanvasBase(): void {
35+
private setCanvasBase(): void {
3636
this.canvas.width = this.canvas.offsetWidth
3737
this.canvas.height = this.canvas.offsetHeight
3838
this.canvas.style.opacity = '1'
@@ -64,17 +64,24 @@ export default class Wave {
6464
}
6565

6666
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)
67+
const { canvas, canvasCtx, props } = this
68+
canvasCtx.globalCompositeOperation = 'destination-atop'
69+
canvasCtx.fillStyle = props.maskColor as string
70+
canvasCtx.fillRect(0, 0, maskWidth, canvas.height)
7071
}
7172

72-
public setCanvasStyle(maskWidth: number): void {
73-
this.canvasCtx.clearRect(0, 0, this.canvas.width, this.canvas.height)
73+
private drawWave(): void {
74+
const { canvasCtx, props } = this
75+
canvasCtx.lineWidth = props.lineWidth as number
76+
canvasCtx.lineCap = props.lineCap as CanvasLineCap
77+
canvasCtx.strokeStyle = props.lineColor as string
78+
canvasCtx.stroke()
79+
}
80+
81+
public setWaveStyle(maskWidth: number): void {
82+
const { canvas, canvasCtx } = this
83+
canvasCtx.clearRect(0, 0, canvas.width, canvas.height)
7484
this.drawMask(maskWidth)
75-
this.canvasCtx.lineWidth = this.props.lineWidth as number
76-
this.canvasCtx.lineCap = this.props.lineCap as CanvasLineCap
77-
this.canvasCtx.strokeStyle = this.props.lineColor as string
78-
this.canvasCtx.stroke()
85+
this.drawWave()
7986
}
8087
}

0 commit comments

Comments
 (0)