日期:2014-05-16  浏览次数:20513 次

请教:canvas的画图问题
能否用一个stroke()函数,画出两种颜色的直线?貌似stroke函数只对最后一个strokeStyle设置的颜色起作用?多谢各位了

------解决方案--------------------
<!DOCTYPE html> <html>
<head>
<script type="text/javascript">
function draw()
{
var canvas = document.getElementById("MyCanvas");
  if (canvas.getContext) {
  var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.strokeStyle="green"; // Use strokeStyle to set the color.
ctx.rect (5,5,300,250); 
ctx.stroke();
ctx.beginPath();
ctx.strokeStyle="blue"; // Use strokeStyle to change the color.
ctx.lineWidth = "5";
ctx.moveTo(100,100);
ctx.lineTo(130,100);
ctx.moveTo(170,100);
ctx.lineTo(200,100);
ctx.stroke();
ctx.beginPath();
ctx.strokeStyle="red"; // Use strokeStyle to change the color.
ctx.arc(150,125,100,0,Math.PI, false);
ctx.stroke();
}
}
</script>
</head>
<body onload="draw();">
<canvas id="MyCanvas" width="600" height="600"> </canvas> 
</body>
</html>
参考