const random = (min, max) => Math.random() * (max - min) + min;
class smoothMove {
constructor(target, wrapWidth = window.innerWidth, wrapHeight = window.innerHeight) {
this.target = target;
this.angle = {
x: random(0,360),
y: random(0,360),
z: random(0,360),
};
this.speed = {
x: random(-2,4),
y: random(-2,4),
z: random(-2,4),
};
this.center = {
x: wrapWidth / 2 - this.target.width() / 2,
y: wrapHeight / 2 - this.target.height() / 2
};
this.radius = window.innerHeight / 2;
return this;
}
getRadian(angle) {
return angle * Math.PI / 180;
}
play() {
let offset = {
x: Math.sin(this.getRadian(this.angle.x)) * this.radius,
y: Math.sin(this.getRadian(this.angle.x)) * this.radius,
z: Math.cos(this.getRadian(this.angle.z)) * this.radius
};
TweenMax.set(this.target, {
x: this.center.x + offset.x,
y: this.center.y + offset.y,
z: offset.z,
});
this.angle.x += this.speed.x;
this.angle.y += this.speed.y;
this.angle.z += this.speed.z;
requestAnimationFrame(() => {
this.play();
});
}
}
let anim = new smoothMove($('.target'));
anim.play();