Skip to content

Commit 480b2cc

Browse files
committed
Calculate auto tick separation without loop
1 parent fe07376 commit 480b2cc

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

src/components/slider/slider.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -243,30 +243,23 @@ export class MdSlider implements AfterContentInit {
243243
}
244244

245245
/**
246-
* Calculates the separation in pixels of tick marks by starting with the assumption every step
247-
* needs a tick and eliminating the number of ticks until there is a distance of at least 30px
248-
* between each tick.
246+
* Calculates the optimal separation in pixels of tick marks based on the minimum auto tick
247+
* separation constant.
249248
*/
250249
private _updateAutoTickSeparation() {
251-
// The pixel value for how far apart the ticks should be.
252-
let tickSeparation = 0;
253-
// Keeps track of how many steps to multiply the slider's step by.
254-
let stepCounter = 1;
255-
256-
while (tickSeparation < MIN_AUTO_TICK_SEPARATION) {
257-
// Multiplying the counter and step together determines which step we want to use. This starts
258-
// at the first step and moves to second, then third, etc. until we find a good distance.
259-
let tickValue = (this.step * stepCounter) + this.min;
260-
261-
// The percentage of the step on the slider is needed in order to calculate the pixel offset
262-
// from the beginning of the slider. This offset is the tick separation.
263-
let tickPercentage = this.calculatePercentage(tickValue);
264-
tickSeparation = this._sliderDimensions.width * tickPercentage;
265-
stepCounter++;
266-
}
250+
let width = this._sliderDimensions.width;
251+
252+
// This calculates which step is far enough away from the beginning of the slider to draw a tick
253+
// at. This value will then be used to draw ticks at every tickStep steps.
254+
let tickStep =
255+
Math.ceil(MIN_AUTO_TICK_SEPARATION * (this.max - this.min) / (width * this.step));
256+
257+
// The percentage of the slider where the first tick should go.
258+
let tickPercentage = this.calculatePercentage((this.step * tickStep) + this.min);
267259

268-
// Once a suitable separation for the ticks is found, draw them on the slider.
269-
this._renderer.drawTicks(tickSeparation);
260+
// The pixel value of the tick is the percentage * the width of the slider. Use this to draw
261+
// the ticks on the slider.
262+
this._renderer.drawTicks(width * tickPercentage);
270263
}
271264

272265
/**

0 commit comments

Comments
 (0)