Is it possible to have 2 images side by side?

I am attempting to combine these two images to create a map. Here is an illustration of how they should fit together.

https://i.sstatic.net/zGZgS.png

var testHeightMap =
  'x\n' +
  'x';

var myCanvas = document.getElementById('game_canvas');
var ctx = myCanvas.getContext('2d');
var image = new Image;
var tilesCycled = 0;

image.onload = function() {
  var lines = testHeightMap.split('\n');
  var rowIndex = 0;

  for (var i = 0; i < lines.length; i++) {
    tilesCycled = 0;

    for (var j = 0; j < lines[i].length; j++) {
      tilesCycled = tilesCycled + 1;

      var width = (myCanvas.width / 2 - image.width / 2) + tilesCycled * 30.8;
      var height = (myCanvas.height / 2 - image.height / 2) + tilesCycled * 15.9;


      ctx.drawImage(image,
        width + rowIndex * 34,
        height - rowIndex * 25,
      );
    }

    rowIndex = rowIndex + 1;
  }
}

image.src = 'https://raw.githubusercontent.com/Joopie1994/html5project-client/master/resources/assets/images/floor_tile.png';
body {
  background-color: black;
}
<canvas id="game_canvas"></canvas>

Answer №1

Simply modify the for loop structure and adjust a few numbers - then the code will function correctly.

var testHeightMap =
  'ooo\n' +
  'ooo\n' +
  'ooo';

var myCanvas = document.getElementById('game_canvas');
var ctx = myCanvas.getContext('2d');
ctx.canvas.width = 512
ctx.canvas.height = 512
var image = new Image;
var tilesCycled = 0;

image.onload = function() {
  var lines = testHeightMap.split('\n');
  for (var i = lines.length; i > 0; i--) {
    for (var j = 0; j < lines[i-1].length; j++) {
      var width = (myCanvas.width / 2 - image.width / 2)
      var height = (myCanvas.height / 2 - image.height / 2)
      ctx.drawImage(image,
        width + (j+i) * 32,
        height + (j-i) * 16,
      );
    }
  }
}

image.src = 'https://raw.githubusercontent.com/Joopie1994/html5project-client/master/resources/assets/images/floor_tile.png';
body {
  background-color: black;
}
<canvas id="game_canvas" style="width:512px;height:512px;"></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

Prevent any http requests until an observable completes its task

I'm currently developing an Angular interceptor where, before sending any non-GET requests, I need to make a GET request to obtain a specific header from the server and then set that header on all subsequent requests. Is there a method for achieving t ...

Flex-wrap functionality in versions of Safari 6 and earlier

As I work on developing my website, I have been using flexbox to assist with the layout. It has been functioning well on most browsers, but I have encountered issues with Safari versions below 6.1. Specifically, I am struggling with flex-wrap as I want the ...

The onreadystatechange function in AJAX is malfunctioning

I am facing an issue with the onreadystatechange while making an ajax request. Previously, it would call the processRequest function successfully but now it seems to be malfunctioning. I'm uncertain if I inadvertently made any changes or modification ...

Transmitting HTML content to the browser from the client using Node.js

Quoted from: Book - "Getting MEAN with Mongo, Express.." When it comes to responding to requests in your application by sending HTML to the browser, Express simplifies this process compared to native Node.js. With support for various templating e ...

Tips for aligning content vertically in the center of a page?

A single div with dimensions of 700px width and 200px height was created. Within this div, various tags will dynamically load, spanning up to two lines. When the tags are displayed in two lines, the alignment appears correct; however, if they only take u ...

Using regular expressions to validate input in Javascript

Seeking assistance to validate an input text using the pattern <some_string>:<some_string> in JS/JQuery. For instance: A110:B120 AB12C:B123 I understand this might seem overly simplistic, but any guidance would be greatly appreciated. ...

An issue is preventing the Angular 2+ http service from making the requested call to the server

I am looking to create a model class that can access services in Angular. Let's say I have the following endpoints: /book/:id /book/:id/author I want to use a service called BooksService to retrieve a list of Book instances. These instances should ...

The module 'react/lib/React' could not be located within the file 'ReactTestUtils.js'

Setting up unit-tests for a React Native project using ReactTestUtils is causing an issue with npm test. The error message I receive is: Cannot find module 'react/lib/React' from 'ReactTestUtils.js' I suspect that my dependencies are ...

Having trouble with jQuery hover and AJAX loaded content not functioning properly?

When I receive content through AJAX, it looks like this: <div class="message" id="1"> blah <div class="details" id="1" style="float: left; display: none;">hidden information</div> <div class="spacer" style="clear: both;"&g ...

Tips on making a fresh form appear during the registration process

After clicking on a submit button labeled as "continue" within a form, a new form will appear for you to complete in order to continue the registration process. ...

Issue with React component not reflecting updated state following Axios call

I have a series of chained axios calls to different APIs. When I log the state inside the function, I see that it is being updated correctly. However, when I log the state in the render() method, I do not see the updated state. The function is triggered a ...

Exploring the concept of union types and typeguards in TypeScript

I'm struggling with this code snippet: function test(): any[] | string { return [1,2] } let obj: any[] = test(); When I try to run it in vscode, I get the following error message: [ts] Type 'string | any[]' is not assignable to type & ...

Show the button only when the text is updated

Is there a way to display a button only when the quantity of items in the cart changes, similar to the eBay shopping cart feature? I was able to implement it but it's not functioning as expected. Here is the link to what I have so far. Does anyone kno ...

Having trouble simulating JavaScript Math.random in Jest?

Math.random() seems to always return random values instead of the mocked ones. script.test.js jest.spyOn(global.Math, "random").mockReturnValue(0.123456789); const html = fs.readFileSync(path.resolve(__dirname, "./script.html"), " ...

AngularJS - Changing Views with Templates

At the moment, I am changing my directives in the following manner. Within my directive: (function(){ 'use strict'; var controller = ['$scope', function ($scope) { }]; angular .module('moduleName' ...

Quickest method for transmitting data to a Java socket server from a website

My main concern is to avoid using Java for the client side as the web interface is specifically designed for mobile devices. The primary objective is to determine the most efficient method to transmit a string from an "Onmousedown", "Onmouseup", or "Oncli ...

What could be the reason for the inability of my external jQuery file to be implemented on a className within React

I am currently in the process of building a navigation menu for the mobile version of my website using jQuery. However, I'm encountering an issue when trying to integrate it within the className of my reactjs application. Despite placing the jQuery fi ...

Employing Ajax for interacting with a database

I am attempting to query my database using a JavaScript function for the first time, and I am struggling to get it to work. While unsure if this is the correct approach, it is the one that I have come across. Below is a simple snippet of my HTML code: < ...

Resizable Bootstrap column-md-4

I need to make a bootstrap column fixed position (col-md-4). This is for a shop layout with 2 columns: Shop content (long & scrollable) col-md-8 Cart content (short & fixed so it stays visible while scrolling the shop content) col-md-4 I'm ...

Using ASP.NET to retrieve data with AJAX and pass parameters to a web method

Looking for assistance with a web method I have created: [WebMethod] [ScriptMethod(UseHttpGet = true)] public static string test(string Name, int? Age) { return "returned value"; } Below is the ajax call I am attempting: $.ajax({ type: "GET", ur ...