要设置HTML5 Canvas线条的颜色,我们可以使用canvas上下文的strokeStyle属性,该属性可以设置为颜色字符串,如红色,绿色或蓝色,十六进制值,如#FF0000或#555,或者 RGB值,例如rgb(255,0,0)。
html代码:
<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="578" height="200"></canvas>
<script>
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
context.beginPath();
context.moveTo(100, 150);
context.lineTo(450, 50);
context.lineWidth = 10;
// set line color
context.strokeStyle = '#ff0000';
context.stroke();
</script>
</body>
</html>