画画&放大同一个画布

时间:2015-07-24 19:30:52

标签: javascript jquery html css canvas

亲爱的Stackoverflow用户,

我正在制定一个网络议程,其工作方式就像人们可以绘制的巨型画布(例如,您可以在酒吧找到的那种议程)

我是javascript的新手,但我管理了一些代码

  • 放大和缩小画布
  • 画在画布上

我试图使用jquery库(zoomooz),它允许你通过点击它来放大div,但它会弄乱我的代码的绘图部分。 (基本上你正在绘制的内容和鼠标指针不同步)

然后我发现另一段代码直接放大画布(没有jquery库)..所以我试图将绘图脚本与该脚本合并,但它没有工作,正如你所期望的那样这样的拼贴画。

有人可以建议一种方法来解决我的问题与jquery库或如何使用画布与绘图和缩放脚本。或者也许是使用javascript画布提高技能的文档.. 我希望我足够清楚。 一旦这个问题得到解决,我就会开始考虑项目的后端部分......



div {
	border:1px solid black;
	width:1000px;
	height:152px;
	margin: none;
	font-size: 0px;

}


div div {
	width:150px;
	height:150px;
	background: #aaa;
	display: inline-block;
	margin: none;
	font-size: 20px;
}

div div div {
	width:50px;
	height:50px;
	display: inline-block;
	margin: none;
	position: absolute;
	z-index: 1;
}

canvas{ position: absolute; z-index: 0 }

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>Hierarchy test</title>
    <link rel="stylesheet" href="test7.css" type="text/css" media="screen" />

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="jquery.zoomooz.min.js"></script>

</head>


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

    var x = "black",
    y = 2;

    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 color(obj) {
        switch (obj.id) {
            case "green":
            x = "green";
            break;
            case "blue":
            x = "blue";
            break;
            case "red":
            x = "red";
            break;
            case "yellow":
            x = "yellow";
            break;
            case "orange":
            x = "orange";
            break;
            case "black":
            x = "black";
            break;
            case "white":
            x = "white";
            break;
        }
        if (x == "white") y = 14;
        else y = 2;

    }

    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 save() {
        document.getElementById("canvasimg").style.border = "2px solid";
        var dataURL = canvas.toDataURL();
        document.getElementById("canvasimg").src = dataURL;
        document.getElementById("canvasimg").style.display = "absolute";
        document.getElementById("canvasimg").style.top = "10px";
        document.getElementById("canvasimg").style.left = "5px";
        document.getElementById("canvasimg").style.zIndex = "-1";

    }

    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();
            }
        }
    }
</script>


<body class="container" onload="init()">

    <canvas id="can" width="1000" height="300" style="position:absolute;top:5;left:5;border:2px solid;"></canvas>

    <div id="w1" class="week">
        <div id="d1" class="day">
            <div id="bd1" class="bouton"></div>
        </div>
        <div id="d2" class="day">
            <div id="bd2" class="bouton"></div>
        </div>
        <div id="d3" class="day">
            <div id="bd3" class="bouton"></div>
        </div>
    </div>



    <div style="position:absolute;top:12%;left:43%;">Choose Color</div>
    <div style="position:absolute;top:15%;left:45%;width:10px;height:10px;background:green;" id="green" onclick="color(this)"></div>
    <div style="position:absolute;top:15%;left:46%;width:10px;height:10px;background:blue;" id="blue" onclick="color(this)"></div>
    <div style="position:absolute;top:15%;left:47%;width:10px;height:10px;background:red;" id="red" onclick="color(this)"></div>
    <div style="position:absolute;top:17%;left:45%;width:10px;height:10px;background:yellow;" id="yellow" onclick="color(this)"></div>
    <div style="position:absolute;top:17%;left:46%;width:10px;height:10px;background:orange;" id="orange" onclick="color(this)"></div>
    <div style="position:absolute;top:17%;left:47%;width:10px;height:10px;background:black;" id="black" onclick="color(this)"></div>
    <div style="position:absolute;top:20%;left:43%;">Eraser</div>
    <div style="position:absolute;top:22%;left:45%;width:15px;height:15px;background:white;border:2px solid;" id="white" onclick="color(this)"></div>
    <img id="canvasimg" style="position:absolute;top:10%;left:52%;" style="display:none;">
    <input type="button" value="save" id="btn" size="30" onclick="save()" style="position:absolute;top:55%;left:10%;">
    <input type="button" value="clear" id="clr" size="23" onclick="erase()" style="position:absolute;top:55%;left:15%;">


    <script>
        $(document).ready(function() {
            $("#bd1").click(function(evt) {
                $("#d1").zoomTo({targetsize:0.75, duration:600});
                evt.stopPropagation();
            });
            $("#bd2").click(function(evt) {
                $("#d2").zoomTo({targetsize:0.75, duration:600});
                evt.stopPropagation();
            });
            $("#bd3").click(function(evt) {
                $("#d3").zoomTo({targetsize:0.75, duration:600});
                evt.stopPropagation();
            });
        });
    </script>


</body> 
&#13;
&#13;
&#13;

0 个答案:

没有答案