├── example ├── abcs.png ├── words.png ├── lettercrap.png └── index.html ├── lettercrap.css ├── LICENSE ├── README.markdown └── lettercrap.js /example/abcs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nate-parrott/lettercrap/HEAD/example/abcs.png -------------------------------------------------------------------------------- /example/words.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nate-parrott/lettercrap/HEAD/example/words.png -------------------------------------------------------------------------------- /example/lettercrap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nate-parrott/lettercrap/HEAD/example/lettercrap.png -------------------------------------------------------------------------------- /lettercrap.css: -------------------------------------------------------------------------------- 1 | [data-letter-crap] { 2 | white-space: pre; 3 | overflow: hidden; 4 | font-size: 10px; 5 | line-height: 10px; 6 | font-family: monospace; 7 | padding: 0; 8 | } 9 | -------------------------------------------------------------------------------- /example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |Customize the letters used with the data-lettercrap-letters='ABC':
Randomly throw specific words into the mix using data-lettercrap-words='apple banana peach':
6 |
7 | [Here's a live demo](https://nate-parrott.github.io/lettercrap)
8 |
9 | ## Usage
10 |
11 | To use _Lettercrap_, import `lettercrap.js` and `lettercrap.css`. Create a `div` with the `data-letter-crap` attribute with the source of a black-and-white image, which will serve as the shape of the generated ascii art:
12 |
13 |
14 |
15 | Make sure to set a height for your `div`. If you want to set a dynamic height based on the div's fluid width, you can specify an aspect ratio using the `data-lettercrap-aspect-ratio` attribute, and _Lettercrap_ will set the height of the `div` for you:
16 |
17 |
18 |
19 | By default, _Lettercrap_ uses `0`s, `1`s and the occasional `_` to fill in the shape of your image. You can change the set of letters used with the `data-lettercrap-letters` attribute:
20 |
21 |
22 |
23 | _Lettercrap_ can also throw the occasional _full word_ into the mix—specify the words to choose from using the `data-lettercrap-words` attribute:
24 |
25 |
26 |
27 | Check out the `examples/index.html` file to see exactly how this all fits together.
28 |
29 |
--------------------------------------------------------------------------------
/lettercrap.js:
--------------------------------------------------------------------------------
1 | (function() {
2 | var charWidth = 6;
3 | var charHeight = 10;
4 | var likelihoodOfReplacingWord = 0.05;
5 | var likelihoodOfChangingExistingText = 0.1;
6 |
7 | function randomChoice(x) {
8 | return x[Math.floor(Math.random() * x.length)];
9 | }
10 |
11 | function initElement(element) {
12 | var img = new Image();
13 | img.onload = function() {
14 | render(element, img, null);
15 | }
16 | img.src = element.getAttribute('data-letter-crap');
17 | }
18 |
19 | function getTextContentWithImageAtSize(image, width, height, existingText, words, letters) {
20 | existingText = existingText ? existingText.replace(/\r?\n|\r/g, '') : null;
21 | var shouldReplaceExisting = function() { return !existingText || Math.random() < likelihoodOfChangingExistingText };
22 |
23 | var canvas = document.createElement('canvas');
24 | canvas.width = parseInt(width / charWidth);
25 | canvas.height = parseInt(height / charHeight);
26 | canvas.getContext('2d').drawImage(image, 0, 0, canvas.width, canvas.height);
27 | var data = canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height);
28 | var chars = "";
29 | var startOfFilledInSequence = 0;
30 | var i = 0;
31 | for (var y=0; y