How to change color of html5 canvas element using jquery?

Basically I have some drawings on the canvas that I want to see when the jquery function is activated, the canvas drawings change color of my choice. I assume this is due to some way of accessing context.fillStyle that defines the original color, but I'm not sure how to change it. Also, is it possible to draw a CSS style in the first instance instead of canvas, and then change that style when processing jquery?

HTML

 <canvas class="canvaslink" id="canvasId" width="50" height="50"></canvas>

 <canvas class="canvaslink" id="canvasId2" width="50" height="50"></canvas>

Canvas script

<script>
function drawSomething(canvas) {
var context = canvas.getContext("2d");

var width = 125;  // Triangle Width
var height = 45; // Triangle Height
var padding = 5;

// Draw a path
context.beginPath();
context.moveTo(padding + width-125, height + padding);        // Top Corner
context.lineTo(padding + width-90,height-17 + padding); // point
context.lineTo(padding, height-35 + padding);         // Bottom Left
context.closePath();

// Fill the path
context.fillStyle = "#9ea7b8";
context.fill();


}

drawSomething(document.getElementById("canvasId"));
drawSomething(document.getElementById("canvasId2"));

</script>

JQuery script

<script>
var counter = $('#counter div strong');

var counterH2 = $('h2 strong');

$('#box').click(function(){
    $("#counter").css("display", "block");
     var counterValue = counter.text();
     counterValue = ++counterValue;
     counter.text(counterValue);
     counterH2.text(counterValue);
     if (counterValue == 3){
        alert("Thanks for visiting!");
        $('body').css({"background-color":"#9ea7b8"});
        $('body').css({"color":"#11112c"});

        **//I'd like to change the canvas colour here!**

     }

});
</script> 

Many thanks

+5
source share
3 answers

In case this is useful, here is the solution in which I ended up:

HTML first:

<canvas class="canvaslink" id="canvasId" width="50" height="50"></canvas>
<canvas class="canvaslink" id="canvasIda" width="50" height="50"></canvas>

<canvas class="canvaslink" id="canvasId2" width="50" height="50"></canvas>
<canvas class="canvaslink" id="canvasId2a" width="50" height="50"></canvas>

I created repeating canvas elements that I used CSS to hide:

CSS:

#canvasIda, canvasId2a {
    display:none;
}

JQuery script:

<script>
var counter = $('#counter div strong');

var counterH2 = $('h2 strong');

$('#box').click(function(){
    $("#counter").css("display", "block");
     var counterValue = counter.text();
     counterValue = ++counterValue;
     counter.text(counterValue);
     counterH2.text(counterValue);
     if (counterValue == 3){

        $('body').css({"background-color":"#9ea7b8"});
        $('body').css({"color":"#11112c"});
        $('a').css({"color":"#11112c"});

     //remove the previous canvas elements

        element = document.getElementById("canvasId");
        element2 = document.getElementById("canvasId2");

        element.parentNode.removeChild(element);
        element2.parentNode.removeChild(element2);

    //function to draw new canvas elements with new desired colour

        function drawSomething2(canvas) {
            var context = canvas.getContext("2d");

            var width = 125;  // Triangle Width
            var height = 45; // Triangle Height
            var padding = 5;

    // Draw a path

            context.beginPath();
            context.moveTo(padding + width-125, height + padding);        // Top Corner
            context.lineTo(padding + width-90,height-17 + padding); // point
            context.lineTo(padding, height-35 + padding);         // Bottom Left
            context.closePath();

    // Fill the path
            context.fillStyle = "#11112c";
            context.fill();


        }
   //draw the canvas elements

        drawSomething2(document.getElementById("canvasIda"));
        drawSomething2(document.getElementById("canvasId2a"));

   //display the previously hidden elements containing the new canvas drawings

        $('#canvasIda').css({"display":"block"});
        $('#canvasId2a').css({"display":"block"});

     }

});

, , , .

-3

:

document.getElementById("ID").style.background = 'color';
+14

You can do it:

var context = canvas.getContext('2d');
context.fillStyle = "#000000";
context.fill();
// Other properties ...

You can see the HTML5 canvas tutorial (very simple) here .

+2
source

All Articles