Drawings on canvas don’t disappear and create weird visual effects

I’ve been trying to code a simple JavaScript game in which the user controls a square across a canvas in order to collect coins, scoring points. My intention is that when the user collects a coin, the coin will move to a new location and the score increases by one. However, I’ve been having problems with trying to have the coin move to another location. When the coin is collected, a new coin is added, but the original stays in the same place and can’t be re-collected. What’s more, a line appears that goes from coin to coin, and even creates this strange yellow background across the coins that covers the player.

Here’s my code for reference:

<!--HTML CODE-->
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Collect the coins game</title>
    <link rel="stylesheet" href="style.css">
  </head>
  <body>
    <canvas id="game">Canvas isn't working</canvas>
    <h2>Score: <span id="score">0</span></h2>
    <script src="script.js"></script>
  </body>
</html>
//js code

// creates character, coin, score and score element on html, and coin coordinates
var character;
var coin;
var scoreHTML = document.getElementById("score");
var score = 0;
var coinLoc;

// creating a new coin, including the first coin
function createNewCoin(){
  coinLoc = [Math.round(Math.random()*900),Math.round(Math.random()*450)];

  if(coinLoc[0] < 12){
    coinLoc[0] = 12;
  }
  else if(coinLoc[0] > 888){
    coinLoc[0] = 888;
  }
  if(coinLoc[1] < 12){
    coinLoc[1] = 12;
  }
  else if(coinLoc[1] > 438){
    coinLoc[1] = 438;
  }
  if(coin){
    delete window.coin;
    console.log(coin);
  }
  
  coin = new component(coinLoc[0],coinLoc[1],10,10,"black","yellow",4,360);
}

// starting game
function startGame(){
  gameCanvas.start();
  character = new component(2,2,20,20,"black","red",4,-1);
  createNewCoin();
  character.move();
}

// updating canvas
function updateGame(){
  gameCanvas.clear();
  character.checkCoin();
  if (gameCanvas.key && (gameCanvas.key == "ArrowLeft"|| gameCanvas.key == "ArrowRight" || gameCanvas.key == "ArrowUp" || gameCanvas.key == "ArrowDown")){
    if (gameCanvas.key && gameCanvas.key == "ArrowLeft"){
      character.Xmove -= 1;
    }
    if (gameCanvas.key && gameCanvas.key == "ArrowRight"){
      character.Xmove += 1;
    }
    if (gameCanvas.key && gameCanvas.key == "ArrowUp"){
      character.Ymove -= 1;
    }
    if (gameCanvas.key && gameCanvas.key == "ArrowDown") {
      character.Ymove += 1;
    }
  }
  else {
    character.slowdown();
  }
  character.move();
  character.update();
  coin.update();
}

// game canvas element
var gameCanvas = {
  canvas : document.getElementById("game"),
  start : function(){
    this.canvas.width = 900;
    this.canvas.height = 450;
    this.context = this.canvas.getContext("2d");
    this.interval = setInterval(updateGame,1000/30);
    window.addEventListener('keydown', function (e) {
      gameCanvas.key = e.key;
    })
    window.addEventListener('keyup', function () {
      gameCanvas.key = false;
    })
  },
  clear : function(){
    this.context.clearRect(0,0,this.canvas.width,this.canvas.height);
  }
}

