我有一个带有颜色选择器的界面,允许用户更改笔触颜色,但发生的事情是,我开始绘制 - 它绘制黑色。我将笔画更改为红色,然后开始绘画——所有黑色的东西现在都变成了红色。我一点也不知道为什么。
这是我认为相关的代码。这一切都在一个咖啡脚本类中
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