Skip to content

add logic which enables setting starting position at any place on the circle #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: refactoring
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
:side="300"
:circleWidth="20"
:knobRadius="20"
:stepSize="10"
:startPosition="300"
>
</circle-slider>
<div class="value"> {{ sliderValue }} </div>
Expand Down
52 changes: 41 additions & 11 deletions src/components/CircleSlider.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,19 @@ export default {
mounted () {
this.containerElement = this.$refs._svg
this.sliderTolerance = this.radius / 2
this.setNewPosition({x: 0, y: 0})

this.setInitialPosition()
},
props: {
startPosition: {
type: Number,
required: false,
default: 90 // degrees
},
startAngleOffset: {
type: Number,
required: false,
default: function () {
// return Math.PI / 20
return 0
}
},
Expand Down Expand Up @@ -140,7 +145,8 @@ export default {
length: 0,
sliderTolerance: 0,
relativeX: 0,
relativeY: 0
relativeY: 0,
redundantAngle: 0
}
},
computed: {
Expand All @@ -156,13 +162,13 @@ export default {
return this.side / 2
},
cpAngle () {
return this.angle + Math.PI / 2
return this.angle + this.cpStartPositionRadians
},
cpMainCircleStrokeWidth () {
return this.circleWidth || (this.side / 2) / this.circleWidthRel
},
cpPathDirection () {
return (this.cpAngle < 3 / 2 * Math.PI) ? 0 : 1
return ((this.angle + Math.PI / 2) < 3 / 2 * Math.PI) ? 0 : 1
},
cpPathX () {
return this.cpCenter + this.radius * Math.cos(this.cpAngle)
Expand All @@ -176,10 +182,16 @@ export default {
cpKnobRadius () {
return this.knobRadius || (this.side / 2) / this.knobRadiusRel
},
cpKnobStartX () {
return this.cpCenter + this.radius * Math.cos(this.cpStartPositionRadians)
},
cpKnobStartY () {
return this.cpCenter + this.radius * Math.sin(this.cpStartPositionRadians)
},
cpPathD () {
let parts = []
parts.push('M' + this.cpCenter)
parts.push(this.cpCenter + this.radius)
parts.push('M' + this.cpKnobStartX)
parts.push(this.cpKnobStartY)
parts.push('A')
parts.push(this.radius)
parts.push(this.radius)
Expand All @@ -203,11 +215,14 @@ export default {
return this.steps[this.currentStepIndex]
},
cpSliderAngle () {
return (Math.atan2(this.relativeY - this.cpCenter, this.relativeX - this.cpCenter) + Math.PI * 3 / 2) % (Math.PI * 2)
return (Math.atan2(this.relativeY - this.cpCenter, this.relativeX - this.cpCenter) + this.cpStartPositionRadians * 3 - this.redundantAngle + this.startAngleOffset) % (Math.PI * 2)
},
cpIsTouchWithinSliderRange () {
const touchOffset = Math.sqrt(Math.pow(Math.abs(this.relativeX - this.cpCenter), 2) + Math.pow(Math.abs(this.relativeY - this.cpCenter), 2))
return Math.abs(touchOffset - this.radius) <= this.sliderTolerance
},
cpStartPositionRadians () {
return this.startPosition / 180 * Math.PI
}
},
methods: {
Expand Down Expand Up @@ -276,7 +291,6 @@ export default {

this.angle = this.cpAngleValue
this.currentStepValue = stepValue
// this.$emit('input', this.currentStepValue)
this.animateSlider(previousAngle, this.angle)
},
updateSlider () {
Expand Down Expand Up @@ -325,8 +339,24 @@ export default {
},
setNewPosition (e) {
const dimensions = this.containerElement.getBoundingClientRect()
this.relativeX = e.clientX - dimensions.left
this.relativeY = e.clientY - dimensions.top
this.relativeX = ( e.clientX || e.x ) - dimensions.left
this.relativeY = ( e.clientY || e.y ) - dimensions.top

this.calculateRedundantAngle()
},
calculateRedundantAngle () {
const totalAngle = Math.atan2(this.relativeY - this.cpCenter, this.relativeX - this.cpCenter) + this.cpStartPositionRadians * 3

if ((this.cpStartPositionRadians !== Math.PI / 2) && !this.redundantAngle) {
this.redundantAngle = totalAngle - (Math.PI * 2)
}
},
setInitialPosition () {
const dimensions = this.containerElement.getBoundingClientRect()
const x = (this.cpPathX + dimensions.left).toFixed(0)
const y = (this.cpPathY + dimensions.top).toFixed(0)

this.setNewPosition({x, y})
}
},
watch: {
Expand Down