Skip to content

Commit a252997

Browse files
committed
improve: reduce graffiti points and optimize drawing performance
1 parent 327fdae commit a252997

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

src/editor/core/draw/graffiti/Graffiti.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ export class Graffiti {
3131
this.isDrawing = true
3232
// 缓存起始数据
3333
this.startStroke = {
34-
isBegin: true,
35-
lineWidth: this.options.graffiti.defaultLineWidth,
3634
lineColor: this.options.graffiti.defaultLineColor,
37-
linePoints: [evt.offsetX, evt.offsetY]
35+
lineWidth: this.options.graffiti.defaultLineWidth,
36+
points: [evt.offsetX, evt.offsetY]
3837
}
3938
}
4039

@@ -49,8 +48,8 @@ export class Graffiti {
4948
const DISTANCE = 2
5049
if (
5150
this.startStroke &&
52-
Math.abs(this.startStroke.linePoints[0] - offsetX) < DISTANCE &&
53-
Math.abs(this.startStroke.linePoints[1] - offsetY) < DISTANCE
51+
Math.abs(this.startStroke.points[0] - offsetX) < DISTANCE &&
52+
Math.abs(this.startStroke.points[1] - offsetY) < DISTANCE
5453
) {
5554
return
5655
}
@@ -70,9 +69,9 @@ export class Graffiti {
7069
this.startStroke = null
7170
}
7271
if (!currentValue?.strokes?.length) return
73-
currentValue.strokes.push({
74-
linePoints: [offsetX, offsetY]
75-
})
72+
const lastPoints =
73+
currentValue.strokes[currentValue.strokes.length - 1].points
74+
lastPoints.push(offsetX, offsetY)
7675
// 重新渲染
7776
this.draw.render({
7877
isCompute: false,
@@ -110,14 +109,12 @@ export class Graffiti {
110109
ctx.save()
111110
for (let s = 0; s < strokes.length; s++) {
112111
const stroke = strokes[s]
113-
const { isBegin, lineColor, lineWidth, linePoints } = stroke
114-
if (isBegin) {
115-
ctx.strokeStyle = lineColor || defaultLineColor
116-
ctx.lineWidth = (lineWidth || defaultLineWidth) * scale
117-
ctx.beginPath()
118-
ctx.moveTo(linePoints[0] * scale, linePoints[1] * scale)
119-
} else {
120-
ctx.lineTo(linePoints[0] * scale, linePoints[1] * scale)
112+
ctx.beginPath()
113+
ctx.strokeStyle = stroke.lineColor || defaultLineColor
114+
ctx.lineWidth = (stroke.lineWidth || defaultLineWidth) * scale
115+
ctx.moveTo(stroke.points[0] * scale, stroke.points[1] * scale)
116+
for (let p = 2; p < stroke.points.length; p += 2) {
117+
ctx.lineTo(stroke.points[p] * scale, stroke.points[p + 1] * scale)
121118
}
122119
ctx.stroke()
123120
}

src/editor/interface/Graffiti.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
export interface IGraffitiStroke {
2-
isBegin?: boolean
32
lineWidth?: number
43
lineColor?: string
5-
linePoints: [x: number, y: number]
4+
points: number[]
65
}
76

87
export interface IGraffitiData {

0 commit comments

Comments
 (0)