Hello there, I have a code that applies photo effects from a menu, and I'm looking for some guidance on how to optimize it.
Should I organize my code with the LOOP before CONDITIONAL, like shown below:
function applyFilter(filter){
var ctx = canvas.getContext('2d');
var imageData = ctx.getImageData(img.x,img.y,img.width,img.height);
var dataArr = imageData.data;
for(i = 0; i < dataArr.length; i+= 4){
let color = {
r : dataArr[i],
g : dataArr[i+1],
b : dataArr[i+2],
}
if(filter == "negative")
negateImage(dataArr, i, color)
else if(filter == "sephia")
addSephia(dataArr, i, color)
// else if some other effects
or should I place the CONDITIONAL before the LOOP as seen in the example below:
function applyFilter(filter){
if(filter == "negative")
negateImage()
else if(filter == "sephia")
addSephia()
// else if some other effects
}
function negateImage(){
var ctx = canvas.getContext('2d');
var imageData = ctx.getImageData(img.x,img.y,img.width,img.height);
var dataArr = imageData.data;
for(i = 0; i < dataArr.length; i+= 4){
// negate image algorithm
}
}
Thanks in advance for your help!