在div下渲染的画布而不是div

时间:2014-07-31 19:02:44

标签: javascript jquery html css canvas

在我的网站上,我希望在<canvas>之上呈现<div>,并在其中包含其他<div>。这一切都正常,但是,画布中的绘图是不可能的,因为它位于主<div>下面。您可以在下面的codepen中测试它。你只能在靠近底部的小条上画画。

Codepen:http://codepen.io/anon/pen/hfmBG

代码:

<html>
<head>
<script type="text/javascript">
var canvas, ctx, flag = false,
    prevX = 0,
    currX = 0,
    prevY = 0,
    currY = 0,
    dot_flag = false;

var x = "#000",
    y = 1;

function init() {
    canvas = document.getElementById('can');
    ctx = canvas.getContext("2d");
    w = canvas.width;
    h = canvas.height;

    canvas.addEventListener("mousemove", function (e) {
        findxy('move', e)
    }, false);
    canvas.addEventListener("mousedown", function (e) {
        findxy('down', e)
    }, false);
    canvas.addEventListener("mouseup", function (e) {
        findxy('up', e)
    }, false);
    canvas.addEventListener("mouseout", function (e) {
        findxy('out', e)
    }, false);
}


function draw() {
    ctx.beginPath();
    ctx.moveTo(prevX, prevY);
    ctx.lineTo(currX, currY);
    ctx.strokeStyle = x;
    ctx.lineWidth = y;
    ctx.stroke();
    ctx.closePath();
}

function erase() {
    var m = confirm("Want to clear");
    if (m) {
        ctx.clearRect(0, 0, w, h);
        document.getElementById("canvasimg").style.display = "none";
   }
}

function findxy(res, e) {
    if (res == 'down') {
        prevX = currX;
        prevY = currY;
        currX = e.clientX - canvas.offsetLeft;
        currY = e.clientY - canvas.offsetTop;

        flag = true;
        dot_flag = true;
        if (dot_flag) {
            ctx.beginPath();
            ctx.fillStyle = x;
            ctx.fillRect(currX, currY, 2, 2);
            ctx.closePath();
            dot_flag = false;
        }
    }
    if (res == 'up' || res == "out") {
        flag = false;
    }
    if (res == 'move') {
        if (flag) {
            prevX = currX;
            prevY = currY;
            currX = e.clientX - canvas.offsetLeft;
            currY = e.clientY - canvas.offsetTop;
            draw();
        }
    }
}

function print(){
    var c = document.getElementById("can");
    var ctx = c.getContext("2d");
    var imgData = ctx.getImageData(10,10,50,50);
    console.log(imgData);
}
</script>
<style>
    .patternlockbutton{
        border-color: red;
        background-color: transparent;
        background-repeat:no-repeat;
        display:block;
        width:33px;
        height:33px;
        float:left;
        margin:26px;
        z-index:1;
        -ms-touch-action: none;
        border-style: solid;
        border-width: 5px;
        -webkit-border-top-left-radius: 50px;
        -webkit-border-top-right-radius: 50px;
        -moz-border-radius-topleft: 50px;
        -moz-border-radius-topright: 50px;
        border-top-left-radius: 50px;
        border-top-right-radius: 50px;
        -webkit-border-bottom-left-radius: 50px;
        -webkit-border-bottom-right-radius: 50px;
        -moz-border-radius-bottomleft: 50px;
        -moz-border-radius-bottomright: 50px;
        border-bottom-left-radius: 50px;
        border-bottom-right-radius: 50px;
    }
    #can {
        z-index: 99;
        display: block;
    }
</style>
</head>
<body onload="init()">
    <div style="width:300px;height:400px;">
        <div class="patternlockbutton" id="patternlockbutton1"></div>
        <div class="patternlockbutton" id="patternlockbutton2"></div>
        <div class="patternlockbutton" id="patternlockbutton3"></div>
        <div class="patternlockbutton" id="patternlockbutton4"></div>
        <div class="patternlockbutton" id="patternlockbutton5"></div>
        <div class="patternlockbutton" id="patternlockbutton6"></div>
        <div class="patternlockbutton" id="patternlockbutton7"></div>
        <div class="patternlockbutton" id="patternlockbutton8"></div>
        <div class="patternlockbutton" id="patternlockbutton9"></div>
        <canvas id="can" width="300px" height="400px"></canvas>
    </div>
    <input type="button" value="clear" id="clr" size="23" onclick="erase()" style="position:absolute;top:55%;left:15%;">
    <button onclick="print()">Console.log</button>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

如果要将HTML元素放在其他元素之上,则应在CSS中设置其position: absolute

在您的情况下,画布的父div应该应用此样式:

position: relative;

画布应该应用此样式:

position: absolute;
top: 0;
left: 0;