<html>
<head>
<meta charset=utf-8 />
<title>JSBin 5th bday!</title>
<style>
body {
text-align: center;
}
canvas {
position: absolute;
top: 100px;
left: 20%;
}
</style>
</head>
<body>
<canvas id="c2" width="500" height="500"></canvas>
<canvas id="c" width="500" height="500"></canvas>
</body>
</html>
/*
For JSBin 5th birthday compo
Author: Kushagra Gour (@chinchang457)
*/
var last_t = Date.now(),
PI_BY_180 = Math.PI / 180,
c = document.querySelector('#c'),
ctx = c.getContext('2d'),
ctx2 = c2.getContext('2d'),
t = 0,
square = {
w: 80,
h: 80,
angle: 91
},
jumps2 = [
{x: 0, y: 2, pivot: 2},
{x: 1, y: 2, pivot: 1},
{x: 2, y: 2, pivot: 3},
{x: 2, y: 1, pivot: 2},
{x: 2, y: 0, pivot: 0},
{x: 1, y: 0, pivot: 3},
{x: 0, y: 0, pivot: 1},
{x: 0, y: 1, pivot: 0}
],
points = [{}, {}, {}, {}],
i = -1,
colorSwitch = 0,
colors = ['red', 'blue', 'green', 'orange'];
function update () {
var x, y, j;
if (square.angle > 90) {
i = (++i) % jumps2.length;
x = jumps2[i].x * square.w,
y = jumps2[i].y * square.h;
if (!i) {
points[0].x = x; points[0].y = y;
points[1].x = x + square.w; points[1].y = y;
points[2].x = x + square.w; points[2].y = y + square.h;
points[3].x = x; points[3].y = y + square.h;
colorSwitch ^= 1;
}
else {
for (j = 0; j <= 3; j++) {
points[j].x = points[j].cx;
points[j].y = points[j].cy;
}
}
points.pivot = jumps2[i].pivot;
square.angle = 0;
}
square.angle += 0.9;
var s = Math.sin(square.angle * PI_BY_180),
c = Math.cos(square.angle * PI_BY_180),
pivotPoint = points[points.pivot];
pivotPoint.cx = pivotPoint.last_x = pivotPoint.x;
pivotPoint.cy = pivotPoint.last_y = pivotPoint.y;
// rotate 3 points about the 4th pivot point
for (j = 0; j < points.length; j++) {
if (j === points.pivot) continue;
// translate point back to origin:
x = points[j].x - pivotPoint.x;
y = points[j].y - pivotPoint.y;
// rotate point
var xnew = x * c - y * s,
ynew = x * s + y * c;
// save last cordinates
points[j].last_x = points[j].cx || points[j].x;
points[j].last_y = points[j].cy || points[j].y;
// translate point back:
points[j].cx = xnew + pivotPoint.x;
points[j].cy = ynew + pivotPoint.y;
}
}
function draw () {
ctx.clearRect (0, 0, 600, 600);
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(points[0].cx, points[0].cy);
ctx2.beginPath();
for (var j = 0; j < points.length; j++) {
ctx.lineTo(points[j].cx, points[j].cy);
ctx2.save();
ctx2.strokeStyle = colorSwitch ? colors[j] : '#f9f9f9';
ctx2.lineWidth = colorSwitch ? 1 : 3;
ctx2.moveTo(points[j].last_x, points[j].last_y);
ctx2.lineTo(points[j].cx, points[j].cy);
ctx2.stroke();
ctx2.restore();
}
ctx.closePath();
ctx.rect(0, 0, 240, 240);
ctx.stroke();
}
function animate() {
update();
draw();
// works on chrome and firefox
window.requestAnimationFrame ? requestAnimationFrame(animate) : mozRequestAnimationFrame(animate);
}
animate();
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. |