Skip to content

Instantly share code, notes, and snippets.

@2bbb
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save 2bbb/e470740575fdeb1aa92a to your computer and use it in GitHub Desktop.

Select an option

Save 2bbb/e470740575fdeb1aa92a to your computer and use it in GitHub Desktop.
輝かしい未来に向けての準備
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
for(int i = 0; i < 20; i++) {
ofRectangle rect;
rect.width = ofRandomWidth() * 0.5f;
rect.height = ofRandomHeight() * 0.5f;
rect.x = ofRandom(ofGetWidth() - rect.width);
rect.y = ofRandom(ofGetHeight() - rect.height);
rects.push_back(rect);
ofColor c;
c.r = ofRandom(255.0f);
c.g = ofRandom(255.0f);
c.b = ofRandom(255.0f);
colors.push_back(c);
}
}
//--------------------------------------------------------------
void ofApp::update(){
float t = ofGetElapsedTimef();
for(int i = easings.size() - 1; 0 <= i; i--) {
easings[i](t);
if(easings[i].isComplete()) {
easings.erase(easings.begin() + i);
}
}
}
//--------------------------------------------------------------
void ofApp::draw(){
for(int i = 0; i < rects.size(); i++) {
ofSetColor(colors[i]);
ofRect(rects[i]);
}
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
if(key == 'c') {
for(int i = 0; i < rects.size(); i++) {
class Ease {
public:
Ease(ofColor &c, ofRectangle &r) : color(c), rect(r) {
from.set(c);
to.set(ofColor(ofRandom(255), ofRandom(255), ofRandom(255)));
fromRect.set(r);
toRect.width = ofRandomWidth() * 0.5f;
toRect.height = ofRandomHeight() * 0.5f;
toRect.x = ofRandom(ofGetWidth() - toRect.width);
toRect.y = ofRandom(ofGetHeight() - toRect.height);
}
void operator()(float t) {
color.r = ofLerp(from.r, to.r, t);
color.g = ofLerp(from.g, to.g, t);
color.b = ofLerp(from.b, to.b, t);
rect.width = ofLerp(fromRect.width, toRect.width, t);
rect.height = ofLerp(fromRect.height, toRect.height, t);
rect.x = ofLerp(fromRect.x, toRect.x, t);
rect.y = ofLerp(fromRect.y, toRect.y, t);
}
ofColor from;
ofColor to;
ofColor &color;
ofRectangle fromRect;
ofRectangle toRect;
ofRectangle &rect;
} f(colors[i], rects[i]);
ofx::LambdaEasing easing;
easing.setup(f, ofRandom(2) + 1, ofRandom(1));
easings.push_back(easing);
}
}
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
#pragma once
#include "ofMain.h"
#include "ofxLambdaEasing.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
private:
vector<ofx::LambdaEasing> easings;
vector<ofRectangle> rects;
vector<ofColor> colors;
};
//
// ofxLambdaEasing.h
// ofxLambdaEasing
//
// Created by ISHII 2bit on 2014/06/15.
//
//
#ifndef __ofxLambdaEasing__
#define __ofxLambdaEasing__
#define BEGIN_NAMESPACE(name) namespace name {
#define END_NAMESPACE(name) };
#include <tr1/functional>
#include "ofMain.h"
BEGIN_NAMESPACE(ofx);
class LambdaEasing {
public:
typedef std::tr1::function<void(float)> EasingFunction;
void setup(EasingFunction f, float duration, float delay) {
this->f = f;
this->duration = duration;
this->start = ofGetElapsedTimef();
this->delay = delay;
this->_isComplete = false;
}
void operator()(float currentTime) {
if(currentTime < start + delay) return;
float t = (currentTime - start - delay) / duration;
if(1.0 <= t) {
t = 1.0;
_isComplete = true;
}
f(t);
}
bool isComplete() {
return _isComplete;
}
private:
EasingFunction f;
float start;
float duration;
float delay;
bool _isComplete;
};
END_NAMESPACE(ofx);
#endif /* defined(__ofxLambdaEasing__ofxLambdaEasing__) */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment