Altering the hue of various stroke patterns

I've encountered an issue where I want to create skill bars that display my skills in percentages. Luckily, I found a great code that allows me to do this. Although I would prefer to do it myself, I haven't come across a good tutorial that teaches me how to (if you know one, please share).

Now, my question is how can I modify the colors of these different strokes?

self.context.clearRect( 0, 0, self.width, self.height );
        self.context.lineWidth = 10;
        self.context.fillStyle = "#000";
        self.context.strokeStyle = "#666";
        self.context.textAlign = "center";

You can find the code I found in the link below.

http://codepen.io/gabrieleromanato/pen/OVprOW

Answer №1

To change the color of elements on your page, comment out the line

self.context.strokeStyle = "#d30000";
. Then, assign an id to each element and set the desired color using
document.getElementById("given-id").getContext( "2d" ).strokeStyle = "#desired-color";
. See example below:

/* Modify the color of elements on a web page */
document.getElementById("csscan").getContext( "2d" ).strokeStyle = "#d30000";
document.getElementById("html5can").getContext( "2d" ).strokeStyle = "blue";
(function() {

var Progress = function( element ) {

this.context = element.getContext( "2d" );
this.refElement = element.parentNode;
this.loaded = 0;
this.start = 4.72;
this.width = this.context.canvas.width;
this.height = this.context.canvas.height;
this.total = parseInt( this.refElement.dataset.percent, 10 );
this.timer = null;

this.diff = 0;

this.init();
};

Progress.prototype = {
init: function() {
var self = this;
self.timer = setInterval(function() {
self.run();
}, 25);
},
run: function() {
var self = this;

self.diff = ( ( self.loaded / 100 ) * Math.PI * 2 * 10 ).toFixed( 2 );
self.context.clearRect( 0, 0, self.width, self.height );
self.context.lineWidth = 10;
self.context.fillStyle = "#000";
//self.context.strokeStyle = "#d30000";
self.context.textAlign = "center";

self.context.fillText( self.loaded + "%", self.width * .5, self.height * .5 + 2, self.width );
self.context.beginPath();
self.context.arc( 35, 35, 30, self.start, self.diff / 10 + self.start, false );
self.context.stroke();

if( self.loaded >= self.total ) {
clearInterval( self.timer );
}

self.loaded++;
}
};

var CircularSkillBar = function( elements ) {
this.bars = document.querySelectorAll( elements );
if( this.bars.length > 0 ) {
this.init();
}
};

CircularSkillBar.prototype = {
init: function() {
this.tick = 25;
this.progress();

},
progress: function() {
var self = this;
var index = 0;
var firstCanvas = self.bars[0].querySelector( "canvas" );
var firstProg = new Progress( firstCanvas );

var timer = setInterval(function() {
index++;

var canvas = self.bars[index].querySelector( "canvas" );
var prog = new Progress( canvas );

if( index == self.bars.length ) {
clearInterval( timer );
} 

}, self.tick * 100);

}
};

document.addEventListener( "DOMContentLoaded", function() {
var circularBars = new CircularSkillBar( "#bars .bar" );
});

})();
#bars {
margin: 2em auto;
max-width: 960px;
overflow: hidden;
}

.bar {
float: left;
width: 20%;
text-align: center;
}

.bar h3 {
font-size: 1.4em;
font-weight: normal;
margin: 0;
text-transform: uppercase;
}

.bar-circle {
display: block;
margin: 0.7em auto;
}
<div id="bars">
<div class="bar" data-percent="100" >
<h3>CSS</h3>
<canvas class="bar-circle" width="70" height="70" id="csscan"></canvas>
</div>
<div class="bar" data-percent="100">
<h3>HTML5</h3>
<canvas class="bar-circle" width="70" height="70" id="html5can"></canvas>
</div>
<div class="bar" data-percent="100">
<h3>JavaScript</h3>
<canvas class="bar-circle" width="70" height="70"></canvas>
</div>
<div class="bar" data-percent="100">
<h3>PHP</h3>
<canvas class="bar-circle" width="70" height="70"></canvas>
</div>
<div class="bar" data-percent="80">
<h3>Server</h3>
<canvas class="bar-circle" width="70" height="70"></canvas>
</div>
</div>

Answer №2

If you want to initialize the function, you simply need to provide some arguments when calling the init method.

I made changes to your Pen

init: function(fillStyle, strokeStyle) {
        var self = this;
        self.context.fillStyle = fillStyle,
        self.context.strokeStyle = strokeStyle,
        self.timer = setInterval(function() {
        self.run(); 
    }, 25);
}

Now, when calling the init function, make sure to pass in 2 parameters for strokeStyle and fillStyle.

this.init('#0F0', '#F00');

Update: I have made further adjustments to the pen by incorporating random colors for a more visually appealing effect.

Answer №3

To manipulate the color attribute, you can follow a similar approach as with the width and height attributes. Simply store the color value within an attribute on the canvas element, then retrieve that value within the progress function.

Explore this example on CodePen

<canvas class="bar-circle" color="blue"  width="70" height="70"></canvas>

this.color = element.getAttribute('color') || "#d30000"; //default

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

What is the method for asynchronously loading a javascript file that includes an ajax call?

