Learn how to create drawings with Vue.js and position them side by side

I'm looking to place images next to each other within the same div. I've been able to draw the images successfully, but I am struggling with positioning them side by side.

Each time I draw an image, it is placed in the same div.

This is my HTML structure:

<div id="thumbnailImages">
    <canvas
        id="thumbnailImage"
    />
  </div>

Here is the code snippet:

 function thumbnail(video) {
  const canvasThumbnail = document.getElementById('thumbnailImage')
  const cth = canvasThumbnail.getContext('2d')

  const data = video.slice(8)
  imgToHTML(data, e => {
    const drawingThumbnail = new Image()
    drawingThumbnail.src = e.target.result
    drawingThumbnail.onload = function () {
      canvasThumbnail.width = 128
      canvasThumbnail.height = 128
      cth.drawImage(drawingThumbnail, 0, 0)
      document.getElementById('thumbnails').appendChild(canvasThumbnail)
    }
  })
}

I want to have multiple 'canvas' elements within the 'thumbnailImages' ID. Can you offer any assistance?

Answer №1

The problem arises from the repeated use of the same canvas element. To resolve this, it is important to generate a new canvas element for each iteration:

 function createThumbnail(video) {
  const thumbnailCanvas = document.createElement('canvas');
  const ctx = thumbnailCanvas.getContext('2d');

  const imageData = video.slice(8);
  
  convertImageToHTML(imageData, event => {
    const thumbnailImage = new Image();
    thumbnailImage.src = event.target.result;
    
    thumbnailImage.onload = function () {
      thumbnailCanvas.width = 128;
      thumbnailCanvas.height = 128;
      ctx.drawImage(thumbnailImage, 0, 0);
      document.getElementById('thumbnailImages').appendChild(thumbnailCanvas);
    }
  });
}

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

Transform Text into Numeric Value/Date or Null if Text is Invalid

Consider the TypeScript interface below: export interface Model { numberValue: number; dateValue: Date; } I have initialized instances of this interface by setting the properties to empty strings: let model1: Model = { numberValue: +'', ...

Having trouble extracting the responseText from the Ajax request

My current challenge involves making an ajax call and receiving an Object in the response. However, when I attempt to access "responseText," it keeps returning as undefined: var results = API.get('users', { username: un, userpass: pw } ); conso ...

Tips for transferring an array from a form to a URL using JavaScript?

My form uses a get method, and includes a select element allowing users to choose multiple options. However, when the user submits the form, the URL does not neatly display these selections. Is there a way for me to pass the array in a more organized manne ...

Using Sails.js server to power an Angular.js web application

Currently, I am in the process of creating a web application with Sailsjs for the backend and Angular.js for the frontend. To set up the Angular app, I utilized yeoman with the angular generator, while for the backend, I relied on the default Sails app gen ...

Combining package.json commands for launching both an Express server and a Vue app

I recently developed an application using Vue.js and express.js. As of now, I find myself having to open two separate terminal windows in order to run npm run serve in one and npm start in the other. My ultimate goal is to streamline this process and have ...

Angular HttpClient does not support cross-domain POST requests, unlike jQuery which does

I am transitioning to Angular 13 and I want to switch from using jQuery.ajax to HttpClient. The jquery code below is currently functional: function asyncAjax(url: any){ return new Promise(function(resolve, reject) { $.ajax({ type: ...

What could be causing the color to not appear in the textarea depending on the time condition?

I am attempting to dynamically change the color of my calendar blocks based on the current time. I have created variables for various times in military format and compared them with the current time using a conditional statement. For instance, I have spec ...

Creating an array in TypeScript and determining its type as an object

Currently, I am in the process of creating an app with Ionic, utilizing TypeScript as the primary language. When declaring an array in my code: products = new Array(); I expect it to be recognized as an array. However, when I check the type using consol ...

Toggling the visibility of a div using JavaScript

When I click the button, I want to show and then hide a div. However, it doesn't work on the first click - only on the second click. How can I make it work on the first click? HTML <p>Click the button</p> <button onclick="myFu ...

Implementing various distinct string substitutions based on the specified character beginning and end positions

I have a string of text: Google is a search engine created by Larry Page and Sergey Brin. Within this string, there are certain named entities stored in a nested array. Here's what the array looks like: [["Google","ORG",0,5],["search engine","PRODU ...

Disabling a chosen option within a dropdown menu

`Hello there, I am just starting out with JavaScript and jQuery. Currently, I am developing a web application for ordering food and drinks. I have a dropdown menu for selecting breakfast items and the quantity. When the "add" button is clicked, a dynamic ...

How can I overlay an environment map onto existing materials in ThreeJS?

Hello, I am a beginner in ThreeJS and I'm currently working on adding an Environment Map on top of the materials loaded using JSON loader. This is my approach: var gun = null; var loader = new THREE.JSONLoader(); loader.load("obj/nissan/nissan-gt-r-n ...

Windows location does not change after an XMLHttpRequest is made

Here is my code that uses XMLHttpRequest: function SignUp() { signUpConnection = new XMLHttpRequest(); signUpConnection.onreadystatechange = processRegistration; signUpConnection.open('GET', 'index.php?registrarse=&username= ...

Challenges of managing Angular state and resolving issues

I'm currently diving into the world of learning the mean stack and facing some challenges with Angular and routing. In my JavaScript file below, I have the routing code stored within the app.config module. var app = angular.module('doorSensorR ...

Develop a monitor for an entity that has not been created

Currently, I am tackling a feature that involves tracking an asynchronous request within a synchronous one. Let me elaborate. The code snippet I am working with looks something like this: const myObj = {}; function sendMessage(requestId, data) { kafkaP ...

Include a CSS counter style class that adds leading zeros

Looking for a way to increment a particular <p> style class (p.wp as an example) from 1 - n across multiple HTML pages. The challenge is that the number needs to have 4 digits, ranging from 0001 to 0117. #start_wp {counter-reset: WP 0;} p.wp{} p. ...

Hold on for the asynchronous operation to complete before redirecting in KoaJS

Currently, I am facing a challenge in spawning a child process to manage some POST data in NodeJS (utilizing the Koa framework). My goal is to wait for the child process to complete before redirecting, but due to the asynchronous nature of the child proce ...

Unable to Add Dependency to Subclassed Object

In my setup, there are three classes that interact with each other. I am utilizing inversify for handling dependency injection. However, I encountered an issue when I injected the class MessageBroker into the derived class Repository and found that the Mes ...

Vue responsiveness with the picture tag utilizing srcset

Just had a quick question. In my HTML, I have a picture tag set up for handling responsive images like this: <picture> <source media="(min-width: 900px)" srcset="image-lg_1x.webp 1x, image-lg_2x.webp 2x" type="image/webp" > <sou ...

When working on one of my subdomains, I require the ability to distribute cookies between the two

Currently, I am involved in a project that utilizes a sub-domain under a parent domain called "my-project.parent.com", which unfortunately I do not have access to. Additionally, there is another project operating under the main domain "parent.com" with t ...