Created
November 28, 2013 17:17
-
-
Save viko16/7695305 to your computer and use it in GitHub Desktop.
如何在32*32的黑白屏幕上显示出文字 #javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var d = document, c = d.createElement('CANVAS'); | |
c.height = c.width = 100; | |
var ctx = c.getContext('2d'); | |
ctx.textBaseline = "top"; | |
var hanzi2dianzhen = { | |
paintFont : function(char, size, font) { | |
ctx.clearRect(0, 0, c.width, c.height); | |
ctx.font = [size, 'px', ' ', font].join(''); | |
ctx.fillText(char, 0, 0); | |
}, | |
getDianZhen : function(x, y, percent, offsetX, offsetY) { | |
var result = [], imgData = ctx.getImageData(offsetX, offsetY, x, y); | |
for(var i = 0; i < x; i++) { | |
for(var j = 0; j < y; j++) result.push( imgData.data[ (i * y + j) * 4 + 3] / 255 > percent ? 1 : 0 ); | |
} | |
return result; | |
} | |
}; | |
var s = '官方30行js比赛:30行js你能做出什么?'.split(''), i = 0, size = 32; | |
(function() { | |
hanzi2dianzhen.paintFont(s[i++ % s.length], size, '宋体'); | |
var r = hanzi2dianzhen.getDianZhen(size, size, 0.3, 0, 0); | |
var ss = ''; | |
for(var j = 0; j < r.length;) { | |
ss += r[j] ? '*' : '+'; | |
if( ++j % size === 0) ss += '\n'; | |
} | |
document.body.innerHTML = '<pre style="font-family:Monaco,Menlo,Consolas,monospace; line-height: 0.6; font-size: 12px; color: black;">' + ss + '</pre>'; | |
setTimeout(arguments.callee, 500); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment