const random = (min, max) => Math.random() * (max - min) + min;
const map = (value, fromMin, fromMax, toMin, toMax) => {
let result = 0;
result = (value <= fromMin)
? toMin : (value >= fromMax)
? toMax : (() => {
let ratio = (toMax - toMin) / (fromMax - fromMin);
return (value - fromMin) * ratio + toMin;
})();
return result;
};
class smoothMove {
constructor(target, speed = 2, wrapWidth = window.innerWidth, wrapHeight = window.innerHeight) {
this.target = target;
this.angle = 0;
this.speed = speed;
this.center = {
x: wrapWidth / 2 - this.target.width() / 2,
y: wrapHeight / 2 - this.target.height() / 2
};
this.radius = window.innerHeight / 4;
return this;
}
getRadian(angle) {
return angle * Math.PI / 180;
}
play() {
let _angle = 0;
this.target.each((i, el) => {
_angle = this.angle + (360 / this.target.length) * i;
let x = this.center.x + Math.sin(this.getRadian(_angle)) * this.radius;
let y = this.center.y + Math.cos(this.getRadian(_angle)) * this.radius;
TweenMax.set(el, {
x: x,
y: y,
z: Math.sin(this.getRadian(this.angle)) * this.radius * 2,
scale: map(Math.sin(this.getRadian(this.angle + i * this.radius)), -1, 1, 1, 1.5),
rotationY: map(Math.cos(this.getRadian(this.angle + i * this.radius)), -1, 1, 1, 360),
rotationZ: map(Math.sin(this.getRadian(this.angle + i * this.radius)), -1, 1, 1, 360),
backgroundColor: `hsla(${this.angle + (360 / this.target.length) * i}, 80%, 70%, 1)`
});
});
this.angle += this.speed;
requestAnimationFrame(() => this.play() );
}
}
let anim = new smoothMove($('.target'));
anim.play();