【Canvas】线性渐变色例子-垂直方向渐变

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>线性渐变色例子-垂直方向渐变</title>
    </head>

     <body onload="draw()">
        <canvas id="myCanvus" width="400px" height="400px" style="border:1px dashed black;">
            出现文字表示你的浏览器不支持HTML5
        </canvas>
     </body>
</html>
<script type="text/javascript">
<!--

function draw(){
    var canvas=document.getElementById('myCanvus');    
    canvas.width=400;
    canvas.height=400;    

    var ctx=canvas.getContext('2d');   
    ctx.translate(200,200);

    // 从上到下垂直渐变,注意之前有平移
    var linegrad=ctx.createLinearGradient(0,-200,0,200);
    linegrad.addColorStop(0,"white");// 白色
    linegrad.addColorStop(1,"navy");// 海蓝

    ctx.fillStyle=linegrad;
    ctx.fillRect(-200,-200,400,400);
};

//-->
</script>
复制代码

效果:

END

posted @ 2022-01-09 09:14  不朽的飞翔  阅读(135)  评论(0编辑  收藏  举报