JSFiddle - React, Tailwind, and code Playground
JavaScript
var o = document.createElement('div')
o.innerHTML = '☻'
o.style.fontSize = '500%'
o.style.position = 'absolute'
document.body.appendChild(o)
var step = 100, // distance to jump, px
speed = 100, // time per frame, ms
s = o.style,
w = document.documentElement.clientWidth,
h = document.documentElement.clientHeight;
setInterval(function(){
var x = parseInt(s.left) || 0;
var y = parseInt(s.top) || 0;
var x_move = getRandomInt(0, step)
var y_move = getRandomInt(0, step)
x = getRandomBool() ? x + x_move : x - x_move
y = getRandomBool() ? y + y_move : y - y_move
x = x<0?0-x:(x>(w-50)?w-x_move:x)
y = y<0?0-y:(y>(h-50)?h-y_move:y)
s.left = x + 'px'
s.top = y + 'px';
}, speed)
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getRandomBool(){
return (Math.random() > 0.5);
}