Different ways to adjust the positioning of the HTML canvas created using p5.js

I am attempting to integrate the output canvas from p5.js into my webpage by following a tutorial heretutorial. I am hoping that the canvas will appear where I have positioned it within the HTML body. However, no matter what I try with CSS properties like position and display, it always ends up at the bottom of the page. Even when I attempted to place the script inside the body tag, the same issue persisted. Additionally, if multiple scripts are included, the canvases start overlapping.

Below are some example code snippets:

index.html

<!doctype html>
<html>
  <head>
    <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b6b2e5b2a352e352b">[email protected]</a>/lib/p5.min.js"></script>
    <script src="test.js"></script>
  </head>
  <body>
    <h1>Hello World</h1>
    <p>This is a sentence</p>
</body>
</html>

test.js

function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(220);
  circle(200,200,50,50);
}

Answer №1

If you need to reposition the canvas, utilizing CSS is the way to go. To achieve this, simply place the canvas within a parent HTML element and apply CSS styles accordingly.

I came across an effective method for accomplishing this here:

Start by storing the declared canvas as a variable

function setup() {
    var canvas = createCanvas(400, 400);
    canvas.parent('canvas'); //'canvas' refers to the id of the desired HTML element
}

Next, insert the following div below while assigning the class as canvas-body

<div class="canvas-body" id="canvas"></div>

Finally, in your CSS stylesheet, define the styling for canvas-body

.canvas-body {
  text-align:center;
  /* text-align can be used for centering, but explore other alignment options */
}

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

I can't see any tools anywhere on my page in Literally Canvas

My Implementation: <!DOCTYPE html> <html lang="en"> <head> <title>Creative Drawing Tool</title> <link href="css/creative-drawing-tool.css" rel="stylesheet"> <script src="jquery-2.1.4.min.js"></scrip ...

JavaScript recording speed

I am currently working on a project that involves video recording on my website. After creating a canvas and pushing the recorded frames to it, I have encountered an issue. The playback of the video is too fast - a 10-second video plays in around 2 second ...

Tips for customization: Altering HTML table borders and column widths

I am looking to update the appearance of an HTML table. Currently, it looks like this: https://i.sstatic.net/pueyW.png Here is the code snippet: <div className="student-schedule"> <table className="student-calendar-table" ...

Vue-Router functions only on specific routes

While my vue-router correctly routes the URL for all menu buttons, it's not displaying each Vue component properly. You can see a demonstration here. Here is a snippet of my HTML (using Vuefy) <div class="navbar-start"> <a ...

The code is triggering IE 8 to switch to compatibility mode resembling IE7

I have created a custom javascript function that generates a pop-up, and I am invoking this function in my code. However, every time I click the button, the browser switches to IE 7 compatibility mode and the pop-up appears behind the button. Below is my ...

Issue with Material-UI and React: Changes not visible after using <ThemeProvider>

I've encountered an issue where the "ThemeProvider" tag does not seem to be causing any changes in my project, even when following a simple example like the one shown below. Despite having no errors or warnings in the browser console (except for some ...

What is the best way to incorporate both images and text in PHP code?

I'm currently working on creating a large image call-to-action (CTA) for my new WordPress website. I've set up a new field group with the type "group" in ACF, and added some functions in PHPStorm. However, none of my images, text, or links are ap ...

Utilizing AJAX, a PHP script creates an HTML table that allows users to easily perform actions such as deleting, updating

I'm a beginner in the world of ajax and I am eager to utilize it with php in order to develop a table that can be edited by users directly on the webapp interface. Here's my current progress... PHP class genTable{ public function table1(){ ...

Implementing custom fonts in Next.js using next-less for self-hosting

Seeking Solutions for Hosting Fonts in Next.js Application I am exploring the idea of self-hosting a font, specifically Noto, within my Next.js application that already utilizes the @zeit/next-less plugin. Should I rely on the npm package next-fonts to h ...

Blurring the background creates an "inset shadow" problem when transitioning

Problem: Strangely, when using Backdrop-filter: blur(Xpx); during a transition as shown below, unexpected "inset Shadows" mysteriously appear until the end of the transition. https://i.stack.imgur.com/uij7F.gif Illustration of the Issue Using jsfiddle Sp ...

Steps to integrate the Save as PNG functionality in Vega using a customized menu

As I develop a data dashboard with Vega for visualizing outputs, I decided to customize the menu system by removing the actions dropdown. However, I still want to incorporate the "Save as PNG" option from the original dropdown (as shown in the image below) ...

Refreshing Angular Page

I'm looking for a way to reset my Angular page back to its original state with just one button click. I attempted to use the angular.copy method, but encountered an error. I have various scope and controller variables that I don't want to reset i ...

Struggling to make EJS button functional on the template

I am currently facing an issue with a loop that populates a webpage with multiple items, each containing an image, text, button, and a unique ID associated with it. Although I have written a function to retrieve the ID and plan name when the button is clic ...

Update the background color on the sidebar

Objective: I want to change the background of the class="col-sm-3 blog-sidebar" to match the color shown in the image below. Essentially, I am looking to update the sidebar background to reflect the picture's color. Challenge: How can this be ach ...

Is it possible for amCharts to show the data properly?

Starting out with amCharts and javascript can be a bit overwhelming. Here is the structure of my html file: <!DOCTYPE html> <html> <head> <link rel="shortcut icon" href=""> <title>chart created with amCharts | amChar ...

The messaging feature is not functioning properly within the page tab

The iframe page tab is not compatible with the Send dialog. I followed the example on the 'Send Dialog' page https://developers.facebook.com/docs/reference/dialogs/send/ <html xmlns:fb="https://www.facebook.com/2008/fbml"> <body> ...

Resetting component state in React Native is essential for maintaining the correct

I need to reset the state of specific states without affecting others. When a button is clicked, the values of present_count, total_count, present, and total should all be restored to their original state (0), while keeping the state of subjects and text u ...

Angular Unit testing error: Unable to find a matching route for URL segment 'home/advisor'

Currently, I am working on unit testing within my Angular 4.0.0 application. In one of the methods in my component, I am manually routing using the following code: method(){ .... this.navigateTo('/home/advisor'); .... } The navigateTo funct ...

What is the method for incorporating PHP's header() function within PayPal JavaScript code?

I'm currently working on integrating Paypal with my website and I've run into some issues handling the various messages sent by Paypal, such as success, failed, and cancelled transactions. Below is a snippet of the Paypal JS code where it manage ...

How can I modify the container to prevent the Bootstrap dropdown from being clipped by overflow:hidden settings?

With bootstrap, I encountered a situation where dropdown menus are being clipped by my <div> with overflow: hidden. How can I address this issue without incurring high costs? One potential solution might be to change the container of all dropdowns wi ...