HTML Canvas Width Specification

Struggling with it is the initial width of my canvas.

My setup consists of a canvas element with an image background set to cover. Using jQuery, the canvas width is set to window.innerWidth while the height is calculated as one-third of the width (to maintain proportionality and avoid cutting off or stretching the background image in this banner).

The issue arises from the canvas starting at 300 x 150 before adjusting to the screen size. This results in the banner loading initially at 300px x 150px and then quickly jumping to the correct size within a second or so.

If you'd like to see an example, check out: CodePen

    function initHeader() {
    width = window.innerWidth;
    height = width / 3;
    target = {
        x: width / 2,
        y: height / 3
};

canvas = document.getElementById( 'canvas' );
canvas.width = width;
canvas.height = height;
ctx = canvas.getContext( '2d' );

Despite trying different CSS adjustments such as setting the canvas width to 100%, I've encountered issues like stretching the banner and creating unwanted gaps below it.

I've exhausted all my ideas and can't seem to find a suitable solution.

Any suggestions would be greatly appreciated!

Answer №1

Is there a delay when resizing images? It might be best to wait until the canvas is resized before initializing it.

canvas.width = width;
canvas.height = height;
canvas.style.backgroundImage = "...."

I hope this information is useful for you.

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

Utilize the $(#id).html(content) method to populate the following column with desired content

Here is a snippet of my HTML code: <div class="row margin-top-3"> <div class="col-sm-7"> <h2>NFTs</h2> <div class="table-responsive"> <table class="table table-bordered&qu ...

Guide to implementing Google Adsense on a page post-load using Angular 6

Hi there, I recently completed my website www.revlproject.org and now I'm trying to get approved for Google Adsense. However, I keep receiving the message "valuable inventory: no content." After some investigation, I realized that because my site is b ...

Is there a way to set an antd checkbox as checked even when its value is falsy within an antd formItem?

I'm currently looking to "invert" the behavior of the antd checkbox component. I am seeking to have the checkbox unchecked when the value/initialValue of the antD formItem is false. Below is my existing code: <FormItem label="Include skills list ...

Using jQuery to validate a form and submit it via Ajax

I have encountered an issue with my validation process. While it works perfectly on a regular PHP form, it seems to only work sporadically on this Ajax form. After clicking the submit button once, the validation fails to run again on subsequent tries. I ...

What steps can I take to resolve a Bootstrap form validation issue where the email field is causing the label to shift down?

I am currently troubleshooting an issue in a website using Visual Studio, which is built with VB.NET, ASP.NET, HTML, CSS, and Bootstrap. The problem lies in the input validation under the email addresses. While one validation is working fine, the validatio ...

Efficiently uploading multiple images with matching names using jQuery and PHP

Looking for a more efficient way to add a large number of images to a website? Is there a method that can automatically find new images based on an incremented file name, like image1.jpg, image2.jpg, and so forth? I typically work with PHP but am open to ...

Advanced filtering of arrays in JavaScript

The data structure I am working with consists of nested arrays of objects, each containing further nested arrays ad infinitum. Imagine deep nesting levels. Let me illustrate my goal with an example. I have a collection of groups. Each group contains heroe ...

Issue with loading STL file in THREE.js: "Uncaught RangeError: offset exceeds the bounds of DataView"

I am currently working on creating a small viewer for stl files using javascript. To achieve this, I am utilizing the library three.js along with the STLLoader module. For file upload functionality, I have integrated an API in node.js which allows me to se ...

Error encountered in jQuery validation: Uncaught TypeError - $(...) ""valid is not a function" is displayed with proper references and order

When I try to call a function, I keep getting an error message that says "Uncaught TypeError: $(...).valid is not a function"... JS $('input[data-val=true]').on('blur', function() { $(this).valid(); }); HTML <div class="col-x ...

Is there a straightforward way to upload a folder in a ReactJS application?

I am searching for a way to upload a folder in ReactJS. I have a folder with .doc and .docx files in it, and I want the user to be able to upload the entire folder when they click the browse button. I need to prevent the user from selecting individual fi ...

Setting the title of the master page body dynamically

Looking for a solution similar to the one discussed below, I am attempting to extract the title entered in the title tag on each page and use it to update an asp label dynamically to display the title on each page. My goal is to achieve this using only C# ...

Warning: multiple selections have been made on the checkboxes

Currently, I am using FormData to submit data and trying to alert the values of checked checkboxes in my form. At the moment, I can only make it work for one checkbox. How can I alert the values of all checked checkboxes? var formData = new FormData(docu ...

The field 'post_id' cannot be identified. Available options include: comment, id, name, post_list

An error occurred while trying to process the request for post number 1. This error is related to a field resolution issue with the keyword 'post_id'. Available fields include: comment, id, name, post_list. The method used was GET and the URL req ...

What causes one CSS file's properties to be inherited by another CSS file in a React application?

I'm puzzled as to why my hero section file seems to be adopting the styling from the navbar CSS file. I do understand that the index.css file sets properties for the entire app, but in my case, I have separate CSS files for different components instea ...

What is the best way to retrieve the array of triangles used to construct the 3D object?

I am trying to retrieve the array of triangles from the geometry object, but I am having trouble locating it. It seems that .faces in the object are not actually triangles. For example, when creating a cube, a face is structured like this: "faces": [{ ...

Performing addition in Angular 2 with the help of TypeScript

Here is a code snippet of a component I have written: export class AppComponent { public num1: number = 2; public num2: number = 3; public sum: number = 0; public add() { this.sum = this.num1 + this.num2; } } However, when I r ...

AJAX and JavaScript outshine Java and Oracle in terms of search performance

My team works with a large enterprise application built in Java that interacts with an Oracle SQL database. We utilize JavaScript on the front end and are constantly seeking ways to enhance the performance of our application as its usage grows. Currently, ...

The Power of AngularJS - Unlocking the Potential of Module Configuration

Exploring the concepts in this AngularJS example: angular.module('myModule', [], function($provide) { $provide.factory('serviceId', function() { var shinyNewServiceInstance; //the factory function creates shinyNewServiceInsta ...

list of key combinations in a ul element

I programmed a list of world states in PHP using an unordered list (<ul>): $WORLD_STATES = array( "France", "Germany", "Greece", "Greenland", "United Kingdom", "United States", "Uruguay" ...

What is the best way to transform the HTML retrieved from an AJAX GET request into an object using JQuery?

$.ajax({ method:"get", url:"/wall", data:"ajax=1", beforeSend:function(){}, success:function(html){ $("#grid_mason").append(html); //Inserting additional co ...