码迷,mamicode.com
首页 > 其他好文 > 详细

Canvas 画板

时间:2015-07-24 14:13:36      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

Canvas 画板

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>canvas draw</title>
</head>

<body>
    <canvas id=‘myCanvas‘ width=‘800‘ height=‘600‘>
        your browser does not support canvas
    </canvas>
    <script type="text/javascript">
    var c = document.getElementById(myCanvas);
    var ctx = c.getContext(2d);
    ctx.fillStyle = black;
    ctx.fillRect(0, 0, 800, 600);
    var onoff = false;
    var oldx = -10;
    var oldy = -10;
    var lineColor = white;
    var linw = 4;
    c.addEventListener(mousemove, draw, true);
    c.addEventListener(mousedown, down, false);
    c.addEventListener(mouseup, up, false);

    function down(event) {
        onoff = true;
        oldx = event.pageX - 10;
        oldy = event.pageY - 10;
    }

    function up() {
        onoff = false;
    }

    function draw(event) {
        if (onoff == true) {
            var newx = event.pageX - 10;
            var newy = event.pageY - 10;
            ctx.beginPath();
            ctx.moveTo(oldx, oldy);
            ctx.lineTo(newx, newy);
            ctx.strokeStyle = lineColor;
            ctx.lineWidth = linw;
            ctx.lineCap = round;
            ctx.stroke();
            oldx = newx;
            oldy = newy;
        }
    }
    </script>
</body>

</html>

 

Canvas 画板

标签:

原文地址:http://www.cnblogs.com/stono/p/4673326.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!