【发布时间】:2020-05-18 16:32:05
【问题描述】:
我正在使用 JavaScript 画布 API 进行免费绘图。我坚持掩盖允许绘制的区域 - 在我的示例中,它应该只是语音气泡区域。 我正在使用这个 Vue 组件:https://github.com/sametaylak/vue-draw/blob/master/src/components/CanvasDraw.vue
draw(event) {
this.drawCursor(event);
if (!this.isDrawing) return;
if (this.tools[this.selectedToolIdx].name === 'Eraser') {
this.canvasContext.globalCompositeOperation = 'destination-out';
} else {
this.canvasContext.globalCompositeOperation = 'source-over';
this.canvasContext.strokeStyle = this.tools[this.selectedToolIdx].color;
}
this.canvasContext.beginPath();
this.canvasContext.moveTo(this.lastX, this.lastY);
this.canvasContext.lineTo(event.offsetX, event.offsetY);
this.canvasContext.stroke();
[this.lastX, this.lastY] = [event.offsetX, event.offsetY];
},
drawCursor(event) {
this.cursorContext.beginPath();
this.cursorContext.ellipse(
event.offsetX, event.offsetY,
this.brushSize, this.brushSize,
Math.PI / 4, 0, 2 * Math.PI
);
this.cursorContext.stroke();
setTimeout(() => {
this.cursorContext.clearRect(0, 0, this.width, this.height);
}, 100);
},
【问题讨论】:
-
你能不检查
event.offsetX, event.offsetY与气泡的边界吗? -
不管怎样,如果你提供一个可运行的代码sn-p,你更有可能得到一个好的答案。
-
@avejidah 如前所述,我正在使用这个 Vue 组件:github.com/sametaylak/vue-draw
-
如果您能用预期的结果和可重现的代码更好地描述您的问题,我们或许可以提供帮助
-
您在画布中是否只有一张图像? (粉红色背景和对话泡泡)或者它们是独立的对象?
标签: javascript vue.js canvas