In order to apply both ctx.strokeStyle and ctx.fillStyle at the same time,

Currently, I am engrossed in creating an animation that utilizes the canvas element to fill a rectangle with color based on specific percentages. While I have managed to accomplish this task, I am encountering issues with applying a bottom and right border to the inner square. Despite using ctx.strokeStyle to define the border, it does not seem to work as intended. Any insights or assistance in identifying where I might be going wrong would be greatly appreciated. Thank you!

You can view the functional fiddle here: http://jsfiddle.net/2n6kduL4/22/

HTML Content:

<div id="mydiv">
    <canvas id="Rectangle1" width="100" height="100" style="border:1px solid #d3d3d3;background:#7392a8;">Your browser does not support the HTML5 canvas tag.</canvas>
    <canvas id="Rectangle2" width="100" height="100" style="border:1px solid #d3d3d3;">Your browser does not support the HTML5 canvas tag.</canvas>
    <canvas id="Rectangle3" width="100" height="100" style="border:1px solid #d3d3d3;">Your browser does not support the HTML5 canvas tag.</canvas>
    <canvas id="Rectangle4" width="100" height="100" style="border:1px solid #d3d3d3;">Your browser does not support the HTML5 canvas tag.</canvas>
</div>

JQUERY Content:

(function(){
   for (j = 1; j < 5; j++) {
       var myTime = {};
        myTime[1] = 0.5;
        myTime[2] = 0.9;
        myTime[3] = 0.1;
        myTime[4] = 0.5;
       (function(){
           var canvas = document.getElementById('Rectangle' + j);
       //console.log(canvas);
       //var ctx = document.getElementById(canvas)[k].getContext('2d');
       var ctx =canvas.getContext('2d');
       //console.log(ctx);
       var myPerc = 100;
       ctx.fillStyle = "rgb(255, 255, 255)";
       ctx.fillRect(0, 0, myPerc, myPerc);
       ctx.save();
       for (var i = 0; i < (myPerc * myTime[j]); i++) {            
           ctx.fillStyle = "rgb(0, 255, 0)"; //greeen
           ctx.strokeStyle = 'rgb(0,0,0)';
           ctx.lineWidth   = 4;
           ctx.stroke();
           console.log("before", ctx);
           // ctx.fillRect(0, 0, +i, +i);
           (function (i) {                 
               setTimeout(function () {
                   console.log(ctx, j);
                   ctx.fillRect(0, 0, +i, +i);
               }, 50 * i);
           })(i);
       }
       })();
   }
})();

Best regards, Pravallika

Answer №1

When using the fillRect function, keep in mind that it only fills in the rectangle without drawing a border around it. To add a border, simply include a call to the strokeRect function after filling it.

http://example.com/code-example

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 implementation of classList.toggle in my JavaScript code is not functioning as expected

I created a button that toggles the visibility of a div. It works fine the first couple of times, but then it stops working. When I click on column1, the hidden div inside column1 doesn't appear. But when I click on column2, the hidden div inside colu ...

After receiving the response, the Ajax success function does not run as expected

