• 【一天一个canvas】绘制渐变线条(二)


    如何使用 <canvas> 标记绘图

    大多数 Canvas 绘图 API 都没有定义在 <canvas> 元素本身上,而是定义在通过画布的 getContext() 方法获得的一个“绘图环境”对象上。

    Canvas API 也使用了路径的表示法。但是,路径由一系列的方法调用来定义,而不是描述为字母和数字的字符串,比如调用 beginPath() 和 arc() 方法。

    渐变线条就是颜色有渐变的效果,当然渐变的样式可以遵循路径的方向也可以不遵循路径的方向:

    <!doctype html>
    <html>
        <head>
            <meta charset="UTF-8">
        </head>
        <style type="text/css">
            canvas{border:dashed 2px #CCC}
        </style>
        <script type="text/javascript">
            function $$(id){
                return document.getElementById(id);
            }
            function pageLoad(){
                var can = $$('can');
                var cans = can.getContext('2d');
                cans.moveTo(0,0);
                cans.lineTo(400,300);
                var gnt1 = cans.createLinearGradient(0,0,400,300);//线性渐变的起止坐标
                gnt1.addColorStop(0,'red');//创建渐变的开始颜色,0表示偏移量,个人理解为直线上的相对位置,最大为1,一个渐变中可以写任意个渐变颜色
                gnt1.addColorStop(1,'yellow');
                cans.lineWidth=3;
                cans.strokeStyle = gnt1;
                cans.stroke();
            }
        </script>
        <body onload="pageLoad();">
            <canvas id="can" width="400px" height="300px">4</canvas>
        </body>
    </html>
  • 相关阅读:
    5.搜索-dfs、回溯、bfs
    4.排序算法
    3.二分查找
    2.双指针
    1.贪心算法
    【目录】leetcode刷题
    深度学习的优化与正则化
    什么是深度学习
    循环神经网络
    Failed to execute 'index' on 'IDBObjectStore': The specified index was not found.
  • 原文地址:https://www.cnblogs.com/babysay123/p/4617975.html
Copyright © 2020-2023  润新知