基本上我的页面上有几个画布图纸我想要发生的是当激活 jquery 功能时,画布图纸更改为我选择的颜色。我认为它涉及访问定义原始颜色的 context.fillStyle 的某种方式,但我不确定如何更改它。此外,是否可以在第一个实例中为画布绘制一个 css 样式,然后在处理 jquery 时更改该样式?
HTML
<canvas class="canvaslink" id="canvasId" width="50" height="50"></canvas>
<canvas class="canvaslink" id="canvasId2" width="50" height="50"></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>
非常感谢