Search code examples
javascriptstringif-statementprocessingloadimage

Loading image to canvas by string in processing


Brand new to this and thought i'd give processing a go.

How can I get an image to load to the canvas by a string input? So far I have managed to make the image load if key is pressed e.g:

if (key == 't' || key == 't') {
img=loadImage ("thebarleymow.jpg");
image(img,0,200);
}
}

What would be the process of making it load through a series of characters? i.e Type 'badger' img=loadImage ("badger.jpg");

Look forward to hearing back. Thanks


Solution

  • Here is a rough code sample.
    Whenever a user inputs a correct image name, the image will be loaded.
    I hope this will help.

    var target = "badger";
    var inputs = [];
    document.onkeydown = function (e){  
        e = e || window.event; // for InternetExplorer
        inputs.push(String.fromCharCode(e.keyCode));
        var testStr = inputs.join("");
        if (target.substr(0,inputs.length).toUpperCase() !== testStr){
            inputs = [];
            return;
        }
        if(testStr.length === target.length){
            //console.log("you inputed image name!");
            img=loadImage (target + ".jpg");
            image(img,0,200);
        }
    };