Simple Vue.js library to apply Javascript tweening. Based on jaxgeller/tweezer.js.
- Vue => 1.x - 2.x
- and maybe
requestAnimationFrame
polyfill (for module)
npm install vue-tween --save
Note: All codes here will be written in ES6. But that doesn't mean any version or type of Javascript not applicable.
Then install it,
import Vue from 'vue'
import VueTween from 'vue-tween'
Vue.use(VueTween)
There are couple options you can fill in within installation.
name
: Change the name of the directive.default
: Change default value for animationstart
,end
andduration
time.easing
: Include easing globally.
import Vue from 'vue'
import VueTween from 'vue-tween'
Vue.use(VueTween, {
name: 'animate', //default: tween
default: {
start: -500, //default: 0
end: 500, //default: 1000
duration: 2000 //default: 1000
},
easing: { // default easing is only easeInOut which is provided by tweezer.js
easeOutQuad (t, b, c, d) {
return -c * (t /= d)*(t - 2) + b;
}
}
})
Dependency and requestAnimationFrame
polyfill already bundled in CDN script. It will also perform an auto-install on Vue.
<script src="https://cdn.rawgit.com/chay22/vue-tween/master/vue-tween.min.js"></script>
<!-- or -->
<script src="https://unpkg.com/[email protected]/vue-tween.min.js"></script>
<template>
<div v-tween:click.capture="myTweening">
<span :style="style"></span>
</div>
</template>
<div v-tween:click.capture="myTweening">
This could be renamed during installation
<div v-tween:click.capture="myTweening">
DOM event. For Vue 1.x, it is based on built-in on
directive.
<div v-tween:click.capture="myTweening">
Available modifiers for Vue 1.x are on the docs. For Vue 2.x capture
is the only valid modifier.
<div v-tween:click.capture="myTweening">
The handler must return an object containing the following:
-
start
integer | default: 0Start of tween.
-
end
integer | default: 1000End of tween.
-
duration
integer | default: 1000Duration of tween.
-
easing
function/string | default: easeInOutLocal component easing function. This can be a function that return an integer or a string specifying a name of globally registered easing during installation.
-
tick
function | requiredA function handler used to do animation. There are two parameters passed to this function, first is
value
which indicates current value betweenstart
toend
. Each value is updated within 16ms throughrequestAnimationFrame
. And the second parameter isevent handler
which passed by DOMaddEventListener
and can be used to do, i.ee.preventDefault()
(to complement the lack of modifier for Vue 2.x). -
done
functionA function handler that fired when tweening has reached the
end
value. Parameters passed are all the same astick
handler.
Consider to consult to jaxgeller/tweezer.js directly for more information and real example.
<template>
<div v-tween:click.capture="myTweening">
<span :style="style"></span>
</div>
</template>
<script>
export default {
data () {
return {
style: {
position: 'absolute',
left: ''
}
}
},
methods () {
myTweening () {
return {
start: 0,
end: 1000,
duration: 1000,
tick (v, e) {
e.preventDefault()
this.style.left = `${v / 10}px`,
this.style.opacity = `${(v / -1000) + 1}`
},
done (v, e) {
this.style.opacity = '0'
}
}
}
}
}
</script>
Copyright (c) 2016 Cahyadi Nugraha
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.