Form
context.addColorStop(offset,color)
Parameters
| parameter |
description |
| offset |
specifies a position (offset) for the gradient |
| color |
specifies a color for the gradient |
Explanation
The addColorStop specifies a color and a offset for the gradient. Specify not a coordinate but a float value for the offset. The 0.5 offset is the center of the entire gradient. The 0 offset is the start of the gradient while the 1 offset is the end of the gradient.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=shift_jis">
<title>Sample</title>
<script type="text/javascript"><!--
function drawPoint()
{
canvasObj = document.getElementById("imgCanvas");
conObj = canvasObj.getContext("2d");
gradObj = conObj.createLinearGradient(0,0,320,240);
gradObj.addColorStop(0.3, "#ff0000");
gradObj.addColorStop(0.6, "#0000ff");
conObj.fillStyle = gradObj;
conObj.rect(10,10,300,220);
conObj.fill();
}
// --></script>
</head>
<body onload="drawPoint()">
<h1>Sample</h1>
<canvas id="imgCanvas" width="320" height="240" style="border:1px black solid"></canvas>
</body>
</html>