EN
JavaScript - how to set line width (thickness / size) drawing on canvas element?
2 points
Simple solution:
xxxxxxxxxx
1
context.lineWidth = 20; // 20px
2
3
// line from x1=10, y1=20 to x2=200, y2=100
4
context.beginPath();
5
context.moveTo(10, 20);
6
context.lineTo(200, 100);
7
context.stroke();
Using JavaScript it is possible to draw line 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.lineWidth = 20;
20
21
context.beginPath();
22
context.moveTo(10, 50);
23
context.lineTo(170, 150);
24
context.stroke();
25
}
26
27
drawLine();
28
29
</script>
30
</body>
31
</html>