// component class that creates the character and coins
class component{
  constructor(x,y,height,width,colorOut,colorIn,lineThick,radius){

    // these determine attributes for each shape (colorOut is fill color, colorIn is stroke color)
    this.x = x;
    this.y = y
    this.height = height;
    this.width = width;
    this.colorOut = colorOut;
    this.colorIn = colorIn;
    this.lineThick = lineThick;
    this.radius = radius;

    // determines movement
    this.Xmove = 0;
    this.Ymove = 0;

    // determines if player should stop accelerating
    this.stopAccelX = false;
    this.stopAccelY = false;

    // sets up coloring of objects
    var Gcontext = gameCanvas.context;
    Gcontext.fillStyle = this.colorIn;
    Gcontext.strokeStyle = this.colorOut;

    // stops player when they crash into a wall
    this.stop = function(){
      if (this.x < 2){
        this.Xmove = 0;
        this.x = 2;
      }      
      else if (this.x > 878){
        this.Xmove = 0;
        this.x = 878;
      }
      else {
      }
      if (this.y < 2){
        this.Ymove = 0;
        this.y = 2;
      }
      else if(this.y > 428){
        this.Ymove = 0;
        this.y = 428;
      }
    }
    
    // stops player from moving too quickly (reaching max speed)
    this.maxSpeed = function(){
      if (Math.sqrt(this.Xmove**2 + this.Ymove**2) > 15){
        if(this.Xmove < 0 && gameCanvas.key == "ArrowLeft"){
          this.stopAccelX = true;
        }
        else if(this.Xmove > 0 && gameCanvas.key == "ArrowRight"){
          this.stopAccelX = true;
        }
        else {
          this.stopAccelX = false;
        }
        if(this.Ymove < 0 && gameCanvas.key == "ArrowUp"){
          this.stopAccelY = true;
        }
        else if(this.Ymove > 0 && gameCanvas.key == "ArrowDown"){
          this.stopAccelY = true;
        }
        else {
          this.stopAccelY = false;
        }
      }
    }

    // slows down the player
    this.slowdown = function(){
      if (this.Xmove < 0){
        this.Xmove = (-1 * ((this.Xmove * -1) ** 0.9)) + 0.066;
      }
      else if (this.Xmove > 0){
        this.Xmove = (this.Xmove ** 0.9) - 0.066;
      }
      if (this.Ymove < 0){
        this.Ymove = (-1 * ((this.Ymove * -1) ** 0.9)) + 0.066;
      }
      else if (this.Ymove > 0){
        this.Ymove = (this.Ymove ** 0.9) - 0.066;
      }
    }
    
    // moves character
    this.move = function(){
      this.stopAccelX = false;
      this.stopAccelY = false;
      this.maxSpeed();
      if(this.stopAccelX){
        if(this.Xmove > 0){
          this.Xmove -= 1;
        }
        else {
          this.Xmove += 1;
        }
      }
      if(this.stopAccelY){
        if(this.Ymove > 0){
          this.Ymove -= 1;
        }
        else {
          this.Ymove += 1;
        }
      }
      this.x += this.Xmove;
      this.y += this.Ymove;
      this.stop();
    }

    // updating character and coins
    this.update = function(){
      if(this.radius == -1) {
        this.rectConstruct(this.colorOut);
      }
      else {
        this.arcConstruct(this.colorOut);
      }
    }

    // checks for coin collision
    this.checkCoin = function(){
      if(this.x + 34 >= coin.x && this.x - 14 <= coin.x && this.y + 34 >= coin.y && this.y - 14 <= coin.y){
        score++;
        scoreHTML.textContent = score;
        createNewCoin();
      }
    }
    
  }

  // updates rectangles
  rectConstruct(stroke){
    var Gcontext = gameCanvas.context;
    Gcontext.fillStyle = this.colorIn;
    Gcontext.fillRect(this.x,this.y,this.height,this.width);
    if(stroke != false){
      Gcontext.strokeStyle = this.colorOut;
      Gcontext.lineWidth = this.lineThick;
      Gcontext.strokeRect(this.x,this.y,this.height,this.width);
    }
  }

  // updates coins
  arcConstruct(stroke){
    var Gcontext = gameCanvas.context;
    Gcontext.fillStyle = this.colorIn;
    Gcontext.arc(this.x,this.y,this.height,this.width,this.radius);
    Gcontext.fill();
    if(stroke != false){
      Gcontext.strokeStyle = this.colorOut;
      Gcontext.lineWidth = this.lineThick;
      Gcontext.stroke();
    }
  }
}

startGame();
/*css code*/
body {
  background-color:rgb(102,120,255);
}

canvas {
  background-color:white;
  border:1px solid black;
}

I’ve tried to use coin = null or delete window.coin to delete the first coin, but they don’t fix the problem at all. Can someone tell me what I need to fix?

New contributor

User11 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật