Animert tekst
Du kan jo se hvordan teksten blir til og forsøke å identifisere forfatteren før teksten er ferdig ordnet.
Trykk her for å starte en tekst:
Det er tre involverte processing-filer:
_kaos2.pde
/* Caotic text Reading from an url, splitting and showing or receiving text as a string array Animate a process where we find neighbours and joins to lines */ /**-------- globals ----*/ // datasource data article; int TEXTSIZE=18; PFont MovingText; int FINALTEXTSIZE=14; PFont FinalText; int W=600; int H=400; boolean LINESREADY=false; int MARGIN=3; // used frams on current data (article) int FRAMESONDATA=0; void setup(){ size(W,H); fill(0); FinalText=createFont("Arial Bold",FINALTEXTSIZE); MovingText = createFont("Arial",TEXTSIZE); textFont(MovingText); smooth(); article=null; /* while testing String[] tst={"dette er linje nummer en","dette er linje nummer to"}; setData(tst,"what");*/ } void draw(){ background(255); if(article!=null){ article.moveWords(); if(FRAMESONDATA > 60) article.joinWords(); article.drawMe(); } FRAMESONDATA++; } /*----- from website ----*/ // data direct void setData(String[] t,String title){ article=new data(t,title); article.randomizeWords(); FRAMESONDATA=0; } // an url to load void loadData(String address,String title){ // give it some space //size(600,400); // make the collected text LINESREADY=false; String[]lines=loadStrings(address); setData(lines,title); FRAMESONDATA=0; }
og
_data.pde
// holding a text as lines and as list of words class data{ String title; String[] lines; word[] words; // as words are joined the wordcount decreases int activeWords; data(String[] lin,String t){ title=t; lines=lin; // how many words in all (avoid stringarray) int wordcount=0; for(int ix=0;ix < lines.length;ix++) wordcount+=lines[ix].split(" ").length; words=new word[wordcount]; // assign word values wordcount=0; for(int ix=0;ix < lines.length;ix++){ String[] wordsinline=lines[ix].split(" "); for(int wix=0;wix<wordsinline.length;wix++) words[wordcount++]=new word(wordsinline[wix],wix==wordsinline.length-1); } activeWords=words.length; } void drawMe(){ if(LINESREADY) { fill(255,255,0); rect(0,0,W,H); fill(0); text(title,20,20); } for(int ix=0;ix < activeWords;ix++){ words[ix].drawMe(); } } // give all words random speeds // from a center position void randomizeWords(){ // some randomizing for(int ix=0;ix < activeWords;ix++){ words[ix].x=(W-textWidth(words[ix].t))/2; words[ix].y=(H-TEXTSIZE+2)/2; words[ix].dx=random(-5,5); words[ix].dy=random(-5,5); } LINESREADY=false; } // move and reflect void moveWords(){ for(int ix=0;ix < activeWords;ix++){ word w=words[ix]; w.x+=w.dx; w.y+=w.dy; if((w.x > W-textWidth(w.t))||(w.x < 0)){ w.dx=-w.dx; w.x+=w.dx; } if((w.y > H-TEXTSIZE)||(w.y < TEXTSIZE)){ w.dy=-w.dy; w.y+=w.dy; } } } // check if all words are part of a line boolean onlyReadyLines(){ for(int ix=0;ix < activeWords;ix++) if(!words[ix].eofLine) return false; return true; } // calculate pos and freeze all words (lines) void organizeLines(){ int maxLength=0; for(int ix=0;ix <activeWords;ix++){ maxLength=Math.max(maxLength,words[ix].t.length()); } float top= H/2 - (activeWords*(FINALTEXTSIZE+MARGIN))/2; for(int ix=0;ix <activeWords;ix++){ words[ix].x=20;//W/2-maxLength/2; words[ix].y=top+ix*(FINALTEXTSIZE+MARGIN); words[ix].dx=0; words[ix].dy=0; } LINESREADY=true; } // joining consecutive words when not eol void joinWords(){ if((!LINESREADY)&& (activeWords-lines.length < 2)){ // clean up final word for(int ix=0;ix< lines.length;ix++){ words[ix]=new word(lines[ix],true); } activeWords=lines.length; LINESREADY=true; } // no loose words ? if(onlyReadyLines()){ organizeLines(); return; } int ix=activeWords-2; while(ix >=0){ if( (!words[ix].eofLine) && (words[ix].touch(words[ix+1]))){ // doit words[ix].append(words[ix+1]); // move up and truncate for(int ix2=ix+1; ix2 < activeWords-1; ix2++){ words[ix2]=words[ix2+1]; } activeWords--; } ix--; } } }
og
_word.pde
// handlig a single word // or as words are joined a (part of) a sentence class word{ String t; float x; float y; float dx; float dy; float w; boolean eofLine; word(String t,boolean eofLine){ this.t=t; this.eofLine=eofLine; textFont(MovingText); this.w=textWidth(t); } void drawMe(){ // simply draw ready lines, frozen as set in // data.organizeLines() if(LINESREADY){ textFont(FinalText ); text(t,x,y); return; } textFont(MovingText); if(eofLine) { // this is eof a line stroke(0); fill(255,255,0); rect(x-MARGIN,y+MARGIN,w+MARGIN*2,-TEXTSIZE-MARGIN); fill(0); text(t,x,y); } else{ // this is a loose word fill(255); rect(x-MARGIN,y+MARGIN,w+MARGIN*2,-TEXTSIZE-MARGIN); //fill(0); //text(t,x,y); } } // do I overlap the other word boolean touch(word w1){ int space=2*MARGIN; if((x+w+space < w1.x-space)||(w1.x+w+space < x-space)) return false; if((y+TEXTSIZE+space< w1.y-space)||(w1.y+TEXTSIZE+space < y-space)) return false; return true; } // join a line with the next word void append(word w1){ t=t+" "+w1.t; this.eofLine=w1.eofLine; this.w=textWidth(t); this.x=random(MARGIN,W-w-MARGIN); } }
Den sentrale funksjonen i jacaskriptet som kaller skissen er slik:
_kaos2.js
// give processing a string to show function selectRandomText(){ // texta are in a json list, select random var ix=Math.floor(Math.random() *S.list.length); var pjs=Processing.getInstanceById(CANVASID); pjs.setData(S.list[ix].lines,S.list[ix].title); }