<html>
<head>
<meta charset="utf-8">
<title>Demo hud</title>
<script src="https://cdn.polyfill.io/v1/polyfill.min.js?features=String.prototype.repeat"></script>
</head>
<body>
<canvas id="c"></canvas>
<!-- <input type=range> -->
</body>
</html>
html, body {
max-height: 400px;
margin: 0;
}
canvas {
display: block;
background: #fff;
margin: 20px auto;
/* transform: scale(8); */
/* transform-origin: 50% 0%; */
/* width: 100%; */
/* height: 100%; */
/* border: 1px solid #ccc; */
image-rendering: optimizeSpeed; /* STOP SMOOTHING, GIVE ME SPEED */
image-rendering: crisp-edges; /* Firefox */
image-rendering: crisp-edges; /* Opera */
image-rendering: optimize-contrast; /* Chrome (and eventually Safari) */
image-rendering: pixelated; /* Chrome */
image-rendering: optimize-contrast; /* CSS3 Proposed */
}
input {
display: block;
margin: 0 auto;
width: 200px;
}
'use strict';
var c = document.querySelector('canvas'),
ctx = c.getContext('2d'),
width = 200,
height = 100,
RAD = Math.PI / 180;
c.width = width;
var hw = width/2;
c.height = height;
var hh = height/2;
ctx.font = 'normal 10px courier';
ctx.fillRect(0, 0, width, height);
ctx.strokeStyle = '#FFEB3B';
var guid = 0;
function Frame(speed) {
this.pos = 0.01;
this.speed = 1 + speed/100;
this.guid = ++guid;
return this;
}
Frame.prototype = {
draw: function () {
var scale = this.pos;
var w = width;
var h = height * 2.1;
var x = (w / 2 - w/2*scale);
var y = (height / 2 - height/2*scale*2.1);
ctx.moveTo(x, y);
ctx.lineTo(x, y + h*scale);
ctx.lineTo(x + width*scale, y + h*scale);
ctx.lineTo(x + width*scale, y);
},
update: function () {
this.pos = this.pos * this.speed;
},
finished: function () {
return width / 2 - width/2*this.pos < 0;
},
};
var boxes = [];
function radians(a) {
return a * RAD;
}
function square(angle) {
ctx.save();
ctx.translate(width/2, height/2);
ctx.rotate(radians(angle));
ctx.translate(-width/2, -height/2);
/* criss cross...gunna make you... */
// top-left -> bottom-right
ctx.moveTo(0,0);
ctx.lineTo(width, height);
ctx.stroke();
ctx.restore();
}
function init() {
ctx.fillRect(0, 0, width, height);
ctx.lineWidth = 1;
ctx.save();
square(20);
square(2);
square(-16);
square(-36);
square(-55);
square(-73);
ctx.restore();
}
function pad(s, length) {
var clen = (s+'').length;
return '0'.repeat(length - clen) + s;
}
function text() {
var d = Date.parse('2015-07-04T10:00:00');
var t = pad(((Date.now() - d) / 1000).toFixed(0), 8);
var textWidth = ctx.measureText(t+'');
ctx.fillText(t, width/2 - textWidth.width/2, 15);
}
function paintToBack() {
var data = c.toDataURL('image/png');
c.style.background = 'url(' + data + ')';
ctx.clearRect(0,0,width,height);
}
var tick = 0;
var speed = 15;
var prevFrameTime = 0;
function draw(elapsedTime) {
var timeSinceLastFrame = elapsedTime - (prevFrameTime || 0);
window.requestAnimationFrame(draw);
if (timeSinceLastFrame < 30 && prevFrameTime) {
return;
}
tick++;
prevFrameTime = elapsedTime;
ctx.clearRect(0,0,width,height);
text();
var spread = 6;
if (tick % speed === 0) {
var f = new Frame(spread);
boxes.push(f);
}
// boxes
var i = 0, length = boxes.length;
for (i = 0; i < length; i++) {
boxes[i].update();
}
i = 0;
while (boxes[i]) {
if (boxes[i].finished()) {
boxes.splice(i, 1);
} else {
i++;
}
}
// do a single draw from multiple paths
ctx.beginPath();
for (i = 0; i < boxes.length; i++) {
boxes[i].draw();
}
ctx.stroke();
ctx.closePath();
}
ctx.imageSmoothingEnabled = false;
init();
paintToBack();
ctx.fillStyle = '#FFEB3B';
ctx.lineWidth = 2;
draw();
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. |