const dot = document.querySelector( `.dot` ).style;
function getRandomInteger( min,max ) {
min = Math.ceil( min );
max = Math.floor( max );
return Math.floor( Math.random() * ( max - min + 1 ) ) + min;
}
// use randomInteger for x and y values
// for transform: translate( x%,y% )
// range here will be from negative integer
// to positive integer. The CSS unit is a %
function move( element,range ) {
element.transform =
`
translate(
${ getRandomInteger( -range,range ) }%,
${ getRandomInteger( -range,range ) }%
)
`
}
//range here is 250 negative ad positive percent
setInterval( function() { move( dot,250 ) },500 );
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html, body {
overflow: hidden;
height: 100%;
}
body {
display: flex;
justify-content: center;
align-items: center;
background-color: #eee;
}
.dot {
border-style: none;
border-radius: 50%;
width: 2.5rem;
height: 2.5rem;
background-color: rgba( 0,0,0,0.5 );
transition-property: transform;
transition-duration: 2s;
transition-timing-function: ease-in-out;
}
<hr class='dot'>
This code above moves a dot to a random position on the page.
It works as expected except for the transitions from one location to another. The goal being a smoother transition in between each movement.
The idea initially was to create a floating or hovering effect with subtle movements similar to an object gently floating on the surface of water.
We used transition-timing-function: ease-in-out on the last CSS line as an attempt to lessen the abruptness of the direction changes above. Yet altering the timing function to any value doesn't seem to help very much. Including custom cubic-bezier values.
How can we get the animation to change directions less abruptly and be a smoother overall motion?