Tips for adjusting the size of an HTML 2D canvas when it changes during animation

I am striving to create an HTML/CSS layout that utilizes flexbox to fill available space with various elements, while also maximizing the size of a 2D canvas within that space.

Although CSS flexbox can help achieve this desired layout, there is a challenge when it comes to resizing the canvas element. The canvas backing store needs to be redrawn in order to prevent distortion, such as stretching a low-resolution image too far or compressing a high-resolution image and losing details.

The provided code snippet (available on jsfiddle) comes close to achieving the intended goal. However, the canvas does not update its size dynamically when CSS3 animations are applied.

HTML:

<button>I am a button</button>
<canvas id="c"></canvas>
<p>Some text.</p>

CSS:

body {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    flex-direction: column;
}
canvas {
    flex: auto;
    align-self: stretch;
}
button {
    align-self: center;
    transition: padding 2s;
}
button:hover {
    padding: 3em;
}

Javascript:

function redraw() {
    var cc = c.getContext("2d");
    c.width = c.clientWidth;
    c.height = c.clientHeight;
    cc.scale(c.width, c.height);
    cc.beginPath();
    cc.arc(0.5, 0.5, .5, 0, 2 * Math.PI);
    cc.fill();
}

// Update canvas on window resize.
window.addEventListener("resize", redraw);

// Initial drawing of the canvas.
redraw();

Answer №1

You can utilize the animationiteration event for that purpose. According to an article found on sitepoint (currently unavailable), this event is supported across all browsers with one of these names:

animationiteration
webkitAnimationIteration
oanimationiteration
MSAnimationIteration

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

The custom classes I have created in Material UI are being overshadowed by the default classes provided by Material UI

I am trying to change the color of a TextField label to black when it is focused. I used classes in InputProps with a variable, but unfortunately, the default styling of material UI is taking precedence in chrome dev tools. Below is the code snippet. Pleas ...

The button appears to be unresponsive

I've been struggling to figure out why the 2-minute/5-minute buttons are not working as expected. Despite identifying some mistakes in my code, nothing seems to change when I test it. There are no error messages either, so I'm hoping someone can ...

The issue of excess space surrounding images while utilizing Bootstrap 3

Hey there, I'm currently working on a website using Bootstrap 3 framework and I've run into a little snag. Whenever I add an image (just a regular one with an img tag), it seems to have some white space on both the left and right sides of the scr ...

How can I access the fourth div element within the initial row of a multirow div container?

My current HTML structure is causing me some confusion. I am trying to create a CSS selector for the element with the text "DESIRED ELEMENT": <div class="TopDiv"> <div class="container"> <div class="row"> ...

Is it possible to create a carousel using PHP and MySQL?

Is it really impossible? I'm determined to pull images from a MySQL database and create a carousel similar to the one showcased in this link: . However, my goal is to achieve this using only HTML, CSS, PHP, and MySQL. ...

How can I vertically and horizontally center elements in the middle of a web page using ASP.NET and CSS?

Creating a basic web form can be easy. It must contain three lines of text/ASP.NET elements within a Master page that includes a header and footer. To ensure that these three lines are centered both vertically and horizontally on the page, especially whe ...

The JAWR generator is having trouble locating the jawr_generator.css file when using Less to compile it

I'm encountering some issues while trying to integrate JAWR with the generator configuration for converting less into CSS post compile. When I attempt this, I consistently run into errors where it's looking for a jawr_generator.css file that I be ...

How you can fix the issue of the "Element not visible" error occurring specifically for one element within the popup

Error Message: "Element Not Visible" when Script Reaches Last Element Despite all attributes being in the same form, an error occurs when the script reaches the last element and displays "element not visible." All elements are enclosed in div tags on the ...

Having trouble resolving row border issues with CSS in DataTables?

I need to enhance the appearance of specific rows in my table by adding a darker border at the bottom. To achieve this, I have assigned a custom class (des-rec-row) to the rows and included the following CSS code: .des-rec-row { border-bottom: 1px soli ...

When hovering over items in the navigation bar, the entire bar expands seamlessly

I've spent countless hours trying to troubleshoot an issue with my dropdown menu that expands whenever I hover over it. The bar stretches out to accommodate the list of dropdown contents and then reverts back to normal when I move my cursor away. My a ...

Difficulties with Bootstrap, JQuery, HTML, and CSS

Currently in the process of revamping a university society's website, I have some experience in web development but nothing too advanced. You can view the current website here: . Here is a sneak peek at the new design I am working on: . However, I& ...

Looking to clean up unnecessary <b></b> tags from my Wordpress website - how can I do this?

One of my sites is located at . When inspecting the source code through the browser, I noticed the presence of empty tags. Does anyone know why these empty tags are being generated and how they can be removed? I have thoroughly searched through all files ...

The page numbers on the GridView are displayed in distant columns, appearing far apart from

My gridview is displaying page numbers in separate columns instead of together in the center. How can I bring the page numbers together in the center? <asp:GridView ID="GridView1" runat="server" Width="100%" OnRowCommand="GridView1_RowCommand" CssClas ...

Difficulty encountered when trying to customize a polymer element that has been expanded (paper-slider)

I've been customizing the Polymer paper-slider element to handle an enumerated list and cycle through these values in the slider instead of just showing numeric values. However, I'm struggling with getting the styles to align properly. When you r ...

The Proper Usage of Commas in CSS

Check out this CSS code snippet. .form-field {min-height: 20px; margin-bottom: 10px; padding-top: 4px; width: 80px;} .form-field TEXTAREA, INPUT[type='text'] {position: absolute; left: 100px; top: 0px; height: 15px;} .form-field TEXTAREA {height ...

What is the best way to ensure the text in my paragraph div is aligned to the left and displays in a vertical line?

I am struggling to align the words in my paragraph to the left in a straight line, instead of the zig-zag placement they currently have. Any guidance on achieving this would be greatly appreciated. /* Styling for tablet-sized screen*/ @media (min-width ...

When the browser window is resized to mobile view, a div is overlapped by an image

I've encountered an issue with the image size and position when resizing to mobile view in the browser. .extension { display: table; padding: 50px 0px 50px; width: 100%; height: auto; color: #fff; background-color: #558C89; ...

What is the best way to conceal floated elements with overflow in CSS?

How can I ensure that my block container element only displays the content of block elements, cutting off any floated elements that are taller than the parent container? Applying overflow: hidden seemed like a simple solution, but it created a new block f ...

Tips for positioning spans or text within a paragraph from the right to left direction

I've been trying to find a simple CSS solution that will allow me to start the content of a paragraph from the end. I've experimented with properties like text-align, text-align-last, text-orientation and others, but haven't had any luck so ...

Changing the width of the file input using css

Clicking just below the word demonstration also triggers a click on the input type file. How can this be avoided so that the click event only fires in the intended area regardless of size? <!DOCTYPE html> <html> <body> <style> in ...