Not the kind of start I was looking for
Completely understand that Carl. No way was I trying to take anything away from GSAP. I owe many 'this looks fantastic'-like moments to this great tool over the years.
The intention, for a simple requirement with such limited scope as this one, was to propose a solution not dependant on external tools i.e. if this is something stot is fine with in the first place. If they already are using TweenMax then by all means hand all your animation needs over to it. That is all. Now, let me try this solution again. RoughEase is definitely a game-changer and it brings wealth of options to play with, which is what I have tried to do with this modified version of the same thing using TweenMax.
Here is the updated code of the same:
JavaScript:
var myDIV = document.querySelector('div');
var translateMin = -40,
translateMax = -60,
scaleMin = .9,
scaleMax = 1.1,
rotateMin = -10,
rotateMax = 10;
var strengthMin = 2,
strengthMax = 4,
pointsMin = 40,
pointsMax = 100;
var tween = null,
duration = .2;
var ease = RoughEase.ease.config({
template: Power0.easeNone,
strength: randInt(strengthMin, strengthMax),
points: randInt(pointsMin, pointsMax),
taper: 'none',
randomize: true
}); //set taper to 'in' and randomize to 'false'
var tempObject = {};
tempObject.posX = tempObject.posY = -50;
tempObject.destX = randInt(translateMin, translateMax);
tempObject.destY = randInt(translateMin, translateMax);
tempObject.scale = 1;
tempObject.destScale = roundDecimal(rand(scaleMin, scaleMax), 2);
tempObject.rotation = 0;
tempObject.destRotation = randInt(rotateMin, rotateMax);
tween = new TweenMax(tempObject, duration, {
posX: tempObject.destX,
posY: tempObject.destY,
scale: tempObject.destScale,
rotation: tempObject.destRotation,
onUpdate: onUpdate,
onRepeat: onRepeat,
repeat: -1,
yoyo: true,
ease: ease
});
function rand(min, max) {
return min + (Math.random() * (max - min));
}
function roundDecimal(value, place) {
return Math.round(value * Math.pow(10, place)) / Math.pow(10, place);
}
function randInt(min, max) {
return Math.floor(Math.random() * (1 + max - min) + min);
}
function onRepeat() {
//ease=RoughEase.ease.config({template:Power0.easeNone,strength:randInt(strengthMin,strengthMax),points:randInt(pointsMin,pointsMax)});
tempObject.destX = randInt(translateMin, translateMax);
tempObject.destY = randInt(translateMin, translateMax);
tempObject.destScale = roundDecimal(rand(scaleMin, scaleMax), 2);
tempObject.destRotation = randInt(rotateMin, rotateMax);
tween.updateTo({
posX: tempObject.destX,
posY: tempObject.destY,
scale: tempObject.destScale,
rotation: tempObject.destRotation
}, true);
}
function onUpdate() {
tempObject.posX = roundDecimal(tempObject.posX, 2);
tempObject.posY = roundDecimal(tempObject.posY, 2);
tempObject.scale = roundDecimal(tempObject.scale, 2);
myDIV.style.transform = 'translate(' + tempObject.posX + '%,' + tempObject.posY + '%) scale(' + tempObject.scale + ') rotate(' + tempObject.rotation + 'deg)';
}
I have used RoughEase before and I am always blown away by the randomness that it brings to our animations (or a controlled randomness thanks to taper and randomize parameters).
Would love to hear comments on the approach used here by the way? I am still learning.
T