我有一个带颜色选择器的界面,允许用户改变颜色,但是发生了什么,我开始绘制 - 它绘制黑色。我将笔画改为红色,我画 - 而黑色的一切现在都是红色的。我没有丝毫的线索,为什么。
这是我认为相关的代码。它都在一个coffeescript类中
canvasMouseDown: (event) ->
@offset = $(@canvas).offset()
_x = @offset.left
_y = @offset.top
@ctx.moveTo event.pageX - _x, event.pageY - _y
# does nothing more than save the current canvas via getImageData() to allow an undo
@saveUndoState()
@ctx.strokeStyle = @attr.stroke
@canvas.bind "mousemove.canvasDraw", (e) =>
x = e.pageX - _x
y = e.pageY - _y
@ctx.lineTo x, y
@ctx.stroke()
initCanvas: ->
@ctx = @canvas[0].getContext '2d'
$(@canvas).bind "mousedown.canvasDraw", (e) =>
@canvasMouseDown e
$(@canvas).bind "mouseup.canvasDraw", (e) =>
@canvasMouseUp e
答案 0 :(得分:0)
布拉赫。忘了在mousedown上包含ctx.beginPath,在mouseup上包含ctx.closePath