Skip to content

Instantly share code, notes, and snippets.

@ssddi456
Last active August 29, 2015 14:14
Show Gist options
  • Save ssddi456/895cea3c7d62da48389f to your computer and use it in GitHub Desktop.
Save ssddi456/895cea3c7d62da48389f to your computer and use it in GitHub Desktop.
random text genaration
<meta charset="UTF-8">
<textarea name="" id="in" cols="30" rows="10">
here is some random direct text!
happy chart;
这是一段随机文字
</textarea>
<div>
<label>chaos_lv</label>
<input type="radio" value="1" name="chaos_lv" checked> 1
<input type="radio" value="1.5" name="chaos_lv"> 1.5
<input type="radio" value="6" name="chaos_lv"> 6
<input type="radio" value="12" name="chaos_lv"> 12
<input type="radio" value="18" name="chaos_lv"> 18
</div>
<h2>
svg
</h2>
<p id="out_svg"></p>
<h2> canvas draw with the img below, <span style="background:black;color:red;">copy this one!!!</span></h2>
<canvas id="out" style=""></canvas>
<script src="http://cdn.staticfile.org/jquery/2.1.1-rc2/jquery.min.js"></script>
<script src="http://cdn.staticfile.org/underscore.js/1.2.4/underscore.js"></script>
<h2>
img with data-url from the svg above, <span style="font-size:10px;">copy this seems ok, too</span>
</h2>
<textarea name="" id="template" style="display:none;">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
width="<%=width%>px"
height="<%=height%>px" version="1.1">
<rect fill="#ffffff" x="0" y="0" width="<%=width%>" height="<%=height%>"></rect>
<foreignObject x="0" y="0" width="<%=width%>" height="<%=height%>">
<%=lines%>
</foreignObject>
</svg>
</textarea>
<script>
"use strict";
/*\
|*|
|*| Base64 / binary data / UTF-8 strings utilities
|*|
|*| https://developer.mozilla.org/en-US/docs/Web/JavaScript/Base64_encoding_and_decoding
|*|
\*/
/* Array of bytes to base64 string decoding */
function b64ToUint6 (nChr) {
return nChr > 64 && nChr < 91 ?
nChr - 65
: nChr > 96 && nChr < 123 ?
nChr - 71
: nChr > 47 && nChr < 58 ?
nChr + 4
: nChr === 43 ?
62
: nChr === 47 ?
63
:
0;
}
function base64DecToArr (sBase64, nBlocksSize) {
var
sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""), nInLen = sB64Enc.length,
nOutLen = nBlocksSize ? Math.ceil((nInLen * 3 + 1 >> 2) / nBlocksSize) * nBlocksSize : nInLen * 3 + 1 >> 2, taBytes = new Uint8Array(nOutLen);
for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) {
nMod4 = nInIdx & 3;
nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4;
if (nMod4 === 3 || nInLen - nInIdx === 1) {
for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) {
taBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255;
}
nUint24 = 0;
}
}
return taBytes;
}
/* Base64 string to array encoding */
function uint6ToB64 (nUint6) {
return nUint6 < 26 ?
nUint6 + 65
: nUint6 < 52 ?
nUint6 + 71
: nUint6 < 62 ?
nUint6 - 4
: nUint6 === 62 ?
43
: nUint6 === 63 ?
47
:
65;
}
function base64EncArr (aBytes) {
var nMod3 = 2, sB64Enc = "";
for (var nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) {
nMod3 = nIdx % 3;
// if (nIdx > 0 && (nIdx * 4 / 3) % 76 === 0) { sB64Enc += "\r\n"; }
nUint24 |= aBytes[nIdx] << (16 >>> nMod3 & 24);
if (nMod3 === 2 || aBytes.length - nIdx === 1) {
sB64Enc += String.fromCharCode(uint6ToB64(nUint24 >>> 18 & 63), uint6ToB64(nUint24 >>> 12 & 63), uint6ToB64(nUint24 >>> 6 & 63), uint6ToB64(nUint24 & 63));
nUint24 = 0;
}
}
return sB64Enc.substr(0, sB64Enc.length - 2 + nMod3) + (nMod3 === 2 ? '' : nMod3 === 1 ? '=' : '==');
}
/* UTF-8 array to DOMString and vice versa */
function UTF8ArrToStr (aBytes) {
var sView = "";
for (var nPart, nLen = aBytes.length, nIdx = 0; nIdx < nLen; nIdx++) {
nPart = aBytes[nIdx];
sView += String.fromCharCode(
nPart > 251 && nPart < 254 && nIdx + 5 < nLen ? /* six bytes */
/* (nPart - 252 << 30) may be not so safe in ECMAScript! So...: */
(nPart - 252) * 1073741824 + (aBytes[++nIdx] - 128 << 24) + (aBytes[++nIdx] - 128 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
: nPart > 247 && nPart < 252 && nIdx + 4 < nLen ? /* five bytes */
(nPart - 248 << 24) + (aBytes[++nIdx] - 128 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
: nPart > 239 && nPart < 248 && nIdx + 3 < nLen ? /* four bytes */
(nPart - 240 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
: nPart > 223 && nPart < 240 && nIdx + 2 < nLen ? /* three bytes */
(nPart - 224 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
: nPart > 191 && nPart < 224 && nIdx + 1 < nLen ? /* two bytes */
(nPart - 192 << 6) + aBytes[++nIdx] - 128
: /* nPart < 127 ? */ /* one byte */
nPart
);
}
return sView;
}
function strToUTF8Arr (sDOMStr) {
var aBytes, nChr, nStrLen = sDOMStr.length, nArrLen = 0;
/* mapping... */
for (var nMapIdx = 0; nMapIdx < nStrLen; nMapIdx++) {
nChr = sDOMStr.charCodeAt(nMapIdx);
nArrLen += nChr < 0x80 ? 1 : nChr < 0x800 ? 2 : nChr < 0x10000 ? 3 : nChr < 0x200000 ? 4 : nChr < 0x4000000 ? 5 : 6;
}
aBytes = new Uint8Array(nArrLen);
/* transcription... */
for (var nIdx = 0, nChrIdx = 0; nIdx < nArrLen; nChrIdx++) {
nChr = sDOMStr.charCodeAt(nChrIdx);
if (nChr < 128) {
/* one byte */
aBytes[nIdx++] = nChr;
} else if (nChr < 0x800) {
/* two bytes */
aBytes[nIdx++] = 192 + (nChr >>> 6);
aBytes[nIdx++] = 128 + (nChr & 63);
} else if (nChr < 0x10000) {
/* three bytes */
aBytes[nIdx++] = 224 + (nChr >>> 12);
aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
aBytes[nIdx++] = 128 + (nChr & 63);
} else if (nChr < 0x200000) {
/* four bytes */
aBytes[nIdx++] = 240 + (nChr >>> 18);
aBytes[nIdx++] = 128 + (nChr >>> 12 & 63);
aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
aBytes[nIdx++] = 128 + (nChr & 63);
} else if (nChr < 0x4000000) {
/* five bytes */
aBytes[nIdx++] = 248 + (nChr >>> 24);
aBytes[nIdx++] = 128 + (nChr >>> 18 & 63);
aBytes[nIdx++] = 128 + (nChr >>> 12 & 63);
aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
aBytes[nIdx++] = 128 + (nChr & 63);
} else /* if (nChr <= 0x7fffffff) */ {
/* six bytes */
aBytes[nIdx++] = 252 + (nChr >>> 30);
aBytes[nIdx++] = 128 + (nChr >>> 24 & 63);
aBytes[nIdx++] = 128 + (nChr >>> 18 & 63);
aBytes[nIdx++] = 128 + (nChr >>> 12 & 63);
aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
aBytes[nIdx++] = 128 + (nChr & 63);
}
}
return aBytes;
}
function btoa ( str ) {
return base64EncArr(strToUTF8Arr(str));
}
var svg = _.template($('#template').text());
var svg_b64 = function(opt) {
var svg_str= $(svg(opt))[0];
$('#out_svg').empty().append(svg_str);
var svg_blob = "data:image/svg+xml;charset=UTF-8;base64,"+
btoa(
new XMLSerializer()
.serializeToString(svg_str));
return svg_blob;
};
var canvas = $('#out')[0];
var ctx = canvas.getContext('2d');
var image = $('<img src="" alt="" />').appendTo('body')[0];
image.onload = function() {
ctx.drawImage( image, 0, 0 );
}
var chaos_lv = 1;
$('[name=chaos_lv]').on('click',function() {
setTimeout(function() {
chaos_lv = $('[name=chaos_lv]:checked').val();
draw();
});
});
function draw () {
var val = $('#in').val().split('');
var lines = ['<p xmlns="http://www.w3.org/1999/xhtml" style="font-size:16px;padding:0;font-family:Arial 宋体;">',
val.map(function( chr) {
if(chr == '\n'){
return '<br />';
}
if( chr == ' ' ){
return '<span style="padding-left:4px;"></span>'
}
return '<span style="display:inline-block;transform:rotate('
+ Math.floor(Math.random() * 20 * chaos_lv )
* (Math.random() > 0.5 ? -1 : 1 )
+ 'deg)">'
+ chr +
'</span>';
}).join(''),
'<p>'].join('');
var $lines= $('<p></p>')
.append($(lines))
.appendTo('body')
.hide();
var svg = svg_b64({
lines : lines,
width : (canvas.width = $lines.width() + 30),
height: (canvas.height = $lines.height())
});
$lines.remove();
image.src = svg;
}
$('#in').on('keyup',function() {
setTimeout(draw);
}).trigger('keyup');
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment