gpt4 book ai didi

javascript - 如何使用 Javascript 中的单击按钮更改笔划的颜色?

转载 作者:行者123 更新时间:2023-11-30 11:04:20 25 4
gpt4 key购买 nike

笔划的默认颜色是黑色。我想要这样当用户按下按钮时,描边颜色从黑色变为蓝色

我试图在 javascript function colorBlue() 之前添加一个 javascript window.addEventListener('load', () => {,但我得到了错误:` `Uncaught ReferenceError: colorBlue is not defined```。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Canvas</title>
<link href="canvas.css" rel="stylesheet">
</head>
<body>
<div id="container">
<canvas id="c"></canvas>
<input class="button" onclick="colorBlue()" type="button" id="blue">Blue</button>
</div>
</body>
<script src="canvas.js"></script>
</html>
#c{
border: 1px solid black;
}
#container{
text-align: center;
}
.button{
width:10em;
height:5em;
text-align: center;
}

function colorBlue(){
const c = document.querySelector("#c");
c.height = window.innerHeight;
c.width = window.innerWidth;
const ctx = c.getContext("2d");

window.addEventListener('load', () => {

let painting = false;

//when mouse is clicked; paint
function mousedown(b){
painting = true;
//allows for paint to appear without nedding to drag mouse
mousemove(b);
}
//when mouse is not clicked; don't paint
function mouseup(){
painting = false;
//resets path each time so multiple can be created
ctx.beginPath();
}
function mousemove(b){
//if not holding down the mouse; nothing happens
if(!painting) return;
//line-width of paint
ctx.lineWidth = 10;
//roundness of paint
ctx.lineCap = 'round';
//change color


//create paint wherever the mouse goes: ctx[on the canvas].lineTo[move the line to]()
ctx.lineTo(b.clientX, b.clientY);
//end the stroke and visualize paint
ctx.stroke();
//begins a new paint so that everything doesn't just stick to a fat line
ctx.beginPath();
//move the new line to wherever the mouse goes
ctx.moveTo(b.clientX, b.clientY);
}
//starting posistion of paint line
c.addEventListener('mousedown', mousedown);
//ending posistion of paint line
c.addEventListener('mouseup', mouseup);
//whenever the mouse is moving; paint
c.addEventListener('mousemove', mousemove);

})};

const c = document.querySelector("#c");
c.height = window.innerHeight;
c.width = window.innerWidth;
const ctx = c.getContext("2d");

let painting = false;

function mousedown(e){
painting = true;
mousemove(e);
}
function mouseup(){
painting = false;
ctx.beginPath();
}
function mousemove(e){
if(!painting) return;
ctx.lineWidth = 10;
ctx.lineCap = 'round';
ctx.strokeStyle = 'black';

ctx.lineTo(e.clientX, e.clientY);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(e.clientX, e.clientY);
}
c.addEventListener('mousedown', mousedown);
c.addEventListener('mouseup', mouseup);
c.addEventListener('mousemove', mousemove);

没有出现蓝色笔划,只有蓝色圆点,以及出于未知原因重置 Canvas 的按钮。我希望创建蓝色笔划,但会创建黑色笔划。

最佳答案

你的意思是这样的吗?

function colorBlue(){
document.getElementById("c").style.border ="1px solid blue";

}
#c{
border: 1px solid black;
}
#container{
text-align: center;
}
.button{
width:10em;
height:5em;
text-align: center;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Canvas</title>
<link href="canvas.css" rel="stylesheet">
</head>
<body>
<div id="container">
<canvas id="c"></canvas>
<input class="button" onclick="colorBlue()" type="button" id="blue">Blue</button>
</div>
</body>
<script src="canvas.js"></script>
</html>

关于javascript - 如何使用 Javascript 中的单击按钮更改笔划的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56285430/

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