Hello,
Anyway, hopefully someone finds this sketch helpful. I used a timer class that I found someone else had made here on the forums (unfortunately I don't remember who wrote it, and I can't find it again) in order to set this up.
/*
thinking about learning how to think in order to more effectively learn
*/
as a n00b to processing and programming in general, I found this difficult to achieve. I searched around on the forums, and I didn't find any help specific to streams of continuous input (like cursor input) to trigger events.
Anyway, hopefully someone finds this sketch helpful. I used a timer class that I found someone else had made here on the forums (unfortunately I don't remember who wrote it, and I can't find it again) in order to set this up.
Use this patch to start a fade event based on cursor input. timer will then enter a 'hold' phase as long as there is input, and then fade out again.
To my untrained brain, it seems like this could be accomplished in a much simpler fashion. If anyone knows how to make it simpler than this, I would appreciate feedback. At this stage of my learning, this is what I came to.
Hopefully it comes in handy
- /*
- //////////////////////////////////
- //////////////////////////////////
- this patch allows for a fade in if there is mouse input.
- once mouse stops moving, the timer enters a 'hold' phase, and then a fadeout phase.
- if mouse input occurs during 'hold' phase, the hold phase continues until there is
- no more input. then the fadeout phase will begin.
- //////////////////////////////////
- //////////////////////////////////
- */
- //float curFade;
- float testFillR, testFillG;
- float easing = 0.05;
- float x, y, alph;
- boolean isMoving;
- boolean firstBit;
- boolean followBit = false;
- PVector mouseLoc;
- PVector easeLoc;
- Timer fadeTimer;
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /////setup setup setup setup setup setup setup setup setup setup setup setup setup setup setup setup
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- void setup() {
- size(200, 200);
- noStroke();
- fadeTimer = new Timer(500, 1000, 1000);
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /////draw draw draw draw draw draw draw draw draw draw draw draw draw draw draw draw draw draw draw
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- void draw() {
- background(0);
- mouseLoc = new PVector(mouseX, mouseY);
- easeMovements(mouseLoc.x, mouseLoc.y);//smooth out the movements of the mouse
- movementDetect(mouseLoc, easeLoc);
- trigger(isMoving, fadeTimer.isFinished());
- alph = fadeTimer.fadeValue();
- fill(75, 0, 170);
- ellipse(easeLoc.x, easeLoc.y, 40, 40);
- fill(testFillR, testFillG, 0);
- ellipse(160, 160, 40, 40);
- fill(40, 0, 190, alph);
- rectMode(CENTER);
- rect(100, 100, 80, 80);
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /////functions functions functions functions functions functions functions functions functions functions functions functions
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- void movementDetect(PVector e, PVector m) {
- if (e.dist(m) > 1.5){
- isMoving = true;
- }else{
- isMoving = false;
- }
- }
- void easeMovements(float targetX, float targetY){ // easing function from examples included with processing
- float dx = targetX - x;
- if(abs(dx) > 1){
- x += dx * easing;
- }
- float dy = targetY - y;
- if(abs(dy) > 1){
- y += dy * easing;
- }
- easeLoc = new PVector(x, y);
- }
- void trigger(boolean firstBit, boolean isFinished){
- if (firstBit != followBit){ //this compares the previous state to the current state. only executes on a state change
- if(firstBit){ // will execute when mouse is moved. this fades up to max & hold
- testFillG = 255;
- testFillR = 0;
- fadeTimer.startIn(true);
- }
- else
- { // will execute when mouse is not moving. this fades out to min and hold
- testFillG = 0;
- testFillR = 255;
- fadeTimer.startIn(false);
- }
- followBit = firstBit; //switch bits so that they are both the same, setting up similar state
- }
- }// end trigger
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ////classes classes classes classes classes classes classes classes classes classes classes classes
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- class Timer {
- int counter;
- int savedTime; // When Timer started
- int targetTime;
- int fadeInTime;
- int fadeOutTime;
- int fadeHoldTime;
- float mappedFade;
- boolean fadeIn;
- boolean fadeHold;
- Timer(int _fadeInTime, int _fadeOutTime, int _fadeHoldTime) {
- fadeInTime = _fadeInTime;
- fadeOutTime = _fadeOutTime;
- fadeHoldTime = _fadeHoldTime;
- fadeHold = false;
- }
- // Starting the timer
- void startIn(boolean _fadeIn) {
- fadeIn = _fadeIn;
- // When the timer starts it stores the current time in milliseconds.
- savedTime = millis();
- if (fadeIn){
- targetTime = fadeInTime;
- println("starting fadeIn " + savedTime + " sTime ");
- }else{
- savedTime = millis();
- targetTime = fadeHoldTime + fadeOutTime;
- println("starting fadeOut " + savedTime + " oTime ");
- }
- }
- float currentTime(){
- counter = millis() - savedTime;
- return counter;
- }
- float fadeValue(){
- counter = millis() - savedTime;
- if(fadeHold && fadeIn){
- mappedFade = 255;
- }
- else
- {
- if (fadeIn){
- if (counter < fadeInTime){
- mappedFade = map(counter, 0, fadeInTime, 0, 255);
- fadeHold = false;
- }
- if (counter > fadeInTime){
- mappedFade = 255;
- fadeHold = true;
- }
- }//end if fadeIn
- if (!fadeIn){
- if (counter < fadeHoldTime){
- mappedFade = 255;
- }
- if (counter < fadeHoldTime + fadeOutTime && counter > fadeHoldTime){
- mappedFade = map(counter, fadeHoldTime, targetTime, 255, 0);
- fadeHold = false;
- }
- }//end if !fadeIn
- }//end else of if fadehold
- return mappedFade;
- }//end fadeValue
- // The function isFinished() returns true if have passed.
- // The work of the timer is farmed out to this method.
- boolean isFinished() {
- // Check how much time has passed
- int passedTime = millis() - savedTime;
- if (passedTime > fadeHoldTime + fadeOutTime) {
- return true;
- } else {
- return false;
- }
- }//end isFinished
- }//end Timer class`
/*
thinking about learning how to think in order to more effectively learn
*/