Skip to content

Commit 4ecf6d9

Browse files
committed
Calculate auto tick separation without loop
1 parent 61ede03 commit 4ecf6d9

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
@@ -240,30 +240,23 @@ export class MdSlider implements AfterContentInit {
240240
}
241241

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

265-
// Once a suitable separation for the ticks is found, draw them on the slider.
266-
this._renderer.drawTicks(tickSeparation);
257+
// The pixel value of the tick is the percentage * the width of the slider. Use this to draw
258+
// the ticks on the slider.
259+
this._renderer.drawTicks(width * tickPercentage);
267260
}
268261

269262
/**

0 commit comments

Comments
 (0)