Draw straight line on canvas with javascript

I want to draw straight line on HTML canvas using with javascript but when I research for how does it done every result abut the solition is using with static position

it’s one of the result when ı search

 const ctx = canvas.getContext('2d');
    ctx.beginPath();
    ctx.moveTo(100, 100);
    ctx.lineTo(300, 100);
    ctx.stroke();   

but its not work with dynamic position same way
does anybody know the solition of using with dynamic width and height