├── LICENSE ├── README.md └── spark-tween.js /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 ChrisError 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SparkTween 2 | A tiny teeny tween function for SparkAR 3 | ### by chrisError 4 | https://pl.ai 5 | 6 | 7 | make sure to add the following to which ever scripts need to use SparkTween 8 | 9 | 10 | const { 11 | Tween, 12 | Ease, 13 | SparkTweener 14 | } = require("./tween.js"); 15 | 16 | Tween(startVal, endVal, duration, loopCount, mirror, ease, completeCallback) 17 | 18 | startVal - start value for tween 19 | 20 | endVal - end value for tween 21 | 22 | duration - length of time for tween (in seconds) 23 | 24 | loopCount - how many times should the tween loop (-1 for infinite) 25 | 26 | mirror - bool - should the tween yoyo 27 | 28 | ease - ease type [see enum for supported eases] 29 | 30 | completeCallback - method to call on complete 31 | 32 | 33 | ### breaking changes from previous version 34 | please note the .animation when setting the tween to a property 35 | 36 | 37 | ## SIMPLE USAGE (using the tween directly) 38 | 39 | var sceneObject = Scene.root.find("MySceneObject); 40 | 41 | sceneObject.transform.y = Tween(0.35, 2, 4, -1, false, Ease.LINEAR, null).animation; 42 | 43 | 44 | ## EXTENDED FUNCIONALITY 45 | var sceneObject = Scene.root.find("MySceneObject); 46 | 47 | var tween = Tween(0.35, 2, 4, -1, false, Ease.LINEAR, null); 48 | 49 | sceneObject.transform.y = tween.animation; 50 | 51 | tween.Kill(); //CALL THIS WHEN YOU WANT TO KILL THE TWEEN 52 | 53 | tweens also now support infinite loops by passing -1 as the loopCount 54 | 55 | 56 | @chrisError 20 / 02 / 2020 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /spark-tween.js: -------------------------------------------------------------------------------- 1 | // function Tween(startVal, endVal, duration, loopCount, mirror, ease, completeCallback) {} 2 | 3 | 4 | /* 5 | 6 | ---NOTES----- 7 | 8 | make sure to add the following to which ever scripts need to use SparkTween 9 | 10 | 11 | const { 12 | Tween, 13 | Ease, 14 | SparkTweener 15 | } = require("./tween.js"); 16 | 17 | 18 | 19 | SIMPLE USAGE (using the tween directly) 20 | var sceneObject = Scene.root.find("MySceneObject); 21 | sceneObject.transform.y = Tween(0.35, 2, 4, -1, false, Ease.LINEAR, null).animation; 22 | 23 | 24 | EXTENDED FUNCIONALITY 25 | var sceneObject = Scene.root.find("MySceneObject); 26 | var tween = Tween(0.35, 2, 4, -1, false, Ease.LINEAR, null); 27 | sceneObject.transform.y = tween.animation; 28 | 29 | tween.Kill(); //CALL THIS WHEN YOU WANT TO KILL THE TWEEN 30 | 31 | 32 | @chrisError 20 / 02 / 2020 33 | 34 | */ 35 | const Time = require('Time'); 36 | const Reactive = require('Reactive'); 37 | const Animation = require('Animation'); 38 | 39 | 40 | export const Ease = { 41 | 42 | LINEAR: "linear", 43 | BOUNCE_IN: "easeInBounce", 44 | BOUNCE_OUT: "easeOutBounce", 45 | EASE_IN_BACK: "easeInBack", 46 | EASE_IN_CIRC: "easeInCirc", 47 | EASE_IN_CUBIC: "easeInCubic", 48 | EASE_IN_ELASTIC: "easeInElastic", 49 | EASE_IN_EXPO: "easeInExpo", 50 | 51 | EASE_IN_OUT_BACK: "easeInOutBack", 52 | EASE_IN_OUT_BOUNCE: "easeInOutBounce", 53 | 54 | EASE_IN_OUT_CIRC: "easeInOutCirc", 55 | EASE_IN_OUT_ELASTIC: "easeInOutElastic", 56 | 57 | EASE_IN_OUT_EXPO: "easeInOutExpo", 58 | EASE_IN_OUT_QUAD: "easeInOutQuad", 59 | 60 | EASE_IN_OUT_QUART: "easeInOutQuart", 61 | EASE_IN_OUT_SINE: "easeInOutSine", 62 | 63 | EASE_IN_QUAD: "easeInQuad", 64 | EASE_IN_QUART: "easeInQuart", 65 | 66 | EASE_IN_QUINT: "easeInQuint", 67 | EASE_IN_SINE: "easeInSine", 68 | 69 | 70 | EASE_OUT_BACK: "easeOutBack", 71 | EASE_OUT_CIRC: "easeOutCirc", 72 | 73 | 74 | EASE_OUT_CUBIC: "easeOutCubic", 75 | EASE_OUT_ELASTIC: "easeOutElastic", 76 | 77 | 78 | EASE_OUT_EXPO: "easeOutExpo", 79 | EASE_OUT_QUAD: "easeOutQuad", 80 | 81 | EASE_OUT_QUART: "easeOutQuart", 82 | EASE_OUT_QUINT: "easeOutQuint", 83 | EASE_OUT_SINE: "easeOutSine" 84 | 85 | }; 86 | 87 | export class SparkTweener { 88 | constructor(animation, driver, sub) { 89 | this.animation = animation; 90 | this.driver = driver; 91 | this.sub = sub; 92 | } 93 | 94 | Kill() { 95 | this.driver.stop(); 96 | if (this.sub != null) { 97 | this.sub.unsubscribe(); 98 | } 99 | } 100 | 101 | 102 | } 103 | 104 | export function Tween(startVal, endVal, duration, loopCount, mirror, ease, completeCallback) { 105 | 106 | if (loopCount == -1) { 107 | loopCount = Infinity; 108 | } 109 | var driver = Animation.timeDriver({ 110 | durationMilliseconds: duration * 1000, 111 | loopCount: loopCount, 112 | onComplete: completeCallback, 113 | mirror: mirror 114 | }); 115 | var sampler; 116 | 117 | try { 118 | sampler = Animation.samplers[ease](startVal, endVal); 119 | } catch (e) { 120 | sampler = Animation.samplers.linear(startVal, endVal); 121 | } 122 | var sub = null; 123 | 124 | if (completeCallback != null) { 125 | var sub = driver.onCompleted().subscribe(completeCallback); 126 | driver.callback = sub; 127 | } 128 | 129 | driver.start(); 130 | 131 | var tweener = new SparkTweener(Animation.animate(driver, sampler), driver, sub); 132 | 133 | return tweener; 134 | } 135 | --------------------------------------------------------------------------------