vue-tween v1.0.0
Vue Tween
Simple Vue.js library to apply Javascript tweening. Based on jaxgeller/tweezer.js.
Demo
Requirements
- Vue => 1.x - 2.x
- and maybe
requestAnimationFrame
polyfill (for module)
Installation
1) Download the package through npm
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.
Example
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;
}
}
})
2) With CDN.
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/vue-tween@1.0.0/vue-tween.min.js"></script>
Usage
<template>
<div v-tween:click.capture="myTweening">
<span :style="style"></span>
</div>
</template>
1) Directive name required
<div v-tween:click.capture="myTweening">
This could be renamed during installation
2) Arguments required
<div v-tween:click.capture="myTweening">
DOM event. For Vue 1.x, it is based on built-in on
directive.
3) Modifier
<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.
4) Handler
<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.
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>
License
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.
8 years ago