Guide on positioning the canvas using p5.js

With just an hour of HTML and CSS experience under my belt, I have a much deeper understanding of programming languages like JavaScript. So please bear with me as I navigate through creating a p5 canvas that follows my CSS styling.

How can I achieve this?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My website!</title>
    <link rel="stylesheet" href="main.css">
</head>
<body>
    <div class="container">
        <h1>My Website</h1>
        <h2>It is all coming together.</h2>

        <script type="text/javascript" src="p5.js"></script>
        <script type="text/javascript" src="my_code.js"></script>
    </div>
</body>
</html>

Here's a snippet of my CSS:

body {
    background-image: url("matt-walsh-tVkdGtEe2C4-unsplash.jpg");
    background-repeat: no-repeat;
    background-size: cover;
}

.container {
    background-color: hsla(0, 0%, 50%, 0.8);
    height: auto;
    width: fit-content;
    padding: 0px 250px;
}

h1 {
    text-shadow: -2px 0 black, 0 2px black, 2px 0 black, 0 -2px black;
    font-size: 10vw;
    font-family: Arial;
    color: white;
    text-align: center;
}

h2 {
    text-shadow: -2px 0 black, 0 2px black, 2px 0 black, 0 -2px black;
    font-size: 5vw;
    font-family: Arial;
    color: white;
    text-align: center;
}

p {
    font-family: Arial;
    color: black;
    text-align: left;
    max-width: 200vw;
}

script {
    alignment: center;
}

And to wrap things up, the setup and draw functions for the canvas:

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

function draw() {
    if (mouseIsPressed) {
        fill(0);
        ellipse(mouseX, mouseY, 80, 80);
    }
}

This creates the following page: https://i.sstatic.net/s5XKD.jpg

Answer №1

To include your canvas within a div in your HTML, you can start by creating the necessary structure:

<body>
    <div class="container">
        <h1>My Website</h1>
        <h2>It is all coming together.</h2>

        <div id="canvasDiv"></div>
    </div>
</body>

In your JavaScript's setup() function, you can then create the canvas and place it inside this designated div:

const myCanvas = createCanvas(400, 400);
myCanvas.parent('canvasDiv');

This approach allows you to style the containing div using CSS for flexibility in positioning:

#canvasDiv {
    // define your canvas styling here
}

Adjusting the canvas position becomes as simple as manipulating the div placement in your HTML code. For guidance on various positioning techniques, there are plenty of resources available online.

Furthermore, regarding @Georges' comment, the following part in your CSS file doesn't serve any purpose:

script {
    alignment: center;
}

Trying to align script tags is unnecessary since they are used solely for sourcing JavaScript and not displayed elements on the page. This styling does not impact HTML elements generated or managed by your JavaScript code, rendering it redundant.

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

Is there a way to modify the props.data in a child component?

I need assistance with displaying a range from an array that is passed into a child component. Currently, my Parent component looks like this: import data from './data.json' return ( <Cell symbol={data.symbol} number={data.number} /> ) ...

Steps to create interconnected circles using CSS

Currently, I am attempting to create a set of steps using only CSS. Here is what I have achieved so far: https://i.sstatic.net/sCRzr.jpg However, I am facing an issue with removing the excess line where the red mark is located. <ul class="step&q ...

Conceal a row in a table using knockout's style binding functionality

Is it possible to bind the display style of a table row using knockout.js with a viewmodel property? I need to utilize this binding in order to toggle the visibility of the table row based on other properties within my viewmodel. Here is an example of HTM ...

Retrieve postal code using longitude and latitude data from Google's API

I am currently working on a project that requires obtaining a postcode from longitude and latitude coordinates. Utilizing the Google Maps API, I'm able to retrieve the longitude and latitude based on a search term like 'Manchester'. After t ...

Locating if a certain string in an array is even or odd in JavaScript

I am currently on a mission to locate an odd string within a given array. Here's the code: const friendArray = ["agdum", "bagdum", "chagdum", "lagdum", "jagdum", "magdum"]; function findOddString(arr) { for (let i = 0; i < arr.length; i++) { ...

