1

This is supposed to generate 4 shapes from a DOM that aree positioned differently across the screen, and move across the screen when prompted.

I'm having issue making the boundaryChecker function work. I've been trying for ages to figure what to do, but am stumped. I know that I have to call by value and not reference for the dx and dy values since they're objects, but I am just unsure how to execute that. If anyone can help and provide code/pseudocode or even just advice I'd appreciate it.

Ultimately it needs to reach the end, and switch directions, so it is bouncing across the screen.

Code example:

function getElement(elementName) { //This is like getElementById()

  var element = document.getElementById(elementName);
  return element;
}

function drawShape(canvasID) { //creates the shapes

  var canvas = getElement(canvasID);
  var ctx = canvas.getContext('2d');

  if (canvasID == "CANVAS1") {
    ctx.rect(25, 25, 100, 100);
    ctx.fillStyle = "red";
    ctx.fill();
  } else if (canvasID == "CANVAS2") {
    ctx.rect(25, 25, 100, 100);
    ctx.fillStyle = "blue";
    ctx.fill();
  } else if (canvasID == "CANVAS3") {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.beginPath();
    ctx.arc(100, 75, 50, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.fillStyle = "green";
    ctx.fill();
  } else if (canvasID == "CANVAS4") {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.beginPath();
    ctx.arc(100, 75, 50, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.fillStyle = "yellow";
    ctx.fill();
  }

}

function setNewPosition(objID, dx, dy) { //neew shape position

  var obj = getElement(objID);
  //boundaryCheck(objID);
  var newleft = parseInt(obj.style.left) + dx;
  var newtop = parseInt(obj.style.top) + dy;
  obj.style.left = newleft.toString() + 'px';
  obj.style.top = newtop.toString() + 'px';

}

function shape(objID, canvasID, dx, dy, delay) {
  var thisShape = this;
  this.objID = objID;
  this.dx = dx;
  this.dy = dy;
  this.speedX = 0;
  this.speedY = 0;

  thisShape.draw = function() {
    drawShape(canvasID);
  }


  thisShape.move = function() {
    setNewPosition(objID, dx, dy);
    setTimeout(thisShape.move, delay);
  }
}

function drawObj(id) { //starts the process to visually show the shapes
  document.shapeObj[id].draw();
}

function moveObj(id) { //starts process to move the shapes. Goes infinitely at the moment
  document.shapeObj[id].move();
}


//A type of boundary checker. This is where im stuck. Anything helps.
/*function boundaryCheck(objID,canvasID,dx,dy){
    var elm = getElement(objID);

    var left = parseInt(elm.style.left);
    var top = parseInt(elm.style.top);
    if(left > 400 || left < 0){
        dx *= -1;
    }
    if(top >4 00 || top < 0){
        dy *= -1;
    }
    left +=dx;
    top +=dy;

}   */
<html>

<head>
  <script type="text/javascript" src="jstest2.js"></script>
  <script type="text/javascript">
    // run function when document is loaded
    function start() {
      //load DOM objects here
      document.shapeObj = {};

      //Creating the shapes in the DOM -T
      document.shapeObj["SHAPE1"] = new shape("SHAPE1", "CANVAS1", 1, 1, 10);
      document.shapeObj["SHAPE2"] = new shape("SHAPE2", "CANVAS2", 1, 1, 10);
      document.shapeObj["SHAPE3"] = new shape("SHAPE3", "CANVAS3", 1, 1, 10);
      document.shapeObj["SHAPE4"] = new shape("SHAPE4", "CANVAS4", 1, 1, 10);

    }
  </script>

</head>
<title> Shape Assignment</title>

<body onload="start()">


  <!-- These are the button div id's that call the drawObj Function - T -->
  <div id="buttons">
    Draw
    <input value="Draw Shape 1" type="button" onclick="drawObj('SHAPE1')">
    <input value="Draw Shape 2" type="button" onclick="drawObj('SHAPE2')">
    <input value="Draw Shape 3" type="button" onclick="drawObj('SHAPE3')">
    <input value="Draw Shape 4" type="button" onclick="drawObj('SHAPE4')">
    <br>
    <button onclick="moveObj('SHAPE1')">Move Shape 1</button>
    <button onclick="moveObj('SHAPE2')">Move Shape 2</button>
    <button onclick="moveObj('SHAPE3')">Move Shape 3</button>
    <button onclick="moveObj('SHAPE4')">Move Shape 4</button>
    <br>

  </div>



  <div id="SHAPE1" style="position: absolute; top: 40px; left: 30px;">
    <canvas id="CANVAS1" width="400" height="400"></canvas>
  </div>
  <div id="SHAPE2" style="position: absolute; top: 160px; left: 320px;">
    <canvas id="CANVAS2" width="400" height="400"></canvas>
  </div>
  <div id="SHAPE3" style="position: absolute; top: 30px; left: 380px;">
    <canvas id="CANVAS3" width="400" height="400"></canvas>
  </div>
  <div id="SHAPE4" style="position: absolute; top: 350px; left: 20px;">
    <canvas id="CANVAS4" width="400" height="400"></canvas>
  </div>

</body>

</html>
kantor9991
  • 61
  • 6
  • 1
    please edit this question with your code working as well in the code editor, so that it is possible to figure out what you are trying to achieve more clearly, thanks! – loretoparisi Mar 12 '18 at 18:26
  • 1
    It should work fine. I made all the trouble-spots comments. I'll try again and look over the code. Edit: works fine for me. Dont forget the src name on the HTML that may it. Not sure why it isnt working for others. – kantor9991 Mar 12 '18 at 18:28
  • The issue I am having is that when I run the code and move the shapes, the shapes continue moving forever. I know I need to implement a boundary to prevent them from going past their canvas, but all attempts have failed. I am not sure how to change the values of dx and dy, but obviously what I have now does not implement a boundary. – kantor9991 Mar 12 '18 at 18:35
  • 1
    Ok I have checked it out and added an example so that the question can be better understood. – loretoparisi Mar 12 '18 at 18:57
  • Oh okay. Thanks. Ill get rid of the non-snippeted section then so it's easier to read! Appreciate it. – kantor9991 Mar 12 '18 at 19:05

1 Answers1

1

You have forgot to update the values of the left and top style, then it works like here:

  var left = parseInt(elm.style.left);
  var top = parseInt(elm.style.top);
  if (left > 100 || left < 0) {
    dx = 0, left = 100;
    elm.style.left = left + 'px'
  }
  if (top > 100 || top < 0) {
    dy = 0, top = 100
    elm.style.top = top + 'px'
  }

function getElement(elementName) { //This is like getElementById()

  var element = document.getElementById(elementName);
  return element;
}

function drawShape(canvasID) { //creates the shapes

  var canvas = getElement(canvasID);
  var ctx = canvas.getContext('2d');

  if (canvasID == "CANVAS1") {
    ctx.rect(25, 25, 100, 100);
    ctx.fillStyle = "red";
    ctx.fill();
  } else if (canvasID == "CANVAS2") {
    ctx.rect(25, 25, 100, 100);
    ctx.fillStyle = "blue";
    ctx.fill();
  } else if (canvasID == "CANVAS3") {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.beginPath();
    ctx.arc(100, 75, 50, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.fillStyle = "green";
    ctx.fill();
  } else if (canvasID == "CANVAS4") {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.beginPath();
    ctx.arc(100, 75, 50, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.fillStyle = "yellow";
    ctx.fill();
  }

}

function setNewPosition(objID, dx, dy) { //neew shape position

  var obj = getElement(objID);
  boundaryCheck(objID, dx, dy);
  var newleft = parseInt(obj.style.left) + dx;
  var newtop = parseInt(obj.style.top) + dy;
  obj.style.left = newleft.toString() + 'px';
  obj.style.top = newtop.toString() + 'px';

}

function shape(objID, canvasID, dx, dy, delay) {
  var thisShape = this;
  this.objID = objID;
  this.dx = dx;
  this.dy = dy;
  this.speedX = 0;
  this.speedY = 0;

  thisShape.draw = function() {
    drawShape(canvasID);
  }


  thisShape.move = function() {
    setNewPosition(objID, dx, dy);
    setTimeout(thisShape.move, delay);
  }
}

function drawObj(id) { //starts the process to visually show the shapes
  document.shapeObj[id].draw();
}

function moveObj(id) { //starts process to move the shapes. Goes infinitely at the moment
  document.shapeObj[id].move();
}


//A type of boundary checker. This is where im stuck. Anything helps.
function boundaryCheck(objID, dx, dy) {
  var elm = getElement(objID);
  var left = parseInt(elm.style.left);
  var top = parseInt(elm.style.top);
  if (left > 100 || left < 0) {
    dx = 0, left = 100;
    elm.style.left = left + 'px'
  }
  if (top > 100 || top < 0) {
    dy = 0, top = 100
    elm.style.top = top + 'px'
  }
  left += dx;
  top += dy;

}
<html>

<head>
  <script type="text/javascript" src="jstest2.js"></script>
  <script type="text/javascript">
    // run function when document is loaded
    function start() {
      //load DOM objects here
      document.shapeObj = {};

      //Creating the shapes in the DOM -T
      document.shapeObj["SHAPE1"] = new shape("SHAPE1", "CANVAS1", 1, 1, 10);
      document.shapeObj["SHAPE2"] = new shape("SHAPE2", "CANVAS2", 1, 1, 10);
      document.shapeObj["SHAPE3"] = new shape("SHAPE3", "CANVAS3", 1, 1, 10);
      document.shapeObj["SHAPE4"] = new shape("SHAPE4", "CANVAS4", 1, 1, 10);

    }
  </script>

</head>
<title> Shape Assignment</title>

<body onload="start()">


  <!-- These are the button div id's that call the drawObj Function - T -->
  <div id="buttons">
    Draw
    <input value="Draw Shape 1" type="button" onclick="drawObj('SHAPE1')">
    <input value="Draw Shape 2" type="button" onclick="drawObj('SHAPE2')">
    <input value="Draw Shape 3" type="button" onclick="drawObj('SHAPE3')">
    <input value="Draw Shape 4" type="button" onclick="drawObj('SHAPE4')">
    <br>
    <button onclick="moveObj('SHAPE1')">Move Shape 1</button>
    <button onclick="moveObj('SHAPE2')">Move Shape 2</button>
    <button onclick="moveObj('SHAPE3')">Move Shape 3</button>
    <button onclick="moveObj('SHAPE4')">Move Shape 4</button>
    <br>

  </div>



  <div id="SHAPE1" style="position: absolute; top: 40px; left: 30px;">
    <canvas id="CANVAS1" width="400" height="400"></canvas>
  </div>
  <div id="SHAPE2" style="position: absolute; top: 160px; left: 320px;">
    <canvas id="CANVAS2" width="400" height="400"></canvas>
  </div>
  <div id="SHAPE3" style="position: absolute; top: 30px; left: 380px;">
    <canvas id="CANVAS3" width="400" height="400"></canvas>
  </div>
  <div id="SHAPE4" style="position: absolute; top: 350px; left: 20px;">
    <canvas id="CANVAS4" width="400" height="400"></canvas>
  </div>

</body>

</html>
loretoparisi
  • 15,724
  • 11
  • 102
  • 146
  • 1
    Thanks for getting the bounds to work. One of the big issues currently though is that the shapes are supposed to bounce around the screen by bouncing when they hit the boundary. Thats why the dx *= -1 was present in those if statements. As far as the actual canvas boundaries go, this solves my problem. Thanks for helping me on one of the steps. – kantor9991 Mar 12 '18 at 19:30