Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
  <title>JS Bin</title>
</head>
<body>
  <div id="example1"></div>
  <div id="example2"></div>
</body>
</html>
 
body {
  background: cornflowerblue;
}
div {
  margin: auto;
  padding-top: 20px;
  vertical-align: middle;
  text-align: center;
}
p {
  margin: auto;
}
#example1 {
  background-color: gainsboro;
  position: absolute;
  width: 200px;
  height: 140px;
  left: 30px;
  top: 45px;
  border-radius: 30px;
}
#example2 {
  background-color: papayawhip;
  position: absolute;
  width: 150px;
  height: 150px;
  left: 122px;
  top: 233px;
}
 
/*
  There are two tasks for you to complete.
  They are listed below. 
  
  1) Create a factory function that will 
     take in the id of the element that 
     your new gameObject is based on.
     You should create the following properties
     for your object:
       id     (from element id)
       x      (from element "left" property)
       y      (from element "top" property)
       width  (from element "width" property)
       height (from element "height" property)
       speedX (set to 0)
       speedY (set to 0)
  
  2) Variables have already been created
     for storing your objects. Use the 
     factory function to create the objects
     and assign them to the variables
*/
// 1) create your factory function here
function Factory(elementId){
  var gameItem = {};
  gameItem.id = elementId;
  gameItem.x = parseFloat($(elementId).css("left"));
  gameItem.y = parseFloat($(elementId).css("top"));
  gameItem.width = $(elementId).width();
  gameItem.height = $(elementId).height();
  gameItem.speedX = 0;
  gameItem.speedY = 0;
  return gameItem;
}
// 2) create your objects here
var obj1 = Factory("#example1");
var obj2 = Factory("#example2");
/*
  Below here is testing code. If you did
  everything properly up above, then
  both objects should have their
  information displayed within them
*/
function displayData(obj){
  $(obj.id).append($("<p>").text(" x: " + obj.x));
  $(obj.id).append($("<p>").text(" y: " + obj.y));
  $(obj.id).append($("<p>").text(" width: " + obj.width));
  $(obj.id).append($("<p>").text(" height: " + obj.height));
  $(obj.id).append($("<p>").text(" speedX: " + obj.speedX));
  $(obj.id).append($("<p>").text(" speedY: " + obj.speedY));
}
displayData(obj1);
displayData(obj2);
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers