Utilize JavaScript to randomly choose images as background tiles in HTML

Currently, I am in the process of developing a game using HTML/CSS/JavaScript. My background is currently set to a single image (100px / 100px) being repeated vertically and horizontally to tile across the entire page body;

CSS:

body {
    background-image: url("./assets/bgImage.png");
    background-repeat: repeat;
}

It works well and functions as intended.

I am wondering if it's feasible to create an array of images to choose from and have each repetition of CSS randomly select one of the images from the array to fill into the next slot. If this is possible, how can it be achieved? The desired outcome is illustrated below (minus the margins);

https://i.sstatic.net/mlxez.png https://i.sstatic.net/dwkth.png
https://i.sstatic.net/M9GML.png https://i.sstatic.net/mlxez.png

The only method that comes to mind for achieving this would involve using

<div style="position: absolute; z-index: -1;">
and then populating it with a generated array of images. While that approach is viable and I could implement it that way, I want to explore simpler and more effective alternatives first.

Answer №1

This solution offers a simple and efficient approach using the HTML canvas element along with image data for rendering.

Modern JavaScript (ES6):

class CreateMap {

  /**
   * Constructor
   */
  constructor() {
    const self = this;

    // canvas
    self.canvas = document.getElementById("canvas");
    self.ctx = canvas.getContext("2d");

    const TILE_WIDTH = 100;
    const TILE_HEIGHT = 100;

    const CANVAS_WIDTH = 500;
    const CANVAS_HEIGHT = 500;

    self.canvas.width = CANVAS_WIDTH;
    self.canvas.height = CANVAS_HEIGHT;

    let xAxes = 0;
    let yAxes = 0;
    let lineCount = 0;

    // existing tiles
    self.tiles = [{
      url: 'https://i.sstatic.net/dwkth.png'
    }, {
      url: 'https://i.sstatic.net/mlxez.png'
    }, {
      url: 'https://i.sstatic.net/M9GML.png'
    }];

    // Generate tiles on the canvas
    Array(CANVAS_WIDTH / TILE_WIDTH * CANVAS_HEIGHT / TILE_HEIGHT)
      .fill()
      .forEach(function() {
        const randomNum = self.getRandomNumber(0, self.tiles.length - 1);
        const tile = self.tiles[randomNum]

        self.createTile(tile.url, xAxes, yAxes);

        xAxes += TILE_WIDTH;

        if (xAxes === CANVAS_WIDTH) {
          xAxes = 0;
          yAxes += TILE_HEIGHT;
        }
      });
  }

  /**
   * Get a random number within specified range
   * @param  {number} min 
   * @param  {number} max 
   * @return {number}     
   */
  getRandomNumber(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }

  /**
   * Render one tile on the canvas
   * @param  {string} url 
   * @param  {number} x  
   * @param  {number} y     
   */
  createTile(url, x, y) {
    let tile = new Image();

    tile.onload = () => {
      this.ctx.drawImage(tile, x, y);
    };

    tile.src = url;
  }
}

const map = new CreateMap();
<canvas id="canvas"></canvas>

Traditional Javascript (ES5):

var CreateMap = function() {

  var self = this;

  // canvas
  self.canvas = document.getElementById("canvas");
  self.ctx = canvas.getContext("2d");

  var TILE_WIDTH = 100;
  var TILE_HEIGHT = 100;

  var CANVAS_WIDTH = 500;
  var CANVAS_HEIGHT = 500;

  var xAxes = 0;
  var yAxes = 0;
  var lineCount = 0;
  
  self.canvas.width = CANVAS_WIDTH;
  self.canvas.height = CANVAS_HEIGHT;

  // existing tiles
  self.tiles = [{
    url: 'https://i.sstatic.net/dwkth.png'
  }, {
    url: 'https://i.sstatic.net/mlxez.png'
  }, {
    url: 'https://i.sstatic.net/M9GML.png'
  }];

  // Generate tiles
  Array(CANVAS_WIDTH / TILE_WIDTH * CANVAS_HEIGHT / TILE_HEIGHT)
    .fill()
    .forEach(function() {
      var randomNum = self.getRandomNumber(0, self.tiles.length - 1);
      var tile = self.tiles[randomNum]

      self.createTile(tile.url, xAxes, yAxes);

      xAxes += TILE_WIDTH;

      if (xAxes === CANVAS_WIDTH) {
        xAxes = 0;
        yAxes += TILE_HEIGHT;
      }
    });
}


CreateMap.prototype.getRandomNumber = function(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

CreateMap.prototype.createTile = function(url, x, y) {
  var tile = new Image();

  tile.onload = () => {
    this.ctx.drawImage(tile, x, y);
  };

  tile.src = url;
}

var map = new CreateMap();
<canvas id="canvas"></canvas>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

tips for getting two ajax json Data from .net

When working with .NET, I am encountering an issue where I need to send two sets of JSON data (test1 and test2) to a .NET controller using JavaScript (ajax). Here is the code snippet for sending the data: .ajax({ type: 'POST', url ...

Displaying an image with a JavaScript variable is a common task in web

I have a Javascript code snippet below where the image name "samson decosta" is stored in a MySQL database. I am retrieving this image and trying to display it as a background_image in a div. document.getElementById("image_chk").style.backgroundImage="url ...

What is the process for assigning variables to modules using RequireJS?

Is there a way to define variables for modules in RequireJS? In simpler terms, how can I achieve the equivalent of the following using RequireJS: var fs = require('fs'); var child_process = require('child_process'); I am looking to s ...

Textfield with predictive text suggestions

I am currently working on implementing an autocomplete textfield for my Rails application, following the example from the Agile Web Development with Rails, 3rd Edition. However, when I try to insert their demo code: <%= stylesheet_link_tag &apo ...

The Microsoft.Azure.WebJobs.Script encountered an issue while attempting to cast an object of type 'System.String' to type 'Microsoft.AspNetCore.Http.HttpRequest' during the return process

I recently encountered an issue with my Azure Function written in JS that is triggered by the Service Bus and generates files to Blob Storage. When attempting to return an HTTP result, I received the following error message: System.Private.CoreLib: Except ...

The installation of robotjs via npm failed due to issues encountered while trying to build the binaries

After attempting to execute the command "npm install robotjs -g," an error is thrown back at me. [email protected] install C:\Users\Ehsan\AppData\Roaming\npm\node_modules\robotjs prebuild-install || node-gyp reb ...

Finding the distance between two coordinates using Mapbox is easy once you understand how to utilize

Currently, I am in the process of learning how to utilize the Mapbox API within my node application. One of my objectives is to execute calculations on the backend, particularly obtaining the distance between two sets of coordinates. I'm struggling w ...

Tips for concealing protruding sections of 3D shapes behind intricate 3D models

Currently, I am working on rendering a complex 3D mesh using Three.js (an iliac bone) and including some simple spheres to mark specific points on the surface where muscles would attach. However, I have encountered an issue where the markers protrude out ...

What's the reason behind the presence of the a tag before the button in this

While working with Bootstrap 4, I noticed that the a tag is being displayed before the button when using the following code. Can anyone explain why? <button class="navbar-toggler hidden-sm-up" type="button" data-toggle="collapse" data-target="#navbar ...

Ways to access the value of an attribute in an AngularJS object

Is there a way to access the value of field.jobtype within a controller? var app=angular.module('myapp',['ui.bootstrap','ui.select']); app.controller('mycontroller',function($scope){ $scope.onStateSelected = func ...

Retrieve an image from the server and display a preview of it on the client side

I'm currently in the process of fetching an image from a server and previewing it on the client side. I have successfully retrieved the image, but I am unsure of how to asynchronously display it on a web page. axios.get(link,{responseType:'strea ...

What is the best way to display JQuery mobile tap event information in real-time?

I am experiencing an issue in my code where the tap event is being triggered, but the array[i] value is printed as null. Whenever I click on any index, it always prints an empty string (" "). Why does it display null instead of the clicked value? I am see ...

How can I retrieve a DOM object following an AJAX request?

My AJAX call fetches and appends HTML content to the current page. I hope to access this newly added HTML using standard jQuery selectors. Here's my desired approach... $.ajax({ url: url, success: function(data) { $('body').app ...

Ways to retrieve interface definition using a variable

I have an interface that organizes various states together export interface ScanFiltersStatePage1 { keywords: SensitiveInfoFileKeywordFilter categories: string[] classifications: string[] fileTypes: string[] infotypes: string[] regulations: str ...

Is there a way to open and view several images within this code snippet?

cv2_imshow((predict[0].masks.masks[0].numpy() * 255).astype("uint8")) I have successfully executed this script to display one image. However, I am curious to know how I can modify it to read and display multiple images stored in the predict[] ar ...

retrieving photos from the server and showcasing them

I'm facing a challenge and would appreciate your guidance. I have a collection of images stored on a server. In my client-side code using jQuery, I need to retrieve these images (not just their links) from the server via AJAX and PHP, then display the ...

JavaScript can extract a portion of an array

Is it possible to generate a new array consisting of all elements ranging from the nth to the (n+k)th positions within an existing array? ...

Is it possible to precisely position multiple children in a relative manner?

I am currently developing a custom dropdown menu where I want the parent element to be relatively positioned within the page. The goal is for all the child elements of the dropdown menu (the list items) to appear as if they are part of a select element and ...

Customize the printing settings within bootstrap by modifying the background color from transparent to a solid color using CSS/SCSS

I am facing an issue with the bootstrap CSS/print feature. In the bootstrap CSS (reset.css) file, all styles are cleared for printing purposes. @media print { * { text-shadow: none !important; color: black !important; background: transparen ...

Tips for incorporating a spinner during content loading within AngularJS

When the user clicks on the "Search" button, content will load and the button label will change to "Searching" with a spinner shown while the content is loading. Once the content has loaded (Promise resolved), the button label will revert back to "Search" ...