gpt4 book ai didi

javascript - 如何使用 JavaScript 部分刷新 元素?

转载 作者:行者123 更新时间:2023-11-30 14:08:41 29 4
gpt4 key购买 nike

我使用 JavaScript 在 HTML 页面上进行了性格测试。基本上,用户选择一个问题的答案,下面的段落会更改内容,并且彩色网格(我使用 Canvas 元素创建)中会出现一个黑点以指示您的个性颜色。

起初一切似乎都运行良好。问题是,如果你做过一次并想再做一次,你不能只选择其他答案之一并单击绿色按钮。现在两个黑点将出现在彩色网格中。有人知道如何在我的 JavaScript 代码中解决这个问题吗?

这是我的代码:

    // The draw function loads when the page is opened
function draw() {
let canvas = document.getElementById('canvas');
if (canvas.getContext) {
let ctx = canvas.getContext('2d');

// The 4 colored squares
ctx.fillStyle = 'rgb(204, 51, 0)';
ctx.fillRect(0, 0, 150, 150);
ctx.fillStyle = 'rgb(51, 153, 51)';
ctx.fillRect(0, 150, 150, 150);
ctx.fillStyle = 'rgb(255, 255, 0)';
ctx.fillRect(150, 0, 150, 150);
ctx.fillStyle = 'rgb(51, 102, 204)';
ctx.fillRect(150, 150, 150, 150);


}
}

const form = document.querySelector('form');
let btn = document.querySelector('button');
let para = document.querySelector('p');

//Event listener for the button
btn.addEventListener('click', myColor);

function myColor() {
let choice = form.color.value;
let canvas = document.getElementById('canvas');
if (!choice) {
para.textContent = 'Please chose one of the answers above.';
}

if (choice === 'red') {
para.textContent = 'You´re so red!';
// Adding a black circle to indicate color on canvas
if (canvas.getContext) {
let ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.arc(75, 75, 23, 0, Math.PI * 2, true);
ctx.fillStyle ='rgb(0, 0, 0)';
ctx.fill();
}

} else if (choice === 'blue') {
para.textContent = 'You´re so blue!';
// Adding a black circle to indicate color on canvas
if (canvas.getContext) {
let ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.arc(225, 225, 23, 0, Math.PI * 2, true);
ctx.fillStyle ='rgb(0, 0, 0)';
ctx.fill();
}
}

else if (choice === 'yellow') {
para.textContent = 'You´re so yellow!';
// Adding a black circle to indicate color on canvas
if (canvas.getContext) {
let ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.arc(225, 75, 23, 0, Math.PI * 2, true);
ctx.fillStyle ='rgb(0, 0, 0)';
ctx.fill();
}
}

else if (choice === 'green') {
para.textContent = 'You´re so green!';
// Adding a black circle to indicate color on canvas
if (canvas.getContext) {
let ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.arc(75, 225, 23, 0, Math.PI * 2, true);
ctx.fillStyle ='rgb(0, 0, 0)';
ctx.fill();
}
}
}
<body onload="draw();">

<form action="">
<h1>A friend invites you to a party. You...</h1>
<br />

<input id="red" type="radio" name="color" value="red">...bluntly tell your friend you have other priorities. <br/>
<input id="blue" type="radio" name="color" value="blue">...tell your friend you are finishing a coding assignment tonight. <br/>
<input id="yellow" type="radio" name="color" value="yellow">...hug your friend and start discussing the outfit. <br/>
<input id="green" type="radio" name="color" value="green">...thank your friend for inviting you, and tell her you look forward to it. <br/>

</form>

<button> Show me the color of my personality! </button>

<canvas id="canvas" width="300" height="300">
A picture of where your personality type fits in.
</canvas>

<p></p>

最佳答案

一种方法是删除 Canvas 元素并在单击按钮时重新应用它。

function myColor() {
let choice = form.color.value;

var oldcanv = document.getElementById('canvas');
document.removeChild(oldcanv)

var canv = document.createElement('canvas');
canv.id = 'canvas';
document.body.appendChild(canv);
let canvas = document.getElementById('canvas');
if (!choice) {
para.textContent = 'Please chose one of the answers above.';
}

.......

或使用

ctx.clearRect(0, 0, canvas.width, canvas.height);

取代之前的建议。

关于javascript - 如何使用 JavaScript 部分刷新 <canvas> 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54872532/

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