cdf
Version:
A library for creating oldschool demo-like animations with JavaScript
65 lines (55 loc) • 2.25 kB
JavaScript
(function(global){
var lib = global.cdf || {};
var vectorText = lib.vectorText = {
draw: function(dest, text, letterWidth, letterHeight, letterSpacing, strokeColor, strokeWidth, offsetX, offsetY){
var cp = 0;
letterSpacing = letterSpacing || 0;
if(letterWidth<0)letterSpacing*=-1;
var oldLineJoin = dest.ctx.lineJoin;
dest.ctx.lineJoin="round";
letters = text.toLowerCase().split('');
letters.forEach(function(letter){
vectorText.drawLetter(dest,vectorText.alphabet[letter], strokeColor, strokeWidth, letterWidth, letterHeight, cp+offsetX,offsetY);
cp+=letterWidth+letterSpacing;
});
dest.ctx.lineJoin=oldLineJoin;
},
dotCoordinates: function(num, w, h, ox, oy){
ox = ox || 0; oy = oy || 0;
num = parseInt(num, 10);
var y = Math.floor(num/3);
var x = num%3;
return [x * (w/2) + ox, y*(h/2) + oy];
},
drawLetter: function(dest, letter, strokeColor, strokeWidth, w, h, ox, oy){
ox = ox || 0; oy = oy || 0;
strokeColor = strokeColor || this.ctx.fillStyle;
strokeWidth = strokeWidth || this.ctx.lineWidth;
w = w || this.width;
h = h || this.height;
var lines = vectorText.plotLetter(letter,w,h, ox, oy);
lines.forEach(function(line){
dest.polyLine(line, strokeWidth, strokeColor);
});
},
plotLetter: function(letter, w, h, ox, oy){
ox = ox || 0; oy = oy || 0;
var lines = (''+letter).split('9');
var linesDots = [];
lines.forEach(function(line){
var dots = [];
line.split('').forEach(function(letter){
dots.push(vectorText.dotCoordinates(letter, w, h, ox, oy));
});
linesDots.push(dots);
});
return linesDots;
},
alphabet:{
a:602853,b:4206853,c:2068,d:510675,e:2068934,f:206934,g:206854,h:603528,i:201768,j:20176,k:6034248,l:860,m:60428,
n:2806,o:20682,p:60253,q:486028,r:6024348,s:203586,t:2017,u:2860,v:270,w:28460,x:80926,y:240947,z:8620,
0:820680,1:68710,2:86520,3:685420,4:82530,5:203586,6:2036853,7:7420,8:2068253,9:682035
}
};
if(typeof module!=='undefined'&& module.exports){module.exports=lib;}else{global.cdf=lib;}
})(this);