<html>
<head>
<title>HTML5, CSS3 and JavaScript demo</title>
<link rel="stylesheet" href="style.css" />
<script src="https://code.createjs.com/createjs-2013.12.12.min.js"></script>
<script src="script.js"></script>
</head>
<body onload="init();">
<canvas id="canvas" width="500" height="500"></canvas>
</body>
<!-- Full tutorial at
https://superdevresources.com/draggable-shapes-canvas-createjs/
-->
</html>
var stage;
var SIZE = 50;
function init() {
//resize canvas to full width and height
var canvas = document.getElementsByTagName('canvas')[0];
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
stage = new createjs.Stage("canvas");
addCircle(canvas.width/2 - (SIZE * 2.5), canvas.height/2, SIZE, "#e74c3c");
addStar(canvas.width/2, canvas.height/2, SIZE, "#f1c40f");
addRoundedSquare(canvas.width/2 + (SIZE * 2.5), canvas.height/2, SIZE * 2, 5, "#9b59b6");
stage.update();
}
function addCircle(x, y, r, fill) {
var circle = new createjs.Shape();
circle.graphics.beginFill(fill).drawCircle(0, 0, r);
circle.x = x;
circle.y = y;
circle.name = "circle";
circle.on("pressmove",drag);
stage.addChild(circle);
}
function addStar(x, y, r, fill) {
var star = new createjs.Shape();
star.graphics.beginFill(fill).drawPolyStar(0, 0, r, 5, 0.6, -90);
star.x = x;
star.y = y;
star.name = "star";
star.on("pressmove",drag);
stage.addChild(star);
}
function addRoundedSquare(x, y, s, r, fill) {
var square = new createjs.Shape();
square.graphics.beginFill(fill).drawRoundRect(0, 0, s, s, r);
square.x = x - s/2;
square.y = y - s/2;
square.name = "square";
square.on("pressmove",drag);
stage.addChild(square);
}
function drag(evt) {
// target will be the container that the event listener was added to
if(evt.target.name == "square") {
evt.target.x = evt.stageX - SIZE;
evt.target.y = evt.stageY - SIZE;
}
else {
evt.target.x = evt.stageX;
evt.target.y = evt.stageY;
}
// make sure to redraw the stage to show the change
stage.update();
}
Output
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. |