I have written the following code: $(".simpleCart_shelfItem button").click(function() { $(this).prop('disabled', true); $(this).addClass('disabled'); $(this).html("Adding &nbsp;&nbsp;<i cl ...

Prevent jQuery mouseover from hiding an element when the mouse is hovering over a popup window

I am working on a project where I need popup windows to appear when the mouse hovers over certain elements for a few seconds. I have managed to achieve this using the script below: var timeOutUserInfo; $('a[datatype=popupAboutUser]').live({ ...

Unable to set DIV width to 100% and show a scrollbar

I am facing an issue with the width of a DIV element on my webpage. Despite setting it to 100% width, it only takes up the visible window's width and not the full page width, especially when a horizontal scroll bar is displayed. Below is the HTML cod ...

Ways to disable an element

How can I prevent a <form:input type="text" path="lateTimeValue" disabled="true" id="lateTime" /> element from sending its value to the server when disabled? What steps should I take to ensure the value is not passed to the server upon submissio ...

Adding extra space to the list items in CSS causes the text to become static and unaffected by fluctuations in the line-height

Here's the problem I'm facing: I have a basic list consisting of a shopping cart, login and user registration. I want to adjust the position of the list by adding padding, but every time I do so, the line height also increases. Is there a workaro ...

The Material UI ToolTip displays inaccurately when placed within a container that has overflow scroll enabled

Within my ReactJS application, I have implemented a list of Material UI ToolTips with IconButtons nested inside a div element featuring overflow: scroll. One specific row displays the Material UI ToolTip in the following manner: <ClickAwayListener onCl ...

Nested Tab Generation on the Fly

My goal is to create dynamically nested tabs based on my data set. While I have successfully achieved the parent tabs, I am encountering an issue with the child tabs. Code $(document).ready(function() { var data1 = [["FINANCE"],["SALE"],["SALE3"]]; var da ...

Guide on implementing gradient animation effects in a React component

How can I implement gradient animation effects like this example in a React component using inline CSS? I need to utilize the CSS-based gradient animation effects shown below directly within the React component. Are there any specific packages available ...

Graphical representation in the body of an email using bar graphs

Can a dynamic bar graph be created in an email message? It should be compatible with Outlook. I want to include a graph in an email sent using oracle database, where the dynamic values will be passed through a procedure. ...

Angular 4.3 - Best Practices for Utilizing Enums in a Nested Component

Today, I encountered an issue trying to access the selected value from an enum in my contact.component file. This enum is a part of a form. Here is how I set up the enum: contact.component.ts (extract of relevant code only) import { Component, OnI ...

What could be causing the side margins on my navbar in mobile view in Bootstrap?

After spending some time working on a simple navbar with CSS styling, I encountered an issue when viewing it in mobile mode. The navbar did not expand to 100% of the screen width and had a margin of about 20px on both sides. CSS .navbar .brand { wi ...

Implement Offcanvas feature with Bootstrap 3 Navbar set to Fixed Top position

I am in the process of creating a website that will feature a large menu. However, I am not a fan of the collapsed menu that comes with BS3. Instead, I would like to implement a drawer or off-canvas menu similar to the one showcased in BS3's offcanvas ...

Child element's CSS is failing to apply, while the parent element's styling is functioning correctly

I have developed a small quiz application where, after answering questions, I display a progress bar. The issue I am facing is with the styling of the progress bar. I have set up two div elements for this purpose - one as the parent element (id = "progress ...

Utilizing jQuery to apply a class to a text element within a div

I have a container with the id 'usercontainer' that will display individual names. My goal is to add a specific class to each name so that hover and click functions can be applied later on. However, my current method is only adding the 'name ...

Stop navigation links from resizing when the page content does not fill the height of the display

As I work on my very first website, I've encountered a strange issue with the navigational links at the top of each page. I've noticed that pages with content exceeding the maximum vertical pixels occupy a specific width, while those with less co ...

Height of the Accordion List

I have a code snippet below for an accordion list that I put together. I initially set the height for all the heading boxes to be uniform, but it seems like box A is displaying larger than the others. Can you help me figure out what went wrong? Any suggest ...

Eliminating the muted attribute does not result in the sound being restored

I am looking to implement a feature where a video loads automatically without sound, but when a user clicks a button labeled "Watch with Sound", the video restarts from the beginning and plays with sound. Below is the JavaScript code: let videoButton = do ...

Step-by-step guide on completing and submitting a form through a Windows application

Many are questioning the age and repetition of this query, but here is where my issue begins. Are you noticing two input fields with the same name? HTML CODE <html> <head><title></title> </head> <body> <input type= ...

Updating serialized $_POST array with new key/value pair using jQuery AJAX

Is there a way to insert additional values into a serialized $_POST array before sending an AJAX request using jQuery? Here's the situation: $('#ajax-preview').on('click', function(e) { e.preventDefault(); var formData = ...