Path
In this chapter you will learn:
Drawing Using Paths
Name | Description | Returns |
---|---|---|
beginPath() | Begins a new path | void |
closePath() | Close a path by drawing a line from the end of the last line to the initial coordinates | void |
fill() | Fills the shape | void |
isPointInPath(x, y) | Returns true if a point is contained by the current path | boolean |
lineTo(x, y) | Draws a sub-path to the specified coordinates | void |
moveTo(x, y) | Moves to the specified coordinates without drawing | void |
rect(x, y, w, h) | Draws a rectangle whose top-left corners is at (x, y) with width w and height h. | void |
stroke() | Draws the outline of the shape | void |
Drawing Paths with Lines
<!DOCTYPE HTML><!-- ja v a 2 s . c o m-->
<html>
<head>
<style>
canvas {
border: thin solid black
}
body>* {
float: left;
}
</style>
</head>
<body>
<canvas id="canvas" width="500" height="140">
Your browser doesn't support the <code>canvas</code> element
</canvas>
<script>
var ctx = document.getElementById("canvas").getContext("2d");
ctx.fillStyle = "yellow";
ctx.strokeStyle = "black";
ctx.lineWidth = 4;
ctx.beginPath();
ctx.moveTo(10, 10);
ctx.lineTo(100, 10);
ctx.lineTo(100, 120);
ctx.closePath();
ctx.fill();
ctx.beginPath();
ctx.moveTo(150, 10);
ctx.lineTo(320, 10);
ctx.lineTo(20, 120);
ctx.lineTo(120, 120);
ctx.fill();
ctx.stroke();
ctx.beginPath();
ctx.moveTo(20, 10);
ctx.lineTo(220, 120);
ctx.stroke();
</script>
</body>
</html>
Next chapter...
What you will learn in the next chapter:
Home » Javascript Tutorial » Canvas