自定义大小调整工具在Canvas Drawing中不起作用

时间:2014-07-10 11:25:41

标签: javascript jquery html5 css3 canvas

这是我的fiddle

Canvas的HTML:

<div id="canvasDiv"><canvas id="myNewCanvasColumn" width="490" height="220"></canvas></div>

我正在使用画布绘图工具。问题是,我无法选择合适的绘图尺寸。我已经定义了像#34;小,正常,大和巨大的#34;。默认选择是正常的。我已经写了一些函数来确定半径,但它不起作用。有人能帮我吗?

非常感谢..!

1 个答案:

答案 0 :(得分:0)

您可以设置不同的context.lineWidth来设置光标大小。我用它更新了代码。

function redraw() {
    clickSize.length = 0;
    context.clearRect(0, 0, context.canvas.width, context.canvas.height);
    //context.strokeStyle = "#df4b26";
    context.lineJoin = "round";
    //context.lineWidth = 5;                        
    for (var i = 0; i < clickX.length; i++) {
        context.beginPath();
        if (clickDrag[i] && i) {
            context.moveTo(clickX[i - 1], clickY[i - 1]);
        } else {
            context.moveTo(clickX[i] - 1, clickY[i]);
        }
        context.lineTo(clickX[i], clickY[i]);
        context.closePath();
        context.strokeStyle = clickColor[i];
        context.lineWidth = cursorSize;
        //alert('radius is'+context.lineWidth)
        context.stroke();
    }
}
$('#choosesmall').click(function () {
    curSize = "small";
    cursorSize = 1;
});
$('#choosenormal').click(function () {
    curSize = "normal";
    cursorSize = 3;
});
$('#chooselarge').click(function () {
    curSize = "large";
    cursorSize = 5;
});
$('#choosehuge').click(function () {
    curSize = "huge";
    cursorSize = 7;
});

请参阅Fiddle

修改

$(document).ready(function () {

    /* Declaration global Variables */
    var clickX = new Array();
    var clickY = new Array();
    var clickDrag = new Array();
    var paint;
    var context;
    var canvas;
    var colorPurple = "#cb3594";
    var colorGreen = "#659b41";
    var colorYellow = "#ffcf33";
    var colorBrown = "#986928";
    var colorRed = "#ff0000";
    var curColor = colorPurple;
    var clickColor = new Array();
    var clickSize = new Array();
    var curSize = 1;
    /* End of Declaring Global Variables */

    context = document.getElementById('myNewCanvasColumn').getContext("2d");
    var canvasDiv = document.getElementById('canvasDiv');
    canvas = document.createElement('canvas');
    canvas.setAttribute('width', 700);
    canvas.setAttribute('height', 300);
    canvas.setAttribute('id', 'canvas');
    canvasDiv.appendChild(canvas);
    if (typeof G_vmlCanvasManager != 'undefined') {
        canvas = G_vmlCanvasManager.initElement(canvas);
    }
    var radius;
    var i = 0;
    context = canvas.getContext("2d");

    /* Event Handlers for drawing */
    $('#canvas').mousedown(function (e) {
        var mouseX = e.pageX - this.offsetLeft;
        var mouseY = e.pageY - this.offsetTop;
        paint = true;
        addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop);
        redraw();
    });
    $('#canvas').mousemove(function (e) {
        if (paint) {
            addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true);
            redraw();
        }
    });
    $('#canvas').mouseup(function (e) {
        paint = false;
    });
    $('#canvas').mouseleave(function (e) {
        paint = false;
    });
    $("#clear").click(function () {
        clearDrawing();
    });
    /* End of Event Handlers */


    /* Custom Color Pickers */
    $('#choosegreen').click(function () {
        curColor = "#659b41";
    });
    $('#choosepurple').click(function () {
        curColor = "#cb3594";
    });
    $('#chooseyellow').click(function () {
        curColor = "#ffcf33";
    });
    $('#choosebrown').click(function () {
        curColor = "#986928";
    });
    $('#choosered').click(function () {
        curColor = "#ff0000";
    });
    /* End of Custom Color Pickers */

    /* Custom Size Picker */
    $('#choosesmall').click(function () {
        curSize = 1;
    });
    $('#choosenormal').click(function () {
        curSize = 3;
    });
    $('#chooselarge').click(function () {
        curSize = 5;
    });
    $('#choosehuge').click(function () {
        curSize = 7;
    });
    /* End of Custom Size Picker */
    function addClick(x, y, dragging) {
        clickX.push(x);
        clickY.push(y);
        clickDrag.push(dragging);
        clickColor.push(curColor);
        clickSize.push(curSize);
        //        alert(clickSize);
    }

    function clearDrawing() {
        clickX.length = 0;
        clickY.length = 0;
        clickDrag.length = 0;
        context.clearRect(0, 0, context.canvas.width, context.canvas.height);
    }

    function redraw() {
        context.clearRect(0, 0, context.canvas.width, context.canvas.height);
        //context.strokeStyle = "#df4b26";
        context.lineJoin = "round";
        //context.lineWidth = 5;                        
        for (var i = 0; i < clickX.length; i++) {

            context.beginPath();

            if (clickDrag[i] && i) {
                context.moveTo(clickX[i - 1], clickY[i - 1]);
            } else {
                context.moveTo(clickX[i] - 1, clickY[i]);
            }
            context.lineTo(clickX[i], clickY[i]);
            context.closePath();
            context.strokeStyle = clickColor[i];
            context.lineWidth = clickSize[i];
            // alert(clickColor[i]);
            //alert('radius is'+context.lineWidth)
            context.stroke();
        }
    }
});

Updated Fiddle