canvas手绘正五边形

2019-11-26 11:09:22

参考地址 canvas手绘正五边形

一、基本用法:

alertShow(param: any, $index: any) {

        let $log = this.$log;

        let $scope = this.$scope;

        if (param !== $scope.lastObj) {

            param.show = true;

            if ($scope.lastObj !== null) {

                $scope.lastObj.show = false;

            }

            $scope.lastObj = param;

            $scope.radarAlert = true;

            if ($index === 0) {

                $(".radarChart_alert").css({left: $scope.arr[$index].x + 100 + "px", top: $scope.arr[$index].y + "px"});

            } else {

                $(".radarChart_alert").css({left: $scope.arr[$index].x, top: $scope.arr[$index].y + 40 + "px"});

            }

        }

    }

drawRadarChart() {

    $scope.radarChartCategroy = [

            {name1: "词汇短语", name2: "排名前34%", show: false},

            {name1: "口语运用", name2: "排名前34%", show: false},

            {name1: "听力理解", name2: "排名前34%", show: false},

            {name1: "阅读理解", name2: "排名前34%", show: false},

            {name1: "语法句法", name2: "排名前34%", show: false}

        ];

        let $scope = this.$scope;

        let width: any, long: any, samll: any, big: any;

        getWidth();

        function getWidth() {

            width = $(".nav").width();

            $(".radarChart_div").height(width);

            $(".nav").height(width);

            $("#radarChart").attr({width: width, height: width});

            long = width / 2;

            samll = (width / 2) * Math.tan(36 * Math.PI / 180);

            big = (width / 2) / Math.cos(36 * Math.PI / 180);

            window.requestAnimationFrame(draw);

            let radarBtnWidth = $(".radarChart_select").width();

            let radarBtnHeight = $(".radarChart_select").height();

            $scope.arr = [

                {x: long - radarBtnWidth / 2, y: 5 - radarBtnHeight - 15},

                {x: -radarBtnWidth - 5, y: samll + 5 - radarBtnHeight / 2},

                {x: big * Math.sin(18 * Math.PI / 180) - radarBtnWidth * 2 / 3, y: big * Math.cos(18 * Math.PI / 180) + samll + radarBtnHeight / 2},

                {x: big * Math.sin(18 * Math.PI / 180) + big - radarBtnWidth / 3, y: big * Math.cos(18 * Math.PI / 180) + samll + radarBtnHeight / 2},

                {x: 2 * long + 5, y: samll + 5 - radarBtnHeight / 2}

            ]

            for (let i = 0; i < $scope.arr.length; i++) {

                $(".radarChart_select").eq(i).css({left: $scope.arr[i].x + "px", top: $scope.arr[i].y + "px"});

            }

        }

        function draw() {

            let canvas = document.getElementById("radarChart");

            if (canvas.getContext) {

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

                ctx.clearRect(0, 0, width, width);

                ctx.beginPath();

                ctx.lineWidth = 1;

                ctx.lineJoin = "miter";

                ctx.strokeStyle = "rgba(255,255,255,.5)";

                ctx.moveTo(long, 5);

                ctx.lineTo(0, samll + 5);

                ctx.lineTo(big * Math.sin(18 * Math.PI / 180), big * Math.cos(18 * Math.PI / 180) + samll + 5);

                ctx.lineTo(big * Math.sin(18 * Math.PI / 180) + big, big * Math.cos(18 * Math.PI / 180) + samll + 5);

                ctx.lineTo(2 * long, samll + 5);

                ctx.lineTo(long, 5);

                ctx.moveTo(long, long / Math.sin(72 * Math.PI / 180));

                ctx.lineTo(long, 5);

                ctx.moveTo(long, long / Math.sin(72 * Math.PI / 180));

                ctx.lineTo(0, samll + 5);

                ctx.moveTo(long, long / Math.sin(72 * Math.PI / 180));

                ctx.lineTo(big * Math.sin(18 * Math.PI / 180), big * Math.cos(18 * Math.PI / 180) + samll + 5);

                ctx.moveTo(long, long / Math.sin(72 * Math.PI / 180));

                ctx.lineTo(big * Math.sin(18 * Math.PI / 180) + big, big * Math.cos(18 * Math.PI / 180) + samll + 5);

                ctx.moveTo(long, long / Math.sin(72 * Math.PI / 180));

                ctx.lineTo(2 * long, samll + 5);

                ctx.stroke();

                ctx.save();

                ctx.beginPath();

                ctx.fillStyle = "rgba(255,255,255,.5)";

                ctx.moveTo(long, long / Math.sin(72 * Math.PI / 180) - $scope.num + 5);

                ctx.lineTo(long - $scope.num * Math.sin(72 * Math.PI / 180), long / Math.sin(72 * Math.PI / 180) - $scope.num * Math.cos(72 * Math.PI / 180) + 5);

                ctx.lineTo(long - $scope.num * Math.sin(36 * Math.PI / 180), long / Math.sin(72 * Math.PI / 180) + $scope.num * Math.cos(36 * Math.PI / 180) + 5);

                ctx.lineTo(long + $scope.num * Math.sin(36 * Math.PI / 180), long / Math.sin(72 * Math.PI / 180) + $scope.num * Math.cos(36 * Math.PI / 180) + 5);

                ctx.lineTo(long + $scope.num * Math.sin(72 * Math.PI / 180), long / Math.sin(72 * Math.PI / 180) - $scope.num * Math.cos(72 * Math.PI / 180) + 5);

//                ctx.lineTo(long, long / Math.sin(72 * Math.PI / 180) - $scope.num + 5);

                ctx.fill();

                ctx.save();

                ctx.restore();

                if ($scope.time2 < 60) {

                    window.requestAnimationFrame(draw);

                    $scope.num = $scope.num + long / 60;

                    $scope.time2 = $scope.time2 + 1;

                }

            }

        }

    }

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

            <div>

                <div class="radarChart_div"></div>

                <div class="nav">

                    <canvas id="radarChart"></canvas>

                    <div class="radarChart_select" ng-repeat="item in radarChartCategroy" ng-click="events.alertShow(item, $index)"

                         ng-class="{'radarChart_select_true':item.show,'radarChart_select_flase':!item.show}">

                        <div>{{item.name1}}</div>

                        <div>{{item.name2}}</div>

                    </div>

                    <div class="radarChart_alert" ng-style="radarAlert ? {'display': 'flex'} : {'display': 'none'}">

                        <div>213道</div>

                        <div>72%</div>

                        <div>正确答题</div>

                        <div>正确率</div>

                    </div>

                </div>

                <div class="radarChart_div"></div>              

            </div>



  • 2019-11-26 11:08:02

    多边型无序点排序(地图绘制多边形)

    任务需求要做一个区域高亮的功能,用到地图,想到了高德地图的多边形API,但是多边形顶点的顺序是要有序的,需求是无序,在API查找无果的情况下,只能手动实现点集合排序。

  • 2019-11-26 11:11:59

    正多边形的编程绘制(javascript)

    如何用程序来绘制正多边形? 在一般情况下,会使用 x = radius * Math.cos(angle), y = radius * Math.sin(angle) 来进行绘制,但这是关于x轴对称的,如果遇到正多边形的边数为奇数,而你又希望它是以y轴对称时,可按照下面的方法。

  • 2019-11-26 13:36:28

    Vue组件命名找不到的问题以及如何给vue组件命名

    首先,Vue 会将 template 中的内容插到 DOM 中,以方便解析标签。由于 HTML 标签不区分大小写,所以在生成的标签名都会转换为小写。例如,当你的 template 为 <MyComponent></MyComponent> 时,插入 DOM 后会被转换为 <mycomponent></mycomponent>。 然后,通过标签名寻找对应的自定义组件。匹配的优先顺序从高到低为:原标签名、camelCase化的标签名、PascalCase化的标签名。例如 <my-component>会依次匹配 my-component、myComponent、MyComponent。camelCase 和 PascalCase 的代码

  • 2019-11-28 11:00:35

    Vue子组件调用父组件的方法

    下面有三种方法,我自己重点推荐第一种,毕竟这种简单粗暴好用好理解,不过这个有一个弊端,再组件嵌套组件的时候,尤其是用第三方组件里面调用自己的子组件的时候,其实已经是孙子组件了,这个时候就要parent.parent。。。。,这样就不好了,我们就得考虑其他方法了,具体怎么判断是父组件,还是爷爷组件,我会单独出一篇文章讲述。

  • 2019-11-29 13:04:47

    计算一个多边形的重心点坐标(准确版)

    在之前的 《如何判断一个多边形是否合法》 一文中有提到,用无人机规划飞行路线前,往往需要框选一个多边形的区域。 而在地图控件上显示这个多边形区域时,往往会遇到这样一个需求:需要把所要测绘的多边形区域移动到地图中心。 实现这个需求的基本思路就是:获取到多边形区域的重心点坐标,然后利用地图控件的 setCenter方法,就可以把地图的显示中心移动到多边形区域重心了。那么问题来了,如何求出一个多边形的重心点坐标呢?

  • 2019-11-29 13:06:27

    如何判断一个多边形是否合法

    利用无人机对一片区域进行测绘前,我们会先在地图上框选一个区域,然后再规划飞行的路线,而需要测绘的这片区域往往是一个多边形。在 MeshKit 中,我们加入了多边形区域的编辑功能,其中就涉及判断用户所编辑出来的多边形是否合法的问题。