首页 > 代码库 > canvas基础教学之线性渐变

canvas基础教学之线性渐变

<!DOCTYPE html><html>    <head>        <meta charset="UTF-8">        <title>color</title>        <style type="text/css">            *{                margin: 0;                padding: 0;            }            #canvas{                background-color: #666666;                /*opacity: 0.2;*/            }        </style>    </head>    <body>        <canvas id="canvas" width="600" height="300">Canvas not supported</canvas>        <script type="text/javascript">            var canvas=document.getElementById(canvas);                context=canvas.getContext(2d);            var    gradient=context.createLinearGradient(0,0,canvas.width,30);                gradient.addColorStop(0,red);            gradient.addColorStop(0.25,blue);            gradient.addColorStop(0.5,black);            gradient.addColorStop(0.75,yellow);            gradient.addColorStop(1,green);            context.fillStyle=gradient;            context.fillRect(0,0,600,300);        </script>    </body></html>

实际效果

技术分享

canvas基础教学之线性渐变