Created
March 4, 2020 15:05
-
-
Save mfazekas/69e866772624ac16cfc17afbb5700ff6 to your computer and use it in GitHub Desktop.
AnimatedLineString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class AnimatedLineString2 extends AnimatedWithChildren { | |
constructor(line) { | |
super(); | |
this.coordinates = line.coordinates.map(coord => [...coord]); | |
} | |
// eslint-disable-next-line class-methods-use-this | |
_onAnimationDone(animation, doneCB) { | |
return new Proxy(animation, { | |
get(obj, prop) { | |
if (prop === 'start') { | |
return function start(origDoneCB, ...args) { | |
return obj.start((startParams, ...startArgs) => { | |
doneCB(startParams); | |
if (origDoneCB) { | |
origDoneCB(startParams, ...startArgs); | |
} | |
}, ...args); | |
}; | |
} | |
return obj[prop]; | |
}, | |
}); | |
} | |
_last(items) { | |
return items.length > 0 ? items[items.length - 1] : null; | |
} | |
_setup(coordinates) { | |
const last = this._last(this.coordinates); | |
let i = this.coordinates.length; | |
while (i < coordinates.length) { | |
this.coordinates.push([...last]); | |
i++; | |
} | |
} | |
_cleanup(coordinates) { | |
while (this.coordinates.length > coordinates.length) { | |
this.coordinates.pop(); | |
} | |
super.__callListeners(this.__getValue()); | |
} | |
animate(type, config) { | |
const {coordinates, ...rest} = config; | |
const last = this._last(coordinates); | |
this._setup(coordinates); | |
this.value = new Animated.Value(0.0); | |
this.newCoords = coordinates.map(coord => [...coord]); | |
this.animation = Animated[type](this.value, {...rest, toValue: 1.0}); | |
this.value.__addChild(this); | |
return this.animation; | |
} | |
timing(config = {coordinates: DEFAULT_COORD}) { | |
return this.animate('timing', config); | |
} | |
__getValue() { | |
if (!this.animation) { | |
return { | |
type: 'LineString', | |
coordinates: this.coordinates.map(coord => [coord[0], coord[1]]), | |
}; | |
} | |
const newF = this.value.__getValue(); | |
const origF = 1.0 - newF; | |
return { | |
type: 'LineString', | |
coordinates: this.coordinates.map((coord, i) => { | |
if (this.newCoords.length > i) { | |
const newCoord = this.newCoords[i]; | |
return [ | |
coord[0] * origF + newCoord[0] * newF, | |
coord[1] * origF + newCoord[1] * newF, | |
]; | |
} | |
return [coord[0], coord[1]]; | |
}), | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment