JSFiddle - React, Tailwind, and code Playground

by Sam Fereday

HTML

<div id="output">
    <span id="mover"></span>
    <span id="moverb"></span>
</div>

CSS

#output {
    width: 100px;
    height: 100px;
    position: relative;
    border: 1px solid red;
}
#mover, #moverb {
    display: block;
    width:5px;
    height:5px;
    position: absolute;
    left: 0;
    top:0;
    background: blue;
}

JavaScript

var output = document.getElementById("output");
var mover = document.getElementById("mover");
var moverb = document.getElementById("moverb");

var y = 100;
var x = 100;

// Player x pos
var x1 = 0;

// Player y pos
var y1 = 0;
var y2 = 0;

var moveMe = function() {
    
    x += 1;
    
    if(x > 95) {
         x = 0;   
    }
    
    // Calculate y position
    // If travelling from bottom left to top right
    y1 = y + (x1 - x);
    
    // If travelling from top left to bottom right
    // we need the top left x coord of the tile, then to ensure
    // we travel down the slope, we take from the height of the tile,
    // rather than add to it.
    y2 = -100 + (100 - (x1 - x));
    
    mover.style.top = (y1 - 5) + 'px';
    mover.style.left = x + 'px';
    
    moverb.style.top = (y2) + 'px';
    moverb.style.left = x + 'px';

    var t = setTimeout(function(){
        moveMe();
    }, 20);
    
}

moveMe();