zoukankan      html  css  js  c++  java
  • canvas绘制一个圆分成六等分颜色随机

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            canvas {
                border: 1px solid #ccc;
            }
        </style>
    </head>
    <body>
    <canvas width="600" height="400"></canvas>
    <script>
        var myCanvas = document.querySelector('canvas');
        var ctx = myCanvas.getContext('2d');
        var w = ctx.canvas.width;
        var h = ctx.canvas.height;
        /*分成几等分*/
        var num = 360;
        /*一份多少弧度*/
        var angle = Math.PI * 2 / num;
        /*原点坐标*/
        var x0 = w / 2;
        var y0 = h / 2;
        /*获取随机颜色*/
        var getRandomColor = function () {
            var r = Math.floor(Math.random() * 256);
            var g = Math.floor(Math.random() * 256);
            var b = Math.floor(Math.random() * 256);
            return 'rgb(' + r + ',' + g + ',' + b + ')';
        }
        /*上一次绘制的结束弧度等于当前次的起始弧度*/
        //var startAngle = 0;
        for (var i = 0; i < num; i++) {
            var startAngle = i * angle;
            var endAngle = (i + 1) * angle;
            ctx.beginPath();
            ctx.moveTo(x0, y0);
            ctx.arc(x0, y0, 150, startAngle, endAngle);
            /*随机颜色*/
            ctx.fillStyle = getRandomColor();
            ctx.fill();
        }
    </script>
    </body>
    </html>

    运行结果如下:

    别废话,拿你代码给我看。
  • 相关阅读:
    gradle 转 maven
    java Multimap
    java 写法推荐
    Python虚拟环境virtualenv
    C# 转换图形为PCX 格式
    微软宣布.NET开发环境将开源 支持Mac OS X和Linux
    写给在Java和.net中徘徊的新手
    HTML5 vs FLASH vs SILVERLIGHT
    我的NHibernate曲折之行
    NHibernate 3 Beginner's Guide
  • 原文地址:https://www.cnblogs.com/lvxueyang/p/13707447.html
Copyright © 2011-2022 走看看