EN
JavaScript - how to set line width (thickness / size) drawing on canvas element?
2
points
Simple solution:
context.lineWidth = 20; // 20px
// line from x1=10, y1=20 to x2=200, y2=100
context.beginPath();
context.moveTo(10, 20);
context.lineTo(200, 100);
context.stroke();
Using JavaScript it is possible to draw line in following way.
1. lineTo
method example
// ONLINE-RUNNER:browser;
<!doctype html>
<html>
<head>
<style>
#my-canvas { border: 1px solid gray; }
</style>
</head>
<body>
<canvas id="my-canvas" width="200" height="200"></canvas>
<script>
var canvas = document.querySelector('#my-canvas');
var context = canvas.getContext('2d');
function drawLine() {
context.lineWidth = 20;
context.beginPath();
context.moveTo(10, 50);
context.lineTo(170, 150);
context.stroke();
}
drawLine();
</script>
</body>
</html>