Drawing to canvas like in paint

前端 未结 3 1575
小蘑菇
小蘑菇 2021-01-27 02:41

I have three arrys:

 clickX = [],
    clickY = [],
    clickDrag = [];

this is what happens when you click down:

$(\'#canvasCur         


        
3条回答
  •  隐瞒了意图╮
    2021-01-27 03:29

    Here’s how to use canvas to draw like Paint

    If you want an undo feature, your best option is to record all line segments drawn by the user.

    This is done with a point array that contains all points (polylines) drawn by the user.

    To track the brush size and brush color, you need to include this info in your array also.

    So each element of the the array will have this info about each line segment:

    • x: the ending x coordinate of this line segment
    • y: the ending y coordinate
    • size: the brush size (lineWidth)
    • color: the brush color (strokeStyle)
    • mode: “begin” indicates the beginning of a new line, “end” indicates the end of this line.

    How does it work?

    When the user is drag-drawing a line segment, each mousemove event is extending the current segment with context.lineTo and context.stroke.

    When the user selects a new BrushSize or BrushColor, context.beginPath starts context.beginPath.

    When the user holds down the Undo button, the last point in the last line segment is popped off the point array. Then all the surviving line segments are redrawn. The undo button fires every 1/10th of a second when held down.

    Here is code and a Fiddle: http://jsfiddle.net/m1erickson/AEYYq/

    
    
    
     
    
    
    
    
    
    
    
    
    
    
        

    Drag to draw. Use buttons to change lineWidth/color




提交回复
热议问题