试图编写一些代码,它会做以下事情。用线性渐变填充画布
用户将通过点击选择一种颜色,画布元素将填充该颜色作为渐变以及固定的白色color.code似乎不起作用。
<html>
<head>
<style>
#chart{width:80px;height:80px;position:fixed;top:20px;left:20px;}
#mycanvas{width:200px;height:150px;position:fixed;top:250px;left:200px;border:1px solid black;}
</style>
</head>
<body>
<script>
function mygradient(colors){
var canvas=document.getElementById('mycanvas');
var ctx=canvas.getContext("2d");
var grad=ctx.createLinearGradient(0,0,190,0);
grad.addColorStop(0,colors);
grad.addColorStop(1,"white");
ctx.fillStyle=grad;
ctx.fillRect(0,0,200,0);
}
</script>
<table id="chart">
<tr>
<td bgColor="#FF8000" onClick="mygradient(this.bgColor);"></td>
<td bgColor="#FFBF00" onClick="mygradient(this.bgColor);"></td>
</tr>
</table>
<canvas id="mycanvas"style=""></canvas>
</body>
</html>
如果你提到我在我的代码中犯的错误,那将会很棒? –
0的高度不显示任何内容:ctx.fillRect(0,0,200,0);它应该是ctx.fillRect(0,0,200,150); –