The basic pattern
var jsLogo = loadImage("https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Unofficial_JavaScript_logo_2.svg/200px-Unofficial_JavaScript_logo_2.svg.png"); var draw = function(){ background(255); image(jsLogo, 0, 0); };
A better implementation
To be more verbose, readable, we can use Object Literals
var JavaScriptLogo = { logo: loadImage("https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Unofficial_JavaScript_logo_2.svg/200px-Unofficial_JavaScript_logo_2.svg.png"), x: 0, y: 0, draw: function(){ image(this.logo, this.x, this.y); } }; var draw = function(){ JavaScriptLogo.draw(); };
A reusable implementation
Finally, notice that we can abstract this pattern to be more reusable
var ExternalImage = function(url, Point){ this.Point = Point; this.image = loadImage(url); this.draw = function(){ image(this.image, this.Point.x, this.Point.y); }; }; var Canvas = { JsLogo: new ExternalImage("https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Unofficial_JavaScript_logo_2.svg/200px-Unofficial_JavaScript_logo_2.svg.png", {x: 0, y: 0}) }; var draw = function(){ background(255); Canvas.JsLogo.draw(); };
Notice that the call to the loadImage() function is never directly called in the draw() function.