Tips on organizing and designing buttons within a canvas

let canvas = document.getElementById("canvas");
    let context = canvas.getContext("2d");
    // for canvas size
    var window_width = window.innerWidth;
    var window_height = window.innerHeight;
    canvas.style.background="yellow"
    
    canvas.width = window_width;
    canvas.height = window_height;
    let hit_counter=0;
    function randomIntFromInterval(min, max) { // min and max included 
      return Math.floor(Math.random() * (max - min + 1) + min)
    }
    
    class Circle {
      constructor(xpos, ypos, radius, color, text) {
        this.position_x = xpos;
        this.position_y = ypos;
        this.radius = radius;
        this.text = text;
        this.color = color;
      }
      
      // creating circle
      draw(context) {
        context.beginPath();
        context.strokeStyle = this.color;
        context.fillText(this.text, this.position_x, this.position_y);
        context.textAlign = "center";
        context.textBaseline = "middle"
        context.font = "20px Arial";
        context.lineWidth = 5;
        context.arc(this.position_x, this.position_y, this.radius, 0, Math.PI * 2);
        context.stroke();
        context.closePath();
      }
      
      update() {
       this.text=hit_counter;
        context.clearRect(0, 0, window_width, window_height)
        this.draw(context);
    
        if ((this.position_x + this.radius) > window_width) {
          this.dx = -this.dx;
          
        }
    
        if ((this.position_x - this.radius) < 0) {
          this.dx = -this.dx;
          
        }
    
        if ((this.position_y - this.radius) < 0) {
          this.dy = -this.dy;
          
        }
    
        if ((this.position_y + this.radius) > window_height) {
          this.dy = -this.dy;
          
        }
    
        this.position_x += this.dx;
        this.position_y += this.dy;
      }
    }
      
    let my_circle = new Circle(100, 100, 50, 'Black', hit_counter);
    
    
      my_circle.update();
    
    
    
  
 <button>ex1</button>
    <button>ex2</button>
    <button>ex3</button>
    <button>ex4</button>
    <button>ex5</button>
    <button>ex6</button>
    <button>ex7</button>
    <button>ex8</button>
    
    <canvas id="canvas"></canvas>

HTML How can I align and design buttons within a canvas? My attempt to add CSS styling to the buttons didn't yield positive results. How can I achieve this? What is the best way to center the buttons within the canvas?

<button>ex1</button>
    <button>ex2</button>
    <button>ex3</button>
    <button>ex4</button>
    <button>ex5</button>
    <button>ex6</button>
    <button>ex7</button>
    <button>ex8</button>

<canvas id="canvas"></canvas>

JS How can I align and style buttons within a canvas? My attempts to apply CSS to the buttons were not successful. How can I do this effectively? What's the method to center the buttons in the canvas?

let canvas = document.getElementById("canvas");
    let context = canvas.getContext("2d");
    // for canvas size
    var window_width = window.innerWidth;
    var window_height = window.innerHeight;
    canvas.style.background="yellow"
    
    canvas.width = window_width;
    canvas.height = window_height;
    let hit_counter=0;
    function randomIntFromInterval(min, max) { // min and max included 
      return Math.floor(Math.random() * (max - min + 1) + min)
    }
    
    class Circle {
      constructor(xpos, ypos, radius, color, text) {
        this.position_x = xpos;
        this.position_y = ypos;
        this.radius = radius;
        this.text = text;
        this.color = color;
      }
      
      // creating circle
      draw(context) {
        context.beginPath();
        context.strokeStyle = this.color;
        context.fillText(this.text, this.position_x, this.position_y);
        context.textAlign = "center";
        context.textBaseline = "middle"
        context.font = "20px Arial";
        context.lineWidth = 5;
        context.arc(this.position_x, this.position_y, this.radius, 0, Math.PI * 2);
        context.stroke();
        context.closePath();
      }
      
      update() {
       this.text=hit_counter;
        context.clearRect(0, 0, window_width, window_height)
        this.draw(context);
    
        if ((this.position_x + this.radius) > window_width) {
          this.dx = -this.dx;
          
        }
    
        if ((this.position_x - this.radius) < 0) {
          this.dx = -this.dx;
          
        }
    
        if ((this.position_y - this.radius) < 0) {
          this.dy = -this.dy;
          
        }
    
        if ((this.position_y + this.radius) > window_height) {
          this.dy = -this.dy;
          
        }
    
        this.position_x += this.dx;
        this.position_y += this.dy;
      }
    }
      
    let my_circle = new Circle(100, 100, 50, 'Black', hit_counter);
    
    
      my_circle.update();
    
    
    
  

Answer №1

To position your buttons effectively, place them inside a container and utilize the absolute property in CSS

let canvas = document.getElementById("canvas");
    let context = canvas.getContext("2d");
    // for canvas size
    var window_width = window.innerWidth;
    var window_height = window.innerHeight;
    canvas.style.background="yellow"
    
    canvas.width = window_width;
    canvas.height = window_height;
    let hit_counter=0;
    function randomIntFromInterval(min, max) { // min and max included 
      return Math.floor(Math.random() * (max - min + 1) + min)
    }
    
    class Circle {
      constructor(xpos, ypos, radius, color, text) {
        this.position_x = xpos;
        this.position_y = ypos;
        this.radius = radius;
        this.text = text;
        this.color = color;
      }
      
      // creating circle
      draw(context) {
        context.beginPath();
        context.strokeStyle = this.color;
        context.fillText(this.text, this.position_x, this.position_y);
        context.textAlign = "center";
        context.textBaseline = "middle"
        context.font = "20px Arial";
        context.lineWidth = 5;
        context.arc(this.position_x, this.position_y, this.radius, 0, Math.PI * 2);
        context.stroke();
        context.closePath();
      }
      
      update() {
       this.text=hit_counter;
        context.clearRect(0, 0, window_width, window_height)
        this.draw(context);
    
        if ((this.position_x + this.radius) > window_width) {
          this.dx = -this.dx;
          
        }
    
        if ((this.position_x - this.radius) < 0) {
          this.dx = -this.dx;
          
        }
    
        if ((this.position_y - this.radius) < 0) {
          this.dy = -this.dy;
          
        }
    
        if ((this.position_y + this.radius) > window_height) {
          this.dy = -this.dy;
          
        }
    
        this.position_x += this.dx;
        this.position_y += this.dy;
      }
    }
      
    let my_circle = new Circle(100, 100, 50, 'Black', hit_counter);
    
    
      my_circle.update();
.container{
position:absolute;
}

.container .buttons{
  position: absolute;
  top:0px;
  padding:0.5rem;
}

.container .buttons button{
  background:transparent;
  padding:0.5rem;
}
<div class="container">
    <div class="buttons">
      <button>ex1</button>
      <button>ex2</button>
      <button>ex3</button>
      <button>ex4</button>
      <button>ex5</button>
      <button>ex6</button>
      <button>ex7</button>
      <button>ex8</button>
    </div>
    <canvas id="canvas"></canvas>
   </div>

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

Jasmine - verifying variable invocation in test function

As part of my testing process, I am exploring a scenario where a service method is called using a local variable within an angular controller. In this particular case, when the items array is empty, a modal for creating a new item will be triggered via th ...

What is the best way to automatically increase the value of another column in SQL when the primary key is set to auto

In my database, I currently have a table storing customer details. I am looking to introduce a new column that will provide either existing or new customers with a unique auto-incremented ID for identification purposes. Additionally, I require a primary ke ...

Javascript: recursive function fails to return existing value

I'm attempting to recursively loop through an array in search of a key that matches a specified regex pattern. Once the condition is met, the loop should halt and return the value of the key. The issue I am facing is that although the loop stops corr ...

Ensuring the accuracy of user input in JavaScript, even after fixing a mistake and submitting again, will not cause the code to exit the else clause

I am currently working on a coupon code field that needs to accept a specific set of coupon keys. Below is the code I have written for validation. If a user enters an invalid key at first, they receive an alert saying "Invalid Key." However, if they corr ...

The overflow-anchor property is not effective for scrolling horizontally