Within my JavaScript file named JScript.js, there is a function that includes an AJAX call to a dot-net page. alert('jscript.js called'); function AddTag() { var htag = document.getElementById("hdntag").value.split('|'); var texttag = ...

Sending a personalized event to a JavaScript object

I am looking to create an object Constructor that can dispatch a customEvent, which I then want to listen to from the instance of the object. The code snippet provided showcases what I have been attempting. I would appreciate any assistance on how to mak ...

CSS media query to target specific viewport width

In my JavaScript code, I am dynamically creating a meta viewport. I set the value of this viewport to be 980px using the following script: var customViewPort=document.createElement('meta'); customViewPort.id="viewport"; customViewPort.name = "vie ...

Using JavaScript to Redirect to Homepage upon successful Ajax response on local server

I need assistance with redirecting to the Homepage from the SignIn Page once the user credentials have been validated. The response is working correctly, and upon receiving a successful response, I want to navigate to the Homepage. My setup involves Javasc ...

What is the best way to organize class usage within other classes to prevent circular dependencies?

The engine class presented below utilizes two renderer classes that extend a base renderer class: import {RendererOne} from "./renderer-one"; import {RendererTwo} from "./renderer-two"; export class Engine { coordinates: number; randomProperty: ...

Utilize Express.js to seamlessly stream and process uploaded files with formidable and gm, ensuring a streamlined process without

I am looking for a solution to upload and resize an image in one step without having to write to disk twice. For uploading images, I am using: node-formidable: https://github.com/felixge/node-formidable And for resizing the images, I am using: gm: Howev ...

Translating Encryption from Javascript to Ruby

I have an application which utilizes HTML5 caching to enable offline functionality. When the app is offline, information is stored using JavaScript in localStorage and then transmitted to the server once online connectivity is restored. I am interested in ...

The Angular overlay is concealed beneath the pinned header

I am seeking a solution to have a mat-spinner displayed on top of my app while it is in the loading state. Currently, I am using an overlay component with Overlay from @angular/cdk/overlay. The issue arises when part of the spinner that should be overlai ...

Ways to eliminate the border color from bootstrap multiselect

I have been attempting to eliminate the border color surrounding options in a select box from Bootstrap Multiselect. Unfortunately, I am unable to identify the specific class responsible for adding the blue color around the option borders. This border app ...

Having trouble with jQuery.cookie saving my JSON data

Being new to jQuery and JSON, I find this situation quite frustrating as it seems pretty straightforward but still doesn't work for me. My ASP.NET MVC website is using AJAX (via jQuery) to load data. Everything works fine until this point. I'm ...

Angular Checkbox Single Select

I have a piece of HTML code with ng-repeat that includes checkboxes. <table> <tr ng-repeat="address in contactInfo.Addresses"> <td>{{address.DisplayType}}</td> <td>{{address.AddressLine1}}</td> <td>{ ...

Emphasize sections of text within a chart

Looking for a Specific Solution: I've encountered similar problems before, but this one has a unique twist. What I'm trying to achieve is to search for a substring within a table, highlight that substring, and hide all other rows (tr's) th ...

Utilizing the Grid-A-Licious plugin multiple times within a single webpage

Currently, I am utilizing the Grid-A-Licious plugin for my project. It functions perfectly when used on a single page with one call to the plugin. However, due to my design requirements, I need to implement it within 3 tabs on the same page. Unfortunately, ...

Using Selectpicker with Jquery .on('change') results in the change event being triggered twice in a row

While utilizing bootstrap-select selectpicker for <select> lists, I am encountering an issue where the on change event is being triggered twice. Here is an example of my select list: <select class="form-control selectpicker label-picker" ...

What could be the reason for the checkbox not being selected in a React component

I'm currently working on integrating an autocomplete feature with checkboxes. Learn more here https://i.stack.imgur.com/YtxSS.png However, when trying to use the same component in final-form, I'm facing issues with checking my options. Why is t ...

Issue arose following implementation of the function that waits for the event to conclude

I've encountered a problem with my HTML and jQuery Ajax code. Here's what I have: <form> <input name="name_field" type="text"> <button type="submit">Save</button> </form> $(document).on("submit", "form", fu ...

Troubleshooting: Else block not functioning as expected within a React JS map function

I have a notification feature that makes an API call every 10 seconds to display an alert based on the response. However, I'm encountering an issue where the div is not being rendered properly. The div should be displayed based on certain conditions w ...

Filter error - Unable to retrieve property 'toLowerCase' from null value

When filtering the input against the cached query result, I convert both the user input value and database values to lowercase for comparison. result = this.cachedResults.filter(f => f.prj.toLowerCase().indexOf((this.sV).toLowerCase()) !== -1); This ...

jQuery element with listener not triggering as expected

I'm facing some confusion while working on this issue. I am attempting to create a dynamic form where users can add descriptions, checkboxes, and number inputs as they please. Currently, I have developed a simple dynamic form using jQuery, which allow ...

Is there a way to make my code on Google Sheets work across multiple tabs?

My Google Sheets code successfully pulls information from the main tab into my CRM Software every time someone fills out a form online. However, I'm struggling to get the script to run for multiple tabs on the same spreadsheet. I've tried a few s ...