JSFiddle - React, Tailwind, and code Playground
HTML
<canvas width="500" height="500" id="can"></canvas>
JavaScript
function Platform(pos, wid, hei){
this.draw = function(ctx){
ctx.strokeRect(pos.x+.5, pos.y+.5, 20, 20);
};
}
var can = document.getElementById("can"),
ctx = can.getContext('2d'),
wid = can.width,
hei = can.height,
numPlatforms = 14,
platWid = 20,
platHei = 20,
platforms = [],
hash = {};
for(var i = 0; i < numPlatforms; i++){
var posX = Math.floor(Math.random()*(wid-platWid)/platWid)*platWid,
posY = Math.floor(Math.random()*(hei-platHei)/platHei)*platHei;
while (hash[posX + 'x' + posY]){
posX = Math.floor(Math.random()*wid/platWid)*platWid;
posY = Math.floor(Math.random()*hei/platHei)*platHei;
}
hash[posX + 'x' + posY] = 1;
platforms.push(new Platform({x:posX, y:posY}, platWid, platHei));
}
for(var i = 0; i < platforms.length; i++){
platforms[i].draw(ctx);
}