I'm trying to make a slider in javascript with canvas. The moveSlider() function is called by 'pointermove' eventlistener, and it changes the X coordinate of a rectangle stored in 'Slider' object in 200 pixel range.
I want it to be done in 11 steps, and cant seems to figure out how.
Also for some reason, if I add value of 'delta' to Slider.x the slider moves the opposite direction of the mouse, why?
any help is appreciated.
var startPos = e.clientX
function moveSlider(e) {
let x = ctx.canvas.width / 2
let delta = startPos - e.clientX;
startPos = e.clientX;
Slider.x -= delta;
Slider.x = Slider.x > x+100 ? x+100 : Slider.x;
Slider.x = Slider.x < x-100 ? x-100 : Slider.x;
}
Slider.x -= delta;try adding it:Slider.x += delta;let delta = startPos - e.clientX