Turning $.post into $.ajax: A step-by-step guide

I need help with converting my $.post code to $.ajax. Here is the code snippet: $.post("../admin-login", { dataName:JSON.stringify({ username:uname, password:pass, }) }, function(data,status){ console.log("Data:"+data); answer = data; ...

Creating personalized tags or components by utilizing insertAdjacentHTML in Vue

With the use of insertAdjacentHTML, I am able to create HTML elements such as div and span, but it seems that I cannot generate custom tags like <myComponent> </myComponent>. I understand that insertAdjacentHTML can create HTML elements, but a ...

Displaying or concealing HTML elements using AngularJS while a modal is open

Looking for a way to display a loading spinner icon on my page when a user triggers a button that opens a modal, and then have the spinner disappear once the modal is open and its content has loaded. Currently, I've managed to make the spinner show up ...

How does Python interpret arrays from JavaScript?

As part of my API setup, I am sending parameters to a python script. One of these parameters happens to be a JavaScript array. Interestingly, when I check the array in Python, it only shows the first index. Here is the snippet of my Angular JS get request ...

What is the reason that a div can accept both before and after selectors, while an img tag cannot have this capability

What is the reason behind a div being able to accept before and after selectors, while an img tag cannot? How can I effectively define two pseudo elements for my image? ...

Retrieve data from Last.fm API by utilizing both Node.js and Angular framework

I am currently working on implementing the node-lastfmapi track.search method into my project. I have successfully retrieved the results, but I am facing challenges in integrating them into the front end using Angular. My backend is powered by mongoDB and ...

Building basic objects in TypeScript

My current project involves utilizing an interface for vehicles. export interface Vehicle { IdNumber: number; isNew: boolean; contact: { firstName: string; lastName: string; cellPhoneNumber: number; ...

What is the best way to dynamically insert a directive element between an already existing parent directive and its child directive?

Background: Working within the constraints of a custom CMS that limits access to the code base, I am exploring the option of using JavaScript for DOM manipulations in certain scenarios. Problem: The issue I am facing involves a container directive conta ...

Converting HTML text input into a PHP variable

How can I achieve a html text input with name="input1" to be a php variable named $value? Here is what I have tried: <html> <form action="added.php" method="post" /> <p>Send a message to JANNES database: <input type="text" name="i ...

What is the best way to ensure the content div fills all available space? (JQM compatibility concerns)

Currently, I am facing an issue with displaying a pdf in an iFrame within my app. The problem lies in the fact that the iFrame does not occupy the entire space between the header and footer divs on the page. I am utilizing Jquery Mobile 1.1.0 for the theme ...

Using a background image in the navigation menu with CSS

Looking to create a horizontal menu with a responsive background image. Struggling with the issue of the 'ul' menu being misaligned with the background image when in full-screen mode. Attempted to use separate 'div' containers for the ...

Can TypeScript support the use of typed streams?

When working with TypeScript and using the typings from @types/node, streams default to using the any type. While this may suffice in most cases, it is not the most optimal approach as data emitted through the data event typically has a consistent type, es ...

Crop images in a canvas using a customized rectangle with the help of JQuery

I am trying to crop an image inside a Canvas element using a selection rectangle. My project utilizes jQuery and I am in search of a plugin that can help me implement this custom selection rectangle on the canvas itself, not just on images. Are there any ...

Can a software be created to capture search results from the internet?

Is it feasible to create a program that can extract online search results? I am specifically interested in retrieving data from Some of the data I need include application numbers, such as 9078871 and 10595401 Although there are CAPTCHAs present, I am w ...

What is the best way to update HTML element contents with new code without losing any associated JavaScript functions?

I am attempting to replace the content within $('#product_blocks') with new HTML while also maintaining the jQuery event listeners on a similar element ID. var newHtml= '<div id="clickme">hello this text will be replaced on click</ ...