首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >清除底层画布

清除底层画布
EN

Stack Overflow用户
提问于 2013-06-16 02:21:31
回答 1查看 182关注 0票数 0

我有3层,一层用于bg,一层用于主画布,一层用于光标。这是一个绘画程序,我希望光标是画笔,为此,我重新绘制基于鼠标移动,并清除以前的状态与清除rect。当我用鼠标按下时,它会在主画布上绘制,你可以这样做。然而,当我运行程序时,clear rect似乎会影响底层,使它们变得透明?为什么背景层不能保持白色?

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

听众:

代码语言:javascript
复制
$("#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();
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-16 02:59:44

我看了一眼,找到了问题所在。在Paint.js的第19行,您可以看到:

代码语言:javascript
复制
var ctxCursor = canvasBg.getContext('2d');

这实际上应该是:

代码语言:javascript
复制
var ctxCursor = canvasCursor.getContext('2d');

还有其他一些问题,所以我清理了它们,现在它工作得很好:http://jsfiddle.net/VmvqJ/2/

这是您的新Paint.js脚本:

代码语言:javascript
复制
//////////////////// 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>标记之间添加以下内容:

代码语言:javascript
复制
<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>标记和其中的所有内容:

代码语言:javascript
复制
<body>
    <canvas id="canvasCursor" width="1000px" height="600px"></canvas>
    <canvas id="canvas" width="1000px" height="600px"></canvas>
</body>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17126612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档