<html>
<head>
<meta charset="utf-8">
<title>DOT CLICKER</title>
<style>
#canvas {
border: thin solid black;
}
</style>
</head>
<body>
<canvas id="canvas" width=600 height=400></canvas>
<div>
<button id="randColBtn" type="button">Randomize Colour</button>
<button id="randPosBtn" type="button">Randomize Position</button>
<button id="sortDotsBtn" type="button">Sort Dots</button>
</div>
<script src="dots.js"></script>
</body>
</html>
"use strict";
//Canvas and context
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d'),
//Buttons
randColBtn = document.getElementById('randColBtn'),
randPosBtn = document.getElementById('randPosBtn'),
sortDotsBtn = document.getElementById('sortDotsBtn'),
//Globals
dots = [],
color = '#AAAAAA';
//Constants
const DOT_RADIUS = 20;
//Dot constructor
var Dot = function(x, y, c) {
this.x = x;
this.y = y;
this.color = c;
this.draw = function() {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x-DOT_RADIUS/2, this.y-DOT_RADIUS/2, DOT_RADIUS, 0, 2 * Math.PI);
ctx.fill();
}
}
//Checks if the mouse has clicked within the boundaries of an existing dot
var isOverlap = function(d) {
for (var i = dots.length - 1; i >= 0; i--) {
let normalizedX = d.x - dots[i].x,
normalizedY = d.y - dots[i].y;
if (-DOT_RADIUS < normalizedX && normalizedX < DOT_RADIUS && -DOT_RADIUS < normalizedY && normalizedY < DOT_RADIUS) {
dots.splice(i,1);
return true;
}
}
return false;
}
var limit = 10;
var bouncing = [-100, 100];
var rand = (min, max) => ~~(Math.random() * (max - min + 1)) + min;
var randColor = ()=> "rgb(" + ~~(Math.random()*256) + "," + ~~(Math.random()*256) + "," + ~~(Math.random()*256) + ")";
//Event listeners
canvas.addEventListener("click", function(e) {
// just check position, no need to create dot here
if (!isOverlap({x: e.clientX, y:e.clientY})) {
// array of random dots
var newDots = [Array(limit||0)]
.map((v,i) => new Dot(
e.clientX + rand.apply(null, bouncing),
e.clientY + rand.apply(null, bouncing),
randColor()
))
// draw new dots
newDots.forEach(dot=>dot.draw());
//extend dots
dots = [dots, newDots];
} else {
ctx.clearRect(0,0,canvas.width,canvas.height);
for (var i = 0; i < dots.length; i++) {
dots[i].draw();
}
}
});
randColBtn.addEventListener("click", function(e) {
color = "rgb(" + Math.floor(Math.random()*256) + "," + Math.floor(Math.random()*256) + "," + Math.floor(Math.random()*256) + ")";
});
randPosBtn.addEventListener("click", function(e) {
ctx.clearRect(0,0,canvas.width,canvas.height);
for (var i = 0; i < dots.length; i++) {
dots[i].x = Math.random()*canvas.width;
dots[i].y = Math.random()*canvas.height;
dots[i].draw();
}
});
sortDotsBtn.addEventListener("click", function(e) {
ctx.clearRect(0,0,canvas.width,canvas.height);
for (var i = 0; i < dots.length; i++) {
dots[i].x = 2*DOT_RADIUS*i%canvas.width + 3/2*DOT_RADIUS;
dots[i].y = Math.floor(2*DOT_RADIUS * i/canvas.width) * 2*DOT_RADIUS + 3/2*DOT_RADIUS;
dots[i].draw();
}
});
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. |