0
votes

I am trying to get this JavaScript to display the coordinates of the client's mouse in the canvas and then create shapes at the location of the user's clicks. The shape color is supposed to rotate between red, blue, and green every click. I haven't been able to test the shape drawing because the coordinates won't work for some reason. I'm more interested in getting the code to work rather than having it be optimal.

var colors = ["red", "blue", "green"];
var index = 0;

function showCoords(event) {
  console.log(event);
  var x = event.clientX;
  var y = event.clientY;
  document.getElementById("mouse_position").innerHTML = x + ", " + y;
}

function clearCoor(event) {
  console.log(event);
  document.getElementById("mouse_position").innerHTML = "";
}

function drawShape(event) {
  var x = event.clientX;
  var y = event.clientY;
  var canvas = document.getElementById("myCanvas");
  var ctx = canvas.getContext("2d");
  var circle = document.getElementById("circle");
  var square = document.getElementById("square");
  var triangle = document.getElementById("triangle");
  var length = colors.length();
  var index = 0;
  var color = colors[index];
  if (index >= length) {
    index = 0;
  } else {
    index++;
  }

  if (circle.checked == true) {
    ctx.beginPath();
    ctx.arc(x, y, 25, 0, 2 * Math.PI);
    ctx.fillStyle = color;
    ctx.fill();
  }

  if (square.checked == true) {
    ctx.fillStyle = color;
    ctx.fillRect(x, y, 50, 50);
  }

  if (triangle.checked == true) {
    ctx.beginPath();
    ctx.moveTo(x, y);
    ctx.lineTo(x + 25, y - 50);
    ctx.lineTo(x - 50, y - 50);
    ctx.closePath();
    ctx.strokeStyle = color;
    ctx.stroke();
  }
}

function clearCanvas() {
  fillStyle = white;
  fillRect(0, 0, canvas.width, canvas.height);
}
<canvas onclick="drawShape(event)" onmousemove="showCoords(event)" onmouseout="clearCoor(event)" id="myCanvas" width="500" height="500" style="border:1px solid #000000;">
  Your browser does not support the HTML5 canvas tag.
</canvas>

<form action="">
  <input type="radio" id="circle" name="shape" value="circle" checked="checked">Circle
  <br>
  <input type="radio" id="square" name="shape" value="square">Square
  <br>
  <input type="radio" id="triangle" name="shape" value="triangle">Triangle
</form>

<button type="button" onclick="clearCanvas()">Clear Canvas</button>

<div id="mouse_position"></div>
1

1 Answers

1
votes

You have several errors in your JS.

  • Use colors.length instead of colors.length()

  • Remove var index = 0 from the drawShape function.

  • Your index increment code isn't quite right.

  • Use event.page(X|Y) to get accurate coordinates.

The below code works correctly.

var colors = ["red", "blue", "green"];
var index = 0;

function showCoords(event) {
  //console.log(event);
  var x = event.clientX;
  var y = event.clientY;
  document.getElementById("mouse_position").innerHTML = x + ", " + y;
}

function clearCoor(event) {
  //console.log(event);
  document.getElementById("mouse_position").innerHTML = "";
}

function drawShape(event) {
  console.log(event);
  var canvas = document.getElementById("myCanvas");
  var x = event.pageX - canvas.offsetLeft;
  var y = event.pageY - canvas.offsetTop;
  var ctx = canvas.getContext("2d");
  var circle = document.getElementById("circle");
  var square = document.getElementById("square");
  var triangle = document.getElementById("triangle");
  var length = colors.length;
  var color = colors[index];
  if (++index >= length) {
    index = 0;
  }
  
  if (circle.checked == true) {
    ctx.beginPath();
    ctx.arc(x, y, 25, 0, 2 * Math.PI);
    ctx.fillStyle = color;
    ctx.fill();
  }

  if (square.checked == true) {
    ctx.fillStyle = color;
    ctx.fillRect(x, y, 50, 50);
  }

  if (triangle.checked == true) {
    ctx.beginPath();
    ctx.moveTo(x, y);
    ctx.lineTo(x + 25, y - 50);
    ctx.lineTo(x - 50, y - 50);
    ctx.closePath();
    ctx.strokeStyle = color;
    ctx.stroke();
  }
}

function clearCanvas() {
  fillStyle = white;
  fillRect(0, 0, canvas.width, canvas.height);
}
<canvas onclick="drawShape(event)" onmousemove="showCoords(event)" onmouseout="clearCoor(event)" id="myCanvas" width="500" height="500" style="border:1px solid #000000;">
  Your browser does not support the HTML5 canvas tag.
</canvas>

<form action="">
  <input type="radio" id="circle" name="shape" value="circle" checked="checked">Circle
  <br>
  <input type="radio" id="square" name="shape" value="square">Square
  <br>
  <input type="radio" id="triangle" name="shape" value="triangle">Triangle
</form>

<button type="button" onclick="clearCanvas()">Clear Canvas</button>

<div id="mouse_position"></div>