Skip to content

Commit

Permalink
fix: make animate a free function
Browse files Browse the repository at this point in the history
moved !this.minimap {return} check to step
  • Loading branch information
aminya committed Dec 29, 2020
1 parent 4005109 commit cfb681b
Showing 1 changed file with 40 additions and 46 deletions.
86 changes: 40 additions & 46 deletions lib/minimap-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -1095,13 +1095,17 @@ class MinimapElement {
const minimapTo = Math.min(1, scrollTop / (this.minimap.getTextEditorMaxScrollTop() || 1)) * this.minimap.getMaxScrollTop()

step = (now, t) => {
if (this.minimap === null) return // TODO why this happens in the tests?
this.minimap.setTextEditorScrollTop(now, true)
this.minimap.setScrollTop(minimapFrom + (minimapTo - minimapFrom) * t)
}
this.animate({ from: from, to: to, duration: duration, step: step })
animate({ from: from, to: to, duration: duration, step: step })
} else {
step = (now) => this.minimap.setTextEditorScrollTop(now)
this.animate({ from: from, to: to, duration: duration, step: step })
step = (now) => {
if (this.minimap === null) return // TODO why this happens in the tests?
this.minimap.setTextEditorScrollTop(now)
}
animate({ from: from, to: to, duration: duration, step: step })
}
} else {
this.minimap.setTextEditorScrollTop(textEditorScrollTop)
Expand Down Expand Up @@ -1269,49 +1273,6 @@ class MinimapElement {
if (!this.minimap) { return }
this.dragSubscription.dispose()
}

// ###### ###### ######
// ## ## ## ## ## ##
// ## ## ##
// ## ###### ######
// ## ## ##
// ## ## ## ## ## ##
// ###### ###### ######

/**
* A method that mimic the jQuery `animate` method and used to animate the
* scroll when clicking on the MinimapElement canvas.
*
* @param {Object} param the animation data object
* @param {[type]} param.from the start value
* @param {[type]} param.to the end value
* @param {[type]} param.duration the animation duration
* @param {[type]} param.step the easing function for the animation
* @access private
*/
animate ({ from, to, duration, step }) {
const start = getTime()
let progress

const update = () => {
if (!this.minimap) { return }

const passed = getTime() - start
if (duration === 0) {
progress = 1
} else {
progress = passed / duration
}
if (progress > 1) { progress = 1 }
const delta = swing(progress)
const value = from + (to - from) * delta
step(value, delta)

if (progress < 1) { requestAnimationFrame(update) }
}

update()
}
}

const minimapElement = MinimapElement.initClass()
Expand Down Expand Up @@ -1377,6 +1338,39 @@ function makeScale (x = 0, y = x, useHardwareAcceleration = false) {
}
}

/**
* A method that mimic the jQuery `animate` method and used to animate the
* scroll when clicking on the MinimapElement canvas.
*
* @param {Object} param the animation data object
* @param {[type]} param.from the start value
* @param {[type]} param.to the end value
* @param {[type]} param.duration the animation duration
* @param {[type]} param.step the easing function for the animation
* @access private
*/
function animate ({ from, to, duration, step }) {
const start = getTime()
let progress

const update = () => {
const passed = getTime() - start
if (duration === 0) {
progress = 1
} else {
progress = passed / duration
}
if (progress > 1) { progress = 1 }
const delta = swing(progress)
const value = from + (to - from) * delta
step(value, delta)

if (progress < 1) { requestAnimationFrame(update) }
}

update()
}

function swing (progress) {
return 0.5 - Math.cos(progress * Math.PI) / 2
}
Expand Down

0 comments on commit cfb681b

Please sign in to comment.