如何用Canvas画一个美国队长的盾牌?

如何用Canvas画一个最简单的美队盾牌呢?
做起来非常简单,找到五角星五个点相对于圆心的位置就可以啦!
效果图和完整代码如下:

效果图:

下载.png

my code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>曾小小晨的页面</title>
    <style>
        canvas {
            background-color: #CACACA;
        }
    </style>
</head>
<body>
<canvas id="cvs" width="600" height="600"></canvas>

<script>
    var cvs = document.getElementById('cvs');
    var ctx = cvs.getContext('2d');
    var w = cvs.width,
        h = cvs.height;

    var x = w / 2,
        y = h / 2,
        start = -Math.PI / 2,
        end = Math.PI * 3 / 2;

    ctx.beginPath();
    ctx.arc(x, y, 250, start, end);
    ctx.fillStyle = '#882446';
    ctx.fill();

    ctx.beginPath();
    ctx.arc(x, y, 210, start, end);
    ctx.fillStyle = '#989CA0';
    ctx.fill();

    ctx.beginPath();
    ctx.arc(x, y, 170, start, end);
    ctx.fillStyle = '#882446';
    ctx.fill();

    ctx.beginPath();
    ctx.arc(x, y, 130, start, end);
    ctx.fillStyle = '#1A306D';
    ctx.fill();

    r = 128;

    ctx.beginPath();
    ctx.moveTo(x, y - r);
    // 顶点连下左
    ctx.lineTo(x - r * Math.sin(Math.PI / 5), y + r * Math.cos(Math.PI / 5));
    // 下左连上右
    ctx.lineTo(x + r * Math.cos(Math.PI / 10), y - r * Math.sin(Math.PI / 10));
    // 上右连上左
    ctx.lineTo(x - r * Math.cos(Math.PI / 10), y - r * Math.sin(Math.PI / 10));
    // 上左连下右
    ctx.lineTo(x + r * Math.sin(Math.PI / 5), y + r * Math.cos(Math.PI / 5));
    ctx.lineWidth = 2;
    ctx.stroke();
    ctx.fillStyle = '#989CA0';
    ctx.fill();


</script>
</body>
</html>


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,804评论 25 709
  • 一:canvas简介 1.1什么是canvas? ①:canvas是HTML5提供的一种新标签 ②:HTML5 ...
    GreenHand1阅读 4,731评论 2 32
  • 本文首发于我的个人博客:http://cherryblog.site/github项目地址:https://git...
    sunshine小小倩阅读 2,031评论 1 8
  • 20171102星期四 天气晴 说实话,今天的天气有点热,可能是穿多了吧。别脱哈,听说明天降温,要注意保暖哈 ...
    璇戎爸爸阅读 126评论 0 0
  • 好话讲到这里就算 好爱侣也无法登对 情与爱像极一场梦 醒觉后便再难自控 从前只怪命运太作弄 怕得到后更令人疲累 不...
    苟且偷生脱苦海阅读 278评论 0 0