EN
JavaScript - how to draw path (polygonal chain) on canvas element?
7 points
Simple solution:
xxxxxxxxxx
1
context.beginPath();
2
context.moveTo(20, 40);
3
context.lineTo(70, 150);
4
context.lineTo(110, 80);
5
context.lineTo(180, 170);
6
context.stroke();
Using JavaScript it is possible to draw path in following way.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
6
#my-canvas { border: 1px solid gray; }
7
8
</style>
9
</head>
10
<body>
11
<canvas id="my-canvas" width="200" height="200"></canvas>
12
<script>
13
14
var canvas = document.querySelector('#my-canvas');
15
var context = canvas.getContext('2d');
16
17
function drawLine() {
18
19
context.beginPath();
20
context.moveTo(20, 40);
21
context.lineTo(70, 150);
22
context.lineTo(110, 80);
23
context.lineTo(180, 170);
24
context.stroke();
25
}
26
27
drawLine();
28
29
</script>
30
</body>
31
</html>