how can I solve this problem ? they ask me that the var ctx is unavailable class

edited June 2016 in JavaScript Mode
var ctx;

PImage currentImage;
PImage previousImage;
final int cameraWidth = 960;
final int cameraHeight = 640;
final int shrinkFactor = 8;               // sort of arbitary, the amount the image will be shrunk for for motion detection
final float colorChangeThreshold = 50.0;  // again arbitary, the "amount" of color a pixel needs to change to be regarded as motion 

void setup() { 
    size(960,640);
    ctx = externals.context;    
}

void draw() {
    if (!video.available) return;

    // I want 2 images in memory, the current and previous image and the differences between these two things is motion!
    previousImage = currentImage;  

    pushMatrix();
    translate(width,0);
    scale(-1,1);                                            //mirror the video so it is like looking in a mirror
    ctx.drawImage(video, 0, 0, cameraWidth, cameraHeight);  //video is defined outside processing code
    popMatrix();

    currentImage=get(0,0,cameraWidth,cameraHeight);

    PImage motionDetectionImage = createImage(cameraWidth/shrinkFactor, cameraHeight/shrinkFactor, RGB);
    if (currentImage != null && previousImage != null) {    
        j = 0;
        for (int i = 0 ; i <  previousImage.pixels.length; i=nextI(i)) {
           color p1 = previousImage.pixels[i];
           color p2 = currentImage.pixels[i];

           float totalDiff =  abs(red(p1) - red(p2)) + abs( green(p1) - green(p2)) + abs(blue(p1) - blue(p2));

           if (totalDiff > colorChangeThreshold) { 
             motionDetectionImage.pixels[j] = color(255);
           } else {
             motionDetectionImage.pixels[j] = color(0);
           }           
           j = j + 1;
        }
        image(motionDetectionImage, 0, 0);
    }
}

private int nextI(int i) {
  if ( (int)( (int)(i+shrinkFactor) / (int)(cameraWidth) ) % (int) shrinkFactor == 0){
    return i + shrinkFactor;
  } else {
    return i + shrinkFactor + cameraWidth % shrinkFactor + (cameraWidth*(shrinkFactor-1));
  }
}

Answers

  • If you write in java, you do not use "var", but rather particular data type (which corresponds to the class you want to use, something like ExternalContext ctx;. When you do this ctx = externals.context; what do you expect ctx to be?

  • ctx is meant context .. How can I change it ? do you have any suggestions

  • Where did you see examples or guidance, which your program is based on? I see it has something to do with motion detection, but can't understand the logic..

    Context may mean different things in web or mobile applications..

  • So I want to make an installation with like falling objects (circles or flowers) and people can interact with by hand movement .. how it is possible ? if you have ant easier way for doing that

  • So, this is javascript... Processing.js is not evolving anymore, I believe, and I never used it, really. There's a lot of libraries to do it in java mode. Do you want this to be in javascript cause you want it for a web? I don't know much on the topic to help you make this work, but maybe you can find alternative solutions on what you want to do?

  • simliar like this

    I still want to use processing 3 and webcam to create an installtion

Sign In or Register to comment.