Skip to content

Instantly share code, notes, and snippets.

@futuraprime
Created July 3, 2012 00:55
Show Gist options
  • Save futuraprime/3036702 to your computer and use it in GitHub Desktop.
Save futuraprime/3036702 to your computer and use it in GitHub Desktop.
A simple function to drop icons on divs.
.icon-container {
width: 64px;
height: 64px;
text-indent: -9999px;
position: relative;
color: #cc00aa; // the icon color is determined here
}
.icon-container canvas {
position: absolute;
top: 0;
left: 0;
}
function placeIcon(target, iconURL) {
var canvas = document.createElement('canvas'),
targetStyle = getComputedStyle(target);
ctx = canvas.getContext("2d"),
canvas.height = target.offsetHeight;
canvas.width = target.offsetWidth;
icon = new Image();
color = targetStyle.color.match(/\d{1,3}/g);
target.appendChild(canvas);
icon.src = iconURL;
icon.onload = function() {
ctx.drawImage(icon, 0, 0);
var imageData = ctx.getImageData(0,0,this.width,this.height),
data = imageData.data;
for (var i = 0, l = data.length; i < l; i += 4) {
data[i] = color[0];
data[i + 1] = color[1];
data[i + 2] = color[2];
}
ctx.putImageData(imageData, 0, 0);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment