How would I return the object r as a promise in the following function? 

I know that I have to start with promisifying onload, onComplete, toDataUrl
 and c; then we see how get fr to return a promise for r but I am not sure 
how to go about it.

function fr(f, p) {
  var r = {};
  var c = function(base64Img) {
    var u = f.data;
    var d = base64Img;
    var diff = resemble(u).compareTo(d).onComplete(function(data){
      r.data = data;
      r.name = p[i].name;
      r.imageUrl = p[i].imageUrl;
      r.pageUrl = p[i].pageUrl;
    });
    diff.scaleToSameSize();
    diff.ignoreAntialiasing();
    diff.ignoreColors();
    $log.debug(r);
  };
  for(var i = 0; i < 3; i++) {
    toDataUrl(p[i], c);
  }
  // return r as an object once all elements have been collected
}

function toDataUrl(src, callback, outputFormat) {
  var img = new Image();
  img.crossOrigin = 'Anonymous';
  img.onload = function() {
    var canvas = document.createElement('CANVAS');
    var ctx = canvas.getContext('2d');
    var dataURL;
    canvas.height = this.height;
    canvas.width = this.width;
    ctx.drawImage(this, 0, 0);
    dataURL = canvas.toDataURL(outputFormat);
    callback(dataURL);
  };
  img.src = src.imageUrl;
  if (img.complete || img.complete === undefined) {
    img.src = 
'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';
    img.src = src.imageUrl;
  }

-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to