清除底层画布

时间:2013-06-15 18:21:31

标签: javascript canvas

我有3层,一个用于bg,一个用于主画布,一个用于光标。 这是一个绘图程序,我希望光标是画笔,为此我根据鼠标移动重绘并清除以前的状态,清除rect。当我用鼠标单击它时,它将在主画布上绘制,你可以这样痛苦。但是,当我运行程序时,clear rect似乎会影响底层,使它们透明? 为什么背景层不会保持白色?

http://www.taffatech.com/Paint.html

听众:

$("#canvas").mousedown(function (e) {
    mouseX = e.pageX - this.offsetLeft;
    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;
});

$('#canvasCursor').mousemove(function (e) {

    ctxCursor.clearRect(0, 0, canvasWidth, canvasHeight);

    mouseX = e.pageX - this.offsetLeft;
    mouseY = e.pageY - this.offsetTop;

    ctxCursor.beginPath();
    ctxCursor.arc(mouseX, mouseY, brushSize, 0, 2 * Math.PI, false);
    ctxCursor.fillStyle = 'green';
    ctxCursor.fill();

});

window.onload = function () {
    ctxBg.beginPath();
    ctxBg.rect(0, 0, canvasWidth, canvasHeight);
    ctxBg.fillStyle = 'white';
    ctxBg.fill();
};

1 个答案:

答案 0 :(得分:0)

我看了一下并追查了问题。在Paint.js的第19行,你有这个:

var ctxCursor = canvasBg.getContext('2d');

这应该是:

var ctxCursor = canvasCursor.getContext('2d');

还有一些其他问题,所以我将它们清理干净,现在它已经完美运行了:http://jsfiddle.net/VmvqJ/2/

这是您的新Paint.js脚本:

//////////////////// Wayne Daly
///////////////////  03/06/2013

$(document).ready(function () {


    /////////////// SETTING UP CONTEXT & VARIABLES ///////////////
    var canvas = document.getElementById('canvas'),
        ctx = canvas.getContext('2d'),
        canvasWidth = canvas.width,
        canvasHeight = canvas.height;

    var canvasCursor = document.getElementById('canvasCursor'),
        ctxCursor = canvasCursor.getContext('2d');


    var mouseX,
        mouseY,
        clickX = [],
        clickY = [],
        clickDrag = [],
        paint = false,
        brushSize = 20;

    /////////////// EVENT HANDLERS ///////////////

    $('#canvasCursor').mousemove(function (e) {
        updateCursor(this, e);
        console.log(paint);
        if (paint) {
            addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true);
            redraw();
        }
    });

    $('#canvasCursor').mousedown(function (e) {
        console.log('down');
        mouseX = e.pageX - this.offsetLeft;
        mouseY = e.pageY - this.offsetTop;
        paint = true;
        addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop);
        redraw();
    });

    $('#canvasCursor').mouseup(function (e) {
        console.log('up');
        paint = false;
    });


    /////////////// GENERAL FUNCTIONS ///////////////

    function updateCursor(elem, e) {
        ctxCursor.clearRect(0, 0, canvasWidth, canvasHeight);

        mouseX = e.pageX - elem.offsetLeft;
        mouseY = e.pageY - elem.offsetTop;

        ctxCursor.beginPath();
        ctxCursor.arc(mouseX, mouseY, brushSize, 0, 2 * Math.PI, false);
        ctxCursor.fillStyle = 'green';
        ctxCursor.fill();
    }

    function addClick(x, y, dragging) {
        clickX.push(x);
        clickY.push(y);
        clickDrag.push(dragging);
    }


    function Paint(e) {

        mouseX = parseInt(e.clientX - offsetX);
        mouseY = parseInt(e.clientY - offsetY);

        ctx.beginPath();
        ctx.arc(mouseX, mouseY, brushSize, 0, 2 * Math.PI, false);
        ctx.fillStyle = 'green';
        ctx.fill();

    }

    function redraw() {
        ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); // Clears the canvas

        ctx.strokeStyle = "#df4b26";
        ctx.lineJoin = "round";
        ctx.lineWidth = 5;

        for (var i = 0; i < clickX.length; i++) {
            ctx.beginPath();
            if (clickDrag[i] && i) {
                ctx.moveTo(clickX[i - 1], clickY[i - 1]);
            } else {
                ctx.moveTo(clickX[i] - 1, clickY[i]);
            }
            ctx.lineTo(clickX[i], clickY[i]);
            ctx.closePath();
            ctx.stroke();
        }
    }

});

并在页面的<head>标记之间添加此内容:

<style type="text/css">
body {
    background:#303030;
}
#canvas, #canvasCursor {
    cursor: none;
    background: #fff;
    position: absolute;
    left: 50px;
    top: 30px;
    z-index: 1;
}
#canvasCursor {
    z-index: 20;
    background: none;
}
</style>

然后用以下内容替换您的<body>标记及其中的所有内容:

<body>
    <canvas id="canvasCursor" width="1000px" height="600px"></canvas>
    <canvas id="canvas" width="1000px" height="600px"></canvas>
</body>