Time for action – the imageEffects object
We will now create a new object called imageEffects
to encapsulate all of the code for our image effects and put it in a new file, imageEffects.js
. The imageEffects
object will be a global static object defined using the revealing module pattern.
Note
With the revealing module pattern, you define a set of functions in a private scope and then return an anonymous object that reveals which of those methods you want to be public. This works well for defining static objects.
Let's start by defining the imageEffects
object and adding two helper functions which will remain private. They are used to get and set the image data for the entire canvas:
var imageEffects = function() { function getImageData(canvas) { return canvas.getContext("2d").getImageData(0, 0, canvas.width, canvas.height) } function putImageData(canvas, imageData) { canvas.getContext("2d").putImageData(imageData, 0, 0); }
The getImageData...