Javascript 如何使用 jquery 更改 html5 canvas 元素的颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10882872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:24:22  来源:igfitidea点击:

How can I alter the colour of a html5 canvas element using jquery?

javascriptjqueryhtmlhtml5-canvas

提问by Ollie Jones

Basically I have several canvas drawings on my page what I want to happen is when a the jquery function is activated the canvas drawings change to the colour of my choosing. I assume it involves some way of accessing context.fillStyle which defines the original colour but I am unsure how to alter it. In addition, would it be possible to instead give the canvas drawing a css style in the first instance and then change that style when the jquery is processed?

基本上我的页面上有几张画布绘图,我想要发生的是当 jquery 功能被激活时,画布绘图更改为我选择的颜色。我认为它涉及访问 context.fillStyle 的某种方式,它定义了原始颜色,但我不确定如何更改它。此外,是否可以在第一个实例中为画布绘制一个 css 样式,然后在处理 jquery 时更改该样式?

HTML

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

jQuery 脚本

<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

非常感谢

采纳答案by Ollie Jones

In case this is of any use, here is the solution I ended up with:

如果这有任何用处,这是我最终得到的解决方案:

First the HTML:

首先是 HTML:

<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 duplicate canvas elements which I used CSS to hide with:

我创建了使用 CSS 隐藏的重复画布元素:

CSS:

CSS:

#canvasIda, canvasId2a {
    display:none;
}

Then I made the following changes to the original Jquery script:

然后我对原始Jquery 脚本进行了以下更改

<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"});

     }

});

I'm sure many can come up with a more efficient solution but this worked and I'm not complaining.

我相信很多人可以想出一个更有效的解决方案,但这有效,我没有抱怨。

回答by David

It's as simple as this:

就这么简单:

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

回答by Val

You can do that :

你可以这样做:

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

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

您可以在此处查看 HTML5 画布教程(非常简单)。