millis() 実行してから今までの時間をms単位で示している。1000msで1秒。
1 2 3 4 5 6 7 8 9 10 |
int value = 0; void draw() { fill(value); rect(25, 25, 50, 50); } void mouseClicked() { println(millis());//クリックすると表示 } |
void drawの後半、以下のコードで、キーボードを最後に押した時の時間と、現在の時間差を計算してフォントサイズに使っている。
1 |
float timeDelta = millis() - pMillis;// |
次のサンプルは、キーを押す度に「現在の時間」から「1個前の時間PMillis」を差し引いてPMillisに再代入している。そしてPMillisをフォントサイズに活用している。時間が開けば開くほどフォントは大きくなる。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
float PMillis; void setup(){ size(600,600); PMillis=0; } void draw(){ fill(0,10); rect(0,0,width,height); fill(255); println(PMillis); textSize(PMillis/50); text(str(PMillis),0,height); text("text",width/2,height/2); } void keyPressed(){ PMillis=millis()-PMillis; } |
文字列と配列
入力した文字はtextTypedに普通に文字列として追加されている。フォントサイズはfontSizesになんと配列で追加されている。なので毎フレームごとに、textTypedのi番目の文字をfontSizesの配列の[i]番目に入っているサイズで描画している。
以下はtextTypedという文字列の最後にkeyの中に含まれる文字列を追加している。
1 |
textTyped += key;//文字列追加 |
以下は、fontSizesという配列の、最後尾にnewFontSizeを追加する。
1 |
fontSizes = append(fontSizes, newFontSize); |
以下は、textTypedという文字列の、i番目を一文字取り出して、letterに入れています。charは1文字だけを入れることができるクラスです。基本的にはStringと文字数以外は変わらないと思います。
1 |
char letter = textTyped.charAt(i); |
ただし、charに文字列を指定する場合には以下に注意してください。
1 2 3 4 5 6 7 |
//charに文字列を入れる場合の注意 char a ='a';//シングルクォーテーションはいける println(a); char b ="b";//ダブルクオーテーションはだめ println(b); |
文字列textTypedから0番目からはじめて(textTyped.length-1)個を返す。例えば3文字からなる文字列にこれをおこなえば、o番目の文字から2個返すので、先頭から2文字が返される。
1 |
textTyped = textTyped.substring(0,(textTyped.length-1));// |
以下引用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
// P_3_1_1_01.pde // Generative Gestaltung, ISBN: 978-3-87439-759-9 // First Edition, Hermann Schmidt, Mainz, 2009 // Hartmut Bohnacker, Benedikt Gross, Julia Laub, Claudius Lazzeroni // Copyright 2009 Hartmut Bohnacker, Benedikt Gross, Julia Laub, Claudius Lazzeroni // // http://www.generative-gestaltung.de // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * typewriter. time reactive. * * MOUSE * position y : adjust spacing (line height) * * KEYS * a-z : text input (keyboard) * backspace : delete last typed letter * ctrl : save png + pdf */ import processing.pdf.*; import java.util.Calendar; boolean doSave = false; String textTyped = "Type slow and fast!"; float[] fontSizes = new float[textTyped.length()]; float minFontSize = 15; float maxFontSize = 800; float newFontSize = 0; int pMillis; float maxTimeDelta = 5000.0; float spacing = 2; // line height float tracking = 0; // between letters PFont font; void setup() { size(800, 600); // make window resizable surface.setResizable(true); font = createFont("Arial",10); smooth(); noCursor(); // init fontSizes for (int i = 0; i < textTyped.length(); i++) { fontSizes[i] = minFontSize; } pMillis = millis(); } void draw() { if (doSave) beginRecord(PDF, timestamp()+".pdf"); background(255); textAlign(LEFT); fill(0); noStroke(); spacing = map(mouseY, 0,height, 0,120); translate(0, 200+spacing); float x = 0, y = 0, fontSize = 20;//文字をうつ場所がxとy for (int i = 0; i < textTyped.length(); i++) { // get fontsize for the actual letter from the array fontSize = fontSizes[i]; textFont(font, fontSize); char letter = textTyped.charAt(i); //テキスト一文字の横幅 を足すことで、文字を配置する位置を求めている float letterWidth = textWidth(letter) + tracking;// //横幅を超える場合には、下に改行 if (x+letterWidth > width) { // start new line and add line height x = 0; y += spacing;//この分、下にずらしている } // draw letter text(letter, x, y); // update x-coordinate for next letter x += letterWidth;//次の文字のためにx座標をズラす } // blinking cursor after text //常にフォントサイズは計算している float timeDelta = millis() - pMillis;//ここで現在時刻と最後にキーをおした時の時間差を出し、フォントサイズに使っている //0〜maxTimeDeltaまでの値を、minFontSize〜maxFontSizeに変換。あまりに小さかったり大きかったりさせない。 newFontSize = map(timeDelta, 0,maxTimeDelta, minFontSize,maxFontSize); //min(,)はその中で一番小さい数字を返す。つまりここではmaxFontsizeより大きい値は出力されなくなる。 newFontSize = min(newFontSize, maxFontSize);//このnewFontSizeがキーをおした時に配列に入る fill(200, 30, 40); //スタートしてから描かれたフレーム数 がある条件のときに白にする。点滅用。 if (frameCount/10 % 2 == 0) fill(255); rect(x, y, newFontSize/2, newFontSize/20); if (doSave) { doSave = false; endRecord(); saveFrame(timestamp()+"_##.png"); } } void keyReleased() { // export pdf and png if (keyCode == CONTROL) doSave = true; } void keyPressed() { if (key != CODED) { switch(key) { case DELETE://breakがないのでdeleteもbackspaceも同じ動作をする case BACKSPACE: if (textTyped.length() > 0) { //substringは文字列から(a,b) a番目からb個の文字を取り出して返す textTyped = textTyped.substring(0,max(0,textTyped.length()-1));// fontSizes = shorten(fontSizes);//配列の最後の文字を取り除いて短くした配列を返す } break; // disable those keys case TAB: case ENTER: case RETURN: case ESC: break; default: textTyped += key;//文字列追加 fontSizes = append(fontSizes, newFontSize);//配列にフォントサイズ用の値を追加 } // reset timer pMillis = millis();//現在時刻をキーを押した瞬間に測定 } } // timestamp String timestamp() { Calendar now = Calendar.getInstance(); return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now); } |
One comment