<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
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |