gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:28:56 26 4
gpt4 key购买 nike

基本上我的页面上有几个 Canvas 绘图,我想要发生的是当 jquery 函数被激活时 Canvas 绘图更改为我选择的颜色。我认为它涉及某种访问定义原始颜色的 context.fillStyle 的方法,但我不确定如何更改它。另外,是否可以先给 Canvas 绘制一个 css 样式,然后在处理 jquery 时更改该样式?

HTML

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

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

Canvas 脚本

<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>
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>

非常感谢

最佳答案

就这么简单:

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

关于javascript - 如何使用 jquery 更改 html5 canvas 元素的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10882872/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com