summaryrefslogtreecommitdiff
path: root/lasertext
diff options
context:
space:
mode:
authorTim Redfern <tim@getdrop.com>2022-08-15 23:44:37 +0100
committerTim Redfern <tim@getdrop.com>2022-08-15 23:44:37 +0100
commit1fd61e1304cba349b4c1233919c862ae22b2ecff (patch)
tree31c57ffe2f9bf9a817e2f8132b28a572cab21623 /lasertext
parente471ae5b7df7e5d7d0190b354846fa24b0ad9d61 (diff)
animate individual words
Diffstat (limited to 'lasertext')
-rw-r--r--lasertext/src/ofApp.cpp4
-rw-r--r--lasertext/src/ofApp.h139
2 files changed, 93 insertions, 50 deletions
diff --git a/lasertext/src/ofApp.cpp b/lasertext/src/ofApp.cpp
index 8e5a4cd..adb393f 100644
--- a/lasertext/src/ofApp.cpp
+++ b/lasertext/src/ofApp.cpp
@@ -25,7 +25,7 @@ void ofApp::setup(){
}
//--------------------------------------------------------------
void ofApp::update(){
-
+ banner.update(10.0f);
}
//--------------------------------------------------------------
@@ -33,7 +33,7 @@ void ofApp::draw(){
ofBackground(0,0,0);
ofSetColor(255,255,255);
- vector<colourPolyline> shapes = banner.getOutlines();
+ vector<colourPolyline> shapes = banner.getOutlines(0.06f+(0.02f*sin(ofGetElapsedTimef()*.1f)));
int num = laser.draw(shapes);
diff --git a/lasertext/src/ofApp.h b/lasertext/src/ofApp.h
index 46f6bbc..a15d918 100644
--- a/lasertext/src/ofApp.h
+++ b/lasertext/src/ofApp.h
@@ -26,20 +26,29 @@ public:
for (auto& v:outline) v.draw();
ofPopMatrix();
}
+ void randomiseColour(){
+ colour=ofColor::fromHsb(ofRandom(255.0),225,255);
+ }
char code;
float width;
vector<ofPolyline> outline;
ofColor colour;
};
-//how to deal with words
+class glyphWord{
+public:
+ glyphWord(){amount=1.0f;}
+ vector<glyph> glyphs;
+ float amount;
+};
class glyphbanner{
ofXml SVGFont;
- vector<glyph> glyphs;
+ vector<glyphWord> words;
vector<colourPolyline> outlines;
ofVec2f centre;
float lastUpdateTime;
+ float enspace;
vector<string> split(string s) {
size_t pos_start = 0, pos_end;
string token;
@@ -57,49 +66,64 @@ class glyphbanner{
public:
glyphbanner(){};
void init(string message){
- vector<string> m=split(message);
- for (auto& word: m){
- for (auto& c: word){
- addGlyph(c,ofColor::fromHsb(ofRandom(255.0),225,255));
- }
- addGlyph(' ');
- }
+ createWords(message);
lastUpdateTime=ofGetElapsedTimef();
}
- int length(){return glyphs.size();}
+ int length(){
+ int l=0;
+ for (auto& w:words) {
+ l+=w.glyphs.size();
+ }
+ return l+max(0,(int)words.size()-1);
+ }
float width(){
- float w=0.0f;
- for (auto& i:glyphs) w+=i.width;
- return w;
+ float _w=0.0f;
+ for (auto& w:words) {
+ for (auto& g:w.glyphs) _w+=g.width;
+ }
+ return _w+max((float)(words.size()-1)*enspace,0.0f);
}
string text(){
string s;
- for (auto& i:glyphs) s+=ofToString(i.code);
+ for (auto& w:words) {
+ for (auto& g:w.glyphs) s+=ofToString(g.code);
+ }
return s;
}
- vector<ofColor> colours(){
- vector<ofColor> c;
- for (auto& i:glyphs) c.push_back(i.colour);
- return c;
- }
void loadFont(filesystem::path path){
if( SVGFont.load(path) ){
- vector<glyph> g=glyphs;
+ vector<glyphWord> w=words;
clear();
- createGlyphs(g);
+ createWords(w);
+ enspace=getGlyph(' ').width;
ofLog()<<"loaded "<<path.stem().string();
}else{
ofLog()<<"unable to load "<<path<<" check data/ folder";
}
}
- void createGlyphs(string text){
- for (auto& c:text) addGlyph(c);
+ void createWords(string message){
+ clear();
+ vector<string> m=split(message);
+ for (auto& word: m){
+ glyphWord w;
+ for (auto& c: word){
+ w.glyphs.push_back(getGlyph(c,ofColor::fromHsb(ofRandom(255.0),225,255)));
+ }
+ words.push_back(w);
+ }
}
- void createGlyphs(vector<glyph> _g){
- for (auto& g:_g) addGlyph(g.code,g.colour);
+ void createWords(vector<glyphWord> _words){
+ clear();
+ for (auto& _w:_words) {
+ glyphWord w;
+ for (auto& g: _w.glyphs){
+ w.glyphs.push_back(getGlyph(g.code,g.colour));
+ }
+ words.push_back(w);
+ }
}
- void addGlyph(char c,ofColor col=ofColor(255,255,255)){
+ glyph getGlyph(char c,ofColor col=ofColor(255,255,255)){
vector<ofPolyline> shapes;
ofPolyline shape;
string elementPath = "/svg/defs/font/glyph[@unicode='"+ofToString(c)+"']";
@@ -126,37 +150,56 @@ public:
}
}
if (shape.size()) shapes.push_back(shape);
- glyphs.push_back(glyph(c,charWidth,shapes,col));
+ return glyph(c,charWidth,shapes,col);
}
- void removeGlyph(){glyphs.pop_back();}
- void clear(){glyphs.clear();}
- void randomiseColours();
- void draw(){
- float p=(-width())/2;
- ofPushMatrix();
- ofTranslate(ofGetWidth()/2,ofGetHeight()/2);
- ofScale(0.05,-0.05,0.05);
- for (auto& i:glyphs){
- i.draw(p,0);
- p+=i.width;
+ void addGlyph(char g,ofColor c){
+ if (g==' ') words.push_back(glyphWord());
+ else {
+ words[words.size()-1].glyphs.push_back(getGlyph(g,c));
+ }
+ }
+ void removeGlyph(){
+ if (words.size()){
+ glyphWord lw=words[words.size()-1];
+ lw.glyphs.pop_back();
+ if (!lw.glyphs.size()){
+ words.pop_back();
+ }
}
- ofPopMatrix();
}
- void update(){
+ void clear(){words.clear();}
+ void update(float period=1.0f){
+ float t=ofGetElapsedTimef()/period;
+ int theword=int(t)%words.size();
+ float segment=t-int(t);
//calculate params for word/letter anim
+ for (int i=0;i<words.size();i++){
+ words[i].amount=(i==theword?sin(segment*3.1415):0);
+ for (auto& g:words[i].glyphs){
+ if (ofRandom(period)<0.1) {
+ g.randomiseColour();
+ }
+ }
+ }
+
}
- vector<colourPolyline>& getOutlines(){
+ vector<colourPolyline>& getOutlines(float s=1.0f){
outlines.clear();
float p=(-width())/2;
- for (auto& i:glyphs){
- for (auto& o:i.outline){
- auto q=o;
- q.scale(.05,-.05);
- q.translate(glm::vec3(p*.05,0,0));
- outlines.push_back(colourPolyline(q,i.colour));
+ for (auto& w:words){
+ for (auto& g:w.glyphs){
+ if (w.amount>0.0f){
+ for (auto& o:g.outline){
+ auto q=o;
+ q.scale(s,-s);
+ q.translate(glm::vec3(p*s,0,0));
+ outlines.push_back(colourPolyline(q,g.colour*w.amount));
+ }
+ }
+ p+=g.width;
}
- p+=i.width;
+ p+=enspace;
}
return outlines;
}