Exemplo n.º 1
0
Arquivo: image.js Projeto: Abioy/p5.js
  p5.prototype.saveFrames = function(fName, ext, _duration, _fps, callback) {
    var duration = _duration || 3;
    duration = p5.prototype.constrain(duration, 0, 15);
    duration = duration * 1000;
    var fps = _fps || 15;
    fps = p5.prototype.constrain(fps, 0, 22);
    var count = 0;

    var makeFrame = p5.prototype._makeFrame;
    var cnv = this._curElement.elt;
    var frameFactory = setInterval(function(){
      makeFrame(fName + count, ext, cnv);
      count++;
    },1000/fps);

    setTimeout(function(){
      clearInterval(frameFactory);
      if (callback) {
        callback(frames);
      }
      else {
        for (var i = 0; i < frames.length; i++) {
          var f = frames[i];
          p5.prototype.downloadFile(f.imageData, f.filename, f.ext);
        }
      }
      frames = []; // clear frames
    }, duration + 0.01);
  };
Exemplo n.º 2
0
 p5.prototype.writeFile = function(dataToDownload, filename, extension) {
   var type = 'application\/octet-stream';
   if (p5.prototype._isSafari() ) {
     type = 'text\/plain';
   }
   var blob = new Blob(dataToDownload, {'type': type});
   var href = window.URL.createObjectURL(blob);
   p5.prototype.downloadFile(href, filename, extension);
 };
Exemplo n.º 3
0
 p5.prototype.lerpColor = function(c1, c2, amt) {
   if (typeof c1 === 'object') {
     var c = [];
     for (var i=0; i<c1.length; i++) {
       c.push(p5.prototype.lerp(c1[i], c2[i], amt));
     }
     return c;
   } else {
     return p5.prototype.lerp(c1, c2, amt);
   }
 };
Exemplo n.º 4
0
 p5.prototype.bezier = function(x1, y1, x2, y2, x3, y3, x4, y4) {
   this.curElement.context.beginPath();
   this.curElement.context.moveTo(x1, y1);
   for (var i = 0; i <= this._bezierDetail; i++) { //for each point as considered by detail, iterate
     var t = i / parseFloat(this._bezierDetail);
     var x = p5.prototype.bezierPoint(x1, x2, x3, x4, t);
     var y = p5.prototype.bezierPoint(y1, y2, y3, y4, t);
     this.curElement.context.lineTo(x, y);
   }
   this.curElement.context.stroke();
   return this;
 };
Exemplo n.º 5
0
 p5.prototype.curve = function(x1, y1, x2, y2, x3, y3, x4, y4) {
   this.curElement.context.moveTo(x1,y1);
   this.curElement.context.beginPath();
   for (var i = 0; i <= this._curveDetail; i++) {
     var t = parseFloat(i/this._curveDetail);
     var x = p5.prototype.curvePoint(x1,x2,x3,x4,t);
     var y = p5.prototype.curvePoint(y1,y2,y3,y4,t);
     this.curElement.context.lineTo(x,y);
   }
   this.curElement.context.stroke();
   this.curElement.context.closePath();
   return this;
 };
Exemplo n.º 6
0
  p5.prototype.save = function(object, _filename, _options) {
    // parse the arguments and figure out which things we are saving
    var args = arguments;
    // =================================================
    // OPTION 1: saveCanvas...

    // if no arguments are provided, save canvas
    var cnv = this._curElement.elt;
    if (args.length === 0) {
      p5.prototype.saveCanvas(cnv);
      return;
    }
    // otherwise, parse the arguments

    // if first param is a p5Graphics, then saveCanvas
    else if (args[0] instanceof p5.Graphics) {
      p5.prototype.saveCanvas(args[0].elt, args[1], args[2]);
      return;
    }

    // if 1st param is String and only one arg, assume it is canvas filename
    else if (args.length === 1 && typeof(args[0]) === 'string') {
      p5.prototype.saveCanvas(cnv, args[0]);
    }

    // =================================================
    // OPTION 2: extension clarifies saveStrings vs. saveJSON

    else {
      var extension = _checkFileExtension(args[1], args[2])[1];
      switch(extension){
      case 'json':
        p5.prototype.saveJSON(args[0], args[1], args[2]);
        return;
      case 'txt':
        p5.prototype.saveStrings(args[0], args[1], args[2]);
        return;
      // =================================================
      // OPTION 3: decide based on object...
      default:
        if (args[0] instanceof Array) {
          p5.prototype.saveStrings(args[0], args[1], args[2]);
        }
        else if (args[0] instanceof p5.Table) {
          p5.prototype.saveTable(args[0], args[1], args[2], args[3]);
        }
        else if (args[0] instanceof p5.Image) {
          p5.prototype.saveCanvas(args[0].canvas, args[1]);
        }
        else if (args[0] instanceof p5.SoundFile) {
          p5.prototype.saveSound(args[0], args[1], args[2], args[3]);
        }
      }
    }
  };
