/* * Virtual Playhead - Timed Item - Timed Text * http://subpixels.com * Version 0.2, 2009-01-29 */ // ----------------------------------------------------------------------------- // A timed item for text // Size, colour and position each have two key points (start and end) // The motionEstimate is set based on translation and rotation speeds // ----------------------------------------------------------------------------- public class TimedText extends TimedItem { // Font face PFont font; // Colour color c1; color c2; // Size float s1; float s2; // Content to display String content; // --------------------------------------------------------------------------- // Constructor // --------------------------------------------------------------------------- public TimedText(String content, float t1, float t2, color c1, color c2, float s1, float s2, float x1, float x2, float y1, float y2, float rot1, float rot2, PFont font) { super(t1, t2, x1, x2, y1, y2, rot1, rot2); this.content = content; this.c1 = c1; this.c2 = c2; this.s1 = s1; this.s2 = s2; this.font = font; // Handy to keep track of the duration so we don't have to recalculate every time duration = t2 - t1; // For rotation or spin, consider the text at its largest size rotating about // its centre, so that is with radius: // r = half the maximum text width textFont(font, max(s1, s2)); float radius = textWidth(content) * 0.5; motionEstimate = calcMotionEstimate(radius); if (content == "spinning") println("radius: "+radius+", motionEstimate: "+motionEstimate); } // --------------------------------------------------------------------------- // Public render method (these text entries know how to draw themselves) // --------------------------------------------------------------------------- public void render(float time) { // Nothing to render if we're outside of the start and end time if (!active(time)) return; // Calculate the position or "progress" (from 0.0 to 1.0) float pos = (time - t1) / duration; float sz = lerp(s1, s2, pos); textFont(font, sz); fill(lerpColor(c1, c2, pos)); // Offsets are so rotation appears to be about the centre of the text float xoff = textWidth(content) * 0.5; float yoff = -0.4 * sz; pushMatrix(); translate(lerp(x1, x2, pos) + xoff, lerp(y1, y2, pos) + yoff); rotate(lerp(rot1, rot2, pos)); text(content, -xoff, -yoff); popMatrix(); } }