Skip to content

Instantly share code, notes, and snippets.

@st98
Last active August 29, 2015 14:06
Show Gist options
  • Save st98/1456ccfdb3f1196f9787 to your computer and use it in GitHub Desktop.
Save st98/1456ccfdb3f1196f9787 to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Icon</title>
<style>
body {
width: 30em;
margin: 0 auto;
font-family: Consolas, Meiryo, monospace;
}
h1, a {
color: #888;
}
h1::before {
position: absolute;
margin-left: -.75em;
color: #ccc;
content: '+';
}
a {
padding: .2em 0;
border-bottom: 2px dotted #888;
text-decoration: none;
}
a:hover {
color: #555;
background: #ccc;
}
canvas {
display: block;
margin: 1em 0;
cursor: pointer;
}
</style>
</head>
<body>
<h1>icon.js</h1>
<canvas id="cv" width="256" height="256"></canvas>
<p>code: <a href="icon.js">icon.js</a></p>
<script src="icon.js"></script>
</body>
</html>
window.addEventListener('load', function main() {
var cv, ctx;
function randint(min, max) {
if (typeof max === 'undefined') {
max = min;
min = 0;
}
return Math.floor(Math.random() * (max - min + 1) + min);
}
function color(r, g, b) {
return 'rgb(' + [r, g, b].join(', ') + ')';
}
function ig(x, d) {
return x > d ? d : x;
}
function render(ctx, n) {
var cv, width, height, x, y, w, h, mx, my;
cv = ctx.canvas;
width = cv.width;
height = cv.height;
ctx.fillStyle = color(255, 255, 255);
ctx.fillRect(0, 0, width, height);
ctx.fillStyle = color(0, 0, 0);
w = width / n;
h = height / n;
for (x = 0; x < n; x++) {
for (y = 0; y < n; y++) {
mx = 255 * Math.abs(n - x) / n;
my = 255 * Math.abs(n - y) / n;
ctx.fillStyle = color(
randint(ig(mx - 16, 255), ig(mx + 16, 255)),
randint(ig(my - 16, 255), ig(my + 16, 255)),
96
);
ctx.fillRect(x * w, y * h, w, h);
}
}
}
cv = document.getElementById('cv');
ctx = ctx = cv.getContext('2d');
render(ctx, 32);
cv.addEventListener('mousemove', render.bind(null, ctx, 32), false);
cv.addEventListener('click', function () {
window.open(cv.toDataURL());
});
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment