Skip to content

Commit 485bbc6

Browse files
committed
added logarithmic scale to flat slider
1 parent d4bcb76 commit 485bbc6

4 files changed

Lines changed: 60 additions & 7 deletions

File tree

ui/src/app/components/options/options.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878

7979
<!-- Flat Slider -->
8080
<eqm-flat-slider *ngIf="option.type === 'flat-slider'" [enabled]="!option.isEnabled || option.isEnabled() === true"
81-
[orientation]="option.orientation"
81+
[orientation]="option.orientation || 'horizontal'" [scale]="option.scale || 'linear'"
8282
[(value)]="option.value" [min]="option.min" [middle]="option.middle" [max]="option.max"
8383
[doubleClickToAnimateToMiddle]="option.doubleClickToAnimateToMiddle" [showMiddleNotch]="option.showMiddleNotch"
8484
[animationDuration]="option.animationDuration" [animationFps]="option.animationFps"

ui/src/app/components/options/options.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,15 @@ interface SliderOption extends BaseOptions {
9090
max?: number
9191
animationDuration?: number
9292
animationFps?: number
93-
scrollEnabled?: number
93+
scrollEnabled?: boolean
9494
stickToMiddle?: boolean
9595
stickedToMiddle?: () => any
9696
changed?: (value: number) => any
9797
}
9898

9999
export interface FlatSliderOption extends SliderOption {
100100
type: 'flat-slider'
101+
scale?: 'linear' | 'logarithmic'
101102
orientation?: 'vertical' | 'horizontal'
102103
showMiddleNotch?: boolean
103104
doubleClickToAnimateToMiddle?: boolean

ui/src/app/modules/eqmac-components/components/flat-slider/flat-slider.component.ts

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export class FlatSliderComponent {
3333
public sanitizer: DomSanitizer
3434
) {}
3535

36+
@Input() scale: 'logarithmic' | 'linear' = 'linear'
3637
@Input() doubleClickToAnimateToMiddle = true
3738
@Input() showMiddleNotch = true
3839
@Input() min: number = 0
@@ -96,7 +97,13 @@ export class FlatSliderComponent {
9697
if (diffFromMiddle < 0) {
9798
diffFromMiddle *= -1
9899
}
99-
const percFromMiddle = this.utils.mapValue(diffFromMiddle, 0, this.max - middleValue, 0, 100)
100+
const percFromMiddle = this.mapValue({
101+
value: diffFromMiddle,
102+
inMin: 0,
103+
inMax: this.max - middleValue,
104+
outMin: 0,
105+
outMax: 100
106+
})
100107
if ((this._value).toFixed(2) === (middleValue).toFixed(2) && percFromMiddle < 5) {
101108
value = middleValue
102109
} else if ((this._value < middleValue && newValue > this._value) || (this._value > middleValue && newValue < this._value)) {
@@ -139,7 +146,16 @@ export class FlatSliderComponent {
139146
public getValueFromMouseEvent (event: MouseEvent) {
140147
const coords = this.utils.getCoordinatesInsideElementFromEvent(event, this.elem.nativeElement)
141148
const progress = this.orientation === 'vertical' ? coords.y : coords.x
142-
const value = this.utils.mapValue(progress, this.knobRadius, (this.orientation === 'vertical' ? this.height : this.width) - this.knobRadius, this.min, this.max)
149+
const value = (() => {
150+
const inMin = this.knobRadius
151+
const inMax = (this.orientation === 'vertical' ? this.height : this.width) - this.knobRadius
152+
return this.mapValue({
153+
value: progress,
154+
inMin, inMax,
155+
outMin: this.min,
156+
outMax: this.max
157+
})
158+
})()
143159
return value
144160
}
145161

@@ -211,7 +227,13 @@ export class FlatSliderComponent {
211227
}
212228

213229
get progress () {
214-
return this.utils.mapValue(this.value, this.min, this.max, 0, 1)
230+
return this.mapValue({
231+
value: this.value,
232+
inMin: this.min,
233+
inMax: this.max,
234+
outMin: 0,
235+
outMax: 1
236+
})
215237
}
216238

217239
get containerStyle () {
@@ -276,6 +298,15 @@ export class FlatSliderComponent {
276298
return style
277299
}
278300

301+
private mapValue ({ value, inMin, inMax, outMin, outMax }: {
302+
[param in 'value' | 'inMin' | 'inMax' | 'outMin' | 'outMax']: number
303+
}) {
304+
switch (this.scale) {
305+
case 'linear': return this.utils.mapValue(value, inMin, inMax, outMin, outMax)
306+
case 'logarithmic': return this.utils.logMapValue({ value, inMin, inMax, outMin, outMax })
307+
}
308+
}
309+
279310
get thumbStyle () {
280311
const style: { [style: string]: number | string } = {}
281312
style.width = `${this.knobRadius * 2}px`
@@ -285,9 +316,21 @@ export class FlatSliderComponent {
285316

286317
style.borderRadius = '100%'
287318
if (this.orientation === 'horizontal') {
288-
style.left = `${this.utils.mapValue(this.value, this.min, this.max, 0, this.width - this.knobRadius * 2)}px`
319+
style.left = `${this.mapValue({
320+
value: this.value,
321+
inMin: this.min,
322+
inMax: this.max,
323+
outMin: 0,
324+
outMax: this.width - this.knobRadius * 2
325+
})}px`
289326
} else {
290-
style.bottom = `${this.utils.mapValue(this.value, this.min, this.max, 0, this.height - this.knobRadius * 2)}px`
327+
style.bottom = `${this.mapValue({
328+
value: this.value,
329+
inMin: this.min,
330+
inMax: this.max,
331+
outMin: 0,
332+
outMax: this.height - this.knobRadius * 2
333+
})}px`
291334
}
292335
return style
293336
}

ui/src/app/modules/eqmac-components/services/utilities.service.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ export class UtilitiesService {
88
return (value - inMin) * (outMax - outMin) / (inMax - inMin) + outMin
99
}
1010

11+
logMapValue ({ value, inMin, inMax, outMin, outMax }: {
12+
value: number, inMin: number, inMax: number, outMin: number, outMax: number
13+
}) {
14+
outMin = Math.log(outMin)
15+
outMax = Math.log(outMax)
16+
const scale = (outMax - outMin) / (inMax - inMin)
17+
return Math.exp(outMin + scale * (value - inMin))
18+
}
19+
1120
getImageFromSrcWhenLoaded (src) {
1221
return new Promise<HTMLImageElement>((resolve, reject) => {
1322
const image = new Image()

0 commit comments

Comments
 (0)