My goal is to create a never-ending horizontal scroll that allows users to scroll both left and right. When users scroll to the left, new content should be added to the beginning of the scrollable element (similar to scrolling through a schedule history). ...

What is the best way to refresh a webpage in Vue.js following a specific event or a button click?

Is there a way to reload a webpage only once after clicking a button, without the need for auto-refreshing at set intervals? I've written my code in Vue.js. Can anyone offer guidance on how to accomplish this? Any assistance would be greatly apprecia ...

How can you create an accordion menu that expands when a button is clicked?

Is there a way to make an accordion menu open when the 'More' button is clicked? I've tried, but it always starts in its expanded state and collapses when the button is clicked. What I really want is for the accordion to be closed initially ...

Angular - automated pagination functionality without requiring user input

I apologize for the poorly worded title. Context In my static display angular app, I am not incorporating any user interactions and most actions are time-based. The page loads, and after a specific amount of time determined by certain elements, it reload ...

Using CSS grid can allow for paragraphs to overlap on top of each

https://i.sstatic.net/LVsgQ.jpgCurrently working on constructing a biography page filled with text content, and I aim to utilize CSS GRID instead of floats. I encountered an issue in getting the text to occupy all the available white space within the layou ...

Styling CSS: Adjusting font sizes for placeholders and text boxes

I am struggling with styling an input field to have a font size of 45px while the placeholder text inside it should be 18px and vertically aligned in the middle. Unfortunately, my current code is not working in Chrome and Opera. Can anyone offer a solutio ...

Vue: Opening all GmapInfoWindows simultaneously upon clicking one

I am working on a platform where users can report crimes or incidents by placing markers on a map. These markers with all the reported incidents are then displayed on another map. Each marker has an info window that provides details about the incident and ...

Ways to implement CSS with javascript

I'm using PHP to retrieve data from my database and then leveraging Javascript to enable users to add it. However, I am facing challenges in making a div change its background color and apply borders. <div class="displayedItems"></div> &l ...

Adjustable Scroll Bar

Could anyone help me find a component similar to the resizable scrollbar on Google Finance? Check out this link: http://www.google.com/finance?q=EURUSD&ei=bhM_UKDXF-aIWqAPVNQ You can see it below the chart. If you know where I can find something like ...

Vue.js and TypeScript combination may result in a 'null' value when using file input

I am attempting to detect an event once a file has been uploaded using a file input. Here is the JavaScript code: fileSelected(e: Event) { if ((<HTMLInputElement>e.target).files !== null && (<HTMLInputElement>e.target).files[0] !== null) { ...

Arranging JavaScript object by object properties (name)

Can anyone assist me with my training? I am currently learning JavaScript (Js) and TypeScript (Ts) by working with an external public API. After successfully fetching and displaying my data, I now want to implement sorting functionality. My goal is to sor ...

Toggle button visibility on ng-repeat item click

Hello everyone, I'm encountering an issue with displaying and hiding buttons in ng-repeat. <div class="row" ng-repeat="item in items"> <button type="button" ng-click="add()">+</button> <button type="button" ng-click="remo ...

Tips for resolving the issue of the '$interval is not a function' error in AngularJS

When I click on the up/down arrows, I am attempting to continuously increase/decrease a value using AngularJS $interval function. However, I keep encountering an error message that says "TypeError: $interval is not a function." Can someone please help me s ...

Guide to Deactivating the ENTER Key Functionality in React Material UI Autocomplete Form

My React component features a Material UI Autocomplete form that is working perfectly, except for one issue - when the user hits ENTER, the input field gets cleared. I simply want to prevent the input field from being cleared when ENTER key is pressed. Des ...

Switch up the blogs displayed depending on the category chosen using React

I seem to have hit a roadblock with my current issue. My challenge involves organizing and displaying blog posts according to their categories: const Posts = ({ state }) => { const data = state.source.get(state.router.link); const postsPerCategory ...

Display HTML content retrieved from a string saved in a React database

I am currently in the process of creating a React page and facing a challenge with incorporating HTML content from a RTDB that has been styled using "use styles". Here is an example of the styling: import { makeStyles } from '@material-ui/core/style ...