Exemplo n.º 7
0
  p5.Image.prototype.save = function(filename, extension) {
    var mimeType;
    if (!extension) {
      extension = 'png';
      mimeType = 'image/png';
    }
    else {
      // en.wikipedia.org/wiki/Comparison_of_web_browsers#Image_format_support
      switch(extension.toLowerCase()){
      case 'png':
        mimeType = 'image/png';
        break;
      case 'jpeg':
        mimeType = 'image/jpeg';
        break;
      case 'jpg':
        mimeType = 'image/jpeg';
        break;
      default:
        mimeType = 'image/png';
        break;
      }
    }
    var downloadMime = 'image/octet-stream';
    var imageData = this.canvas.toDataURL(mimeType);
    imageData = imageData.replace(mimeType, downloadMime);

    //Make the browser download the file
    p5.prototype.downloadFile(imageData, filename, extension);
  };
Exemplo n.º 8
0
  p5.prototype.downloadFile = function(href, fName, extension) {
    var fx = _checkFileExtension(fName, extension);
    var filename = fx[0];
    var ext = fx[1];

    var a = document.createElement('a');
    a.href = href;
    a.download = filename;

    // Firefox requires the link to be added to the DOM before click()
    a.onclick = destroyClickedElement;
    a.style.display = 'none';
    document.body.appendChild(a);

    // Safari will open this file in the same page as a confusing Blob.
    if (p5.prototype._isSafari() ) {
      var aText = 'Hello, Safari user! To download this file...\n';
      aText += '1. Go to File --> Save As.\n';
      aText += '2. Choose "Page Source" as the Format.\n';
      aText += '3. Name it with this extension: .\"' + ext+'\"';
      alert(aText);
    }
    a.click();
    href = null;
  };
Exemplo n.º 9
0
Arquivo: image.js Projeto: Abioy/p5.js
  p5.prototype.saveCanvas = function(filename, extension, _cnv) {
    var cnv;
    if (_cnv) {
      cnv = _cnv;
    } else if (this._curElement && this._curElement.elt) {
      cnv = this._curElement.elt;
    }

    if ( p5.prototype._isSafari() ) {
      var aText = 'Hello, Safari user!\n';
      aText += 'Now capturing a screenshot...\n';
      aText += 'To save this image,\n';
      aText += 'go to File --> Save As.\n';
      alert(aText);
      window.location.href = cnv.toDataURL();
    } else {
      var mimeType;
      if (!extension) {
        extension = 'png';
        mimeType = 'image/png';
      }
      else {
        switch(extension.toLowerCase()){
        case 'png':
          mimeType = 'image/png';
          break;
        case 'jpeg':
          mimeType = 'image/jpeg';
          break;
        case 'jpg':
          mimeType = 'image/jpeg';
          break;
        default:
          mimeType = 'image/png';
          break;
        }
      }
      var downloadMime = 'image/octet-stream';
      var imageData = cnv.toDataURL(mimeType);
      imageData = imageData.replace(mimeType, downloadMime);

      p5.prototype.downloadFile(imageData, filename, extension);
    }
  };
Exemplo n.º 10
0
 p5.prototype.lerpColor = function (c1, c2, amt) {
   amt = Math.max(Math.min(amt, 1), 0);
   if (c1 instanceof Array) {
     var c = [];
     for (var i = 0; i < c1.length; i++) {
       c.push(Math.sqrt(p5.prototype.lerp(c1[i]*c1[i], c2[i]*c2[i], amt)));
     }
     return c;
   } else if (c1 instanceof p5.Color) {
     var pc = [];
     for (var j = 0; j < 4; j++) {
       pc.push(Math.sqrt(p5.prototype.lerp(
         c1.rgba[j]*c1.rgba[j],
         c2.rgba[j]*c2.rgba[j],
         amt)));
     }
     return new p5.Color(this, pc);
   } else {
     return Math.sqrt(p5.prototype.lerp(c1*c1, c2*c2, amt));
   }
 };
Exemplo n.º 11
0
Arquivo: image.js Projeto: Abioy/p5.js
 setTimeout(function(){
   clearInterval(frameFactory);
   if (callback) {
     callback(frames);
   }
   else {
     for (var i = 0; i < frames.length; i++) {
       var f = frames[i];
       p5.prototype.downloadFile(f.imageData, f.filename, f.ext);
     }
   }
   frames = []; // clear frames
 }, duration + 0.01);
Exemplo n.º 12
0
 this.close = function() {
   // convert String to Array for the writeFile Blob
   var arr = [];
   arr.push(this.content);
   p5.prototype.writeFile(arr, filename, extension);
   // remove from _pWriters array and delete self
   for (var i in p5.prototype._pWriters) {
     if (p5.prototype._pWriters[i].name === this.name) {
       // remove from _pWriters array
       p5.prototype._pWriters.splice(i, 1);
     }
   }
   self.flush();
   self = {};
 };