zoukankan      html  css  js  c++  java
  • H5实现绘制三角形并在其三个顶点画三个圆添加点击事件

    1.效果如下:

     代码如下:

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title></title>
        </head>
        <body>
            <canvas id="canvas"></canvas>
        </body>
        <script type="text/javascript">
            //获取canvas容器
            var can = document.getElementById('canvas');
            //创建一个画布
            var ctx = can.getContext('2d');
            //绘制三角形(x1,x2)、(x2,y2) (x3,y3)表示三角形三个点坐标,color表示颜色,type表示绘制类型(填充fill和不填充stroke)
            var draw = function(x1, y1, x2, y2, x3, y3, color, type) {
                ctx.beginPath();
                ctx.moveTo(x1, y1);
                ctx.lineTo(x2, y2);
                ctx.lineTo(x3, y3);
                ctx[type + 'Style'] = color;
                ctx.closePath();
                ctx[type]();
            }
            draw(100,100,175,20,280,100,'green','stroke')
            //绘制圆形(x,y)圆心坐标,r表示半径,start表示起始角度,end表示结束角度,color表示颜色,type表示绘制类型(填充fill和不填充stroke)
            var draw = function(x, y, r, start, end, color, type) {
                var unit = Math.PI / 180;
                ctx.beginPath();
                ctx.arc(x, y, r, start * unit, end * unit);
                ctx[type + 'Style'] = color;
                ctx.closePath();
                ctx[type]();
            }
            draw(175,20,20,0,360,'yellow','fill')
            
            var draw = function(x, y, r, start, end, color, type) {
                var unit = Math.PI / 180;
                ctx.beginPath();
                ctx.arc(x, y, r, start * unit, end * unit);
                ctx[type + 'Style'] = color;
                ctx.closePath();
                ctx[type]();
            }
            draw(100,100,20,0,360,'yellow','fill')
            
            var draw = function(x, y, r, start, end, color, type) {
                var unit = Math.PI / 180;
                ctx.beginPath();
                ctx.arc(x, y, r, start * unit, end * unit);
                ctx[type + 'Style'] = color;
                ctx.closePath();
                ctx[type]();
            }
            draw(280,100,20,0,360,'yellow','fill')
            
            can.onclick=function(e){
                //获取鼠标位置
                var x=e.clientX-can.offsetLeft;
                var y=e.clientY-can.offsetTop;
                //通过圆心判断鼠标是否在圆的范围内
                if(x>=80&&x<=120&&y>=80&&y<=120)
                {
                    window.open('https://www.baidu.com');
                }
            }
        </script>
    </html>

    2.补充:用border画三角形,其效果如下:

     代码如下:

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
        <style>
            .circle {
                width: 50px;
                height: 50px;
                border-radius: 150px;
                position: absolute;
                left: 230px;
                top:80px;
                background: yellow;
            }
    
            .circle1 {
                width: 50px;
                height: 50px;
                border-radius: 150px;
                position: absolute;
                left: 180px;
                top:220px;
                background: yellow;
            }
    
            .circle2 {
                width: 50px;
                height: 50px;
                border-radius: 150px;
                position: absolute;
                left: 380px;
                top:220px;
                background: yellow;
                
            }
    
            .triangle {
                width: 0;
                height: 0;
                border-bottom: 150px solid red;
                border-right: 150px solid transparent;
                border-left: 50px solid transparent;
                margin-left: 200px;
                margin-top: 100px;
            }
        </style>
        <body>
            <div class="triangle">
            <div class="circle"></div>
            <div class="circle1"></div>
            <div class="circle2"></div>
            </div>
        </body>
    </html>
  • 相关阅读:
    WPF 程序 处理未捕获异常,和程序莫名终止说拜拜
    CSS块级元素和行内元素
    Memcache安全配置
    ASP.NET MVC3默认提供了11种ActionResult的实现
    css position: absolute、relative详解
    用Redis实现Session功能
    编写 WPF DataGrid 列模板,实现更好的用户体验
    CSS3去除手机浏览器button点击出现的高亮框
    OpenCV 视频处理框架
    DataGridView绑定数据源
  • 原文地址:https://www.cnblogs.com/Hero-Bin/p/11865892.html
Copyright © 2011-2022 走看看