当前位置:首页 > javascript > 正文内容

h5绘制图画

自由小鸟7年前 (2018-06-19)javascript3240

参考网站资料编写的绘图

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>cavans</title>
    <style>
        body{
            margin:0;
            padding:0;
            background: #f1f1f1;
        }
        .box{display: flex;justify-content: center;}
        #canvas{
            background: #EBE9BB;
            width:480px;
            height:480px;
        }
        .color{
            overflow: hidden;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 10px 0;
        }
        .color span{
            display: block;
            width: 40px;
            height: 40px;
            border-radius: 50%;
            margin-right: 10px;
            border:3px solid transparent;
        }
        .color .active{
            border: 3px solid darkmagenta;
        }
        .clear{text-align: center;}
        .clear button{
            padding: 7px 15px;
            background: #008000;
            border-radius: 5px;
            color: #fff;
            font-size: 5vw;
            border: 0;
        }
        #btn{
            background: #ff328e!important;
        }

         

    </style>
    <script src="https://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
    <!--画布-->
    <div class="box">
        <canvas id="canvas" width="480" height="480"></canvas>
    </div>
    <div class ="color">
        <span class="active"></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
    </div>
    <div class="clear">
        <button id="eraser">橡皮擦</button>
        <button id="btn">清除画布</button>
    </div>
    <script>
        $(function(){
            var x1,y1,x2,y2,a=10;
            //定义画板cavans
            var canvas = document.getElementById("canvas");
            var w=$(canvas).prop('width');
            var h=$(canvas).prop('height');

 

            var ctx = canvas.getContext("2d");

 

            //颜色初始化
            var colors="green";
            var color=["black","green","red","gray","blue","yellow"];
            for(var i=0;i<color.length;i++){
                $('.color span').eq(i).css('background',color[i])
            };

 

            //点击颜色选中
            $('.color span').on('click',function(){
                 $(this).siblings().removeClass("active");
                 $(this).addClass('active');
                 colors=$(this)[0].style.background;
                 console.log(colors);
                 a=10;
                tapClip();
            });

 

            //在画板上画画的函数
            function tapClip(){
                //笔触圆滑
                ctx.lineCap = "round";
                ctx.lineJoin = "round";
                ctx.lineWidth = a;
                ctx.strokeStyle=colors;

 

                canvas.addEventListener('mousedown' , function(e){
                    e.preventDefault();
                    x1 = e.clientX-canvas.offsetLeft;
                    y1 = e.clientY-canvas.offsetTop;
                    ctx.beginPath()
                    ctx.moveTo(x1,y1);
                    ctx.lineTo(x1+1,y1+1);  //考虑到点击时能画出一个点
                    ctx.stroke();

 

                    canvas.addEventListener('mousemove' , tapmoveHandler);
                    canvas.addEventListener('mouseup' , function(e){
                        canvas.removeEventListener('mousemove' , tapmoveHandler);
                    });
                });

  

                //移动
                function tapmoveHandler(e){
                    e.preventDefault();
                    x2 = e.clientX-canvas.offsetLeft;
                    y2 = e.clientY-canvas.offsetTop;
                    ctx.moveTo(x1,y1);
                    ctx.lineTo(x2,y2);
                    ctx.stroke();
                    x1=x2;
                    y1=y2;
                }
            }

             

            tapClip();

 

            //橡皮擦
            $("#eraser").click(function(){
                colors="#EBE9BB";
                a=30;
                tapClip();
            });

 

            //点击清除画布
            $("#btn").click(function(){
                ctx.clearRect(0,0,w,h);
            })

 

        });
    </script>
</body>
</html>



版权声明:本文由Web学习之路发布,如需转载请注明出处。

本文链接:https://www.webge.net/?id=8

“h5绘制图画” 的相关文章

HTTP协议类

HTTP协议类

HTTP协议的主要特点    1,简单快速,每个资源是固定的,通过固定url就可以了    2,灵活  head有数据类型,一个HTTP协议可以完成不同数据类型的传输    3,无连接&nb...

js原型链

js原型链

创建对象有几种方法:原型,构造函数,实例,原型链左边 实例对象  instanceof 构造函数  来判断是不是同一个引用原型用constructor 比instanceof来判断继承更严谨var o3=new M()o3 instanceof  M  &nb...

函数参数会生成新的作用域

函数参数会生成新的作用域

如果函数定义了参数就会自动生成新的作用域,这时候里面的变量值就会先从参数新的作用域找,如果没有就会再往上面找直到全局...

数组去重

var a=[1,3,4,5,6,1,3,9,6]; //代码是去重后的 function arrfn(data){ let newObj={}; for(let i=0;i<data.length;i++...

js 判断数组,对象

arr=[1,2,3,4]; obj={a:'aaa'}; console.log(arr.constructor===Object) //false arr instanceof Array //true Object.prototype.toString.call(...

SSO单点登陆

1、单点登录实现原理1)登录 注意:1)跳转SSO验证登录时,需要使用sendDirect重定向;2)浏览器与SSO之间建立的会话成为全局会话。扩展:a.spring项目如何获取本机的IP地址? 获得仅为ip地址不带端口b.spring项目如何获取项目端口? 2)注销 SSO认证中心有一个全...