1- /*! p5.js v0.3.3 August 20 , 2014 */
1+ /*! p5.js v0.3.3 August 22 , 2014 */
22var shim = function ( require ) {
33 window . requestDraw = function ( ) {
44 return window . requestAnimationFrame || window . webkitRequestAnimationFrame || window . mozRequestAnimationFrame || window . oRequestAnimationFrame || window . msRequestAnimationFrame || function ( callback , element ) {
@@ -998,28 +998,31 @@ var p5Image = function (require, core, filters) {
998998 p5 . Image . prototype . blend = function ( ) {
999999 p5 . prototype . blend . apply ( this , arguments ) ;
10001000 } ;
1001- p5 . Image . prototype . save = function ( extension ) {
1001+ p5 . Image . prototype . save = function ( filename , extension ) {
10021002 var mimeType ;
1003- switch ( extension . toLowerCase ( ) ) {
1004- case 'png' :
1003+ if ( ! extension ) {
1004+ extension = 'png' ;
10051005 mimeType = 'image/png' ;
1006- break ;
1007- case 'jpeg' :
1008- mimeType = 'image/jpeg' ;
1009- break ;
1010- case 'jpg' :
1011- mimeType = 'image/jpeg' ;
1012- break ;
1013- default :
1014- mimeType = 'image/png' ;
1015- break ;
1016- }
1017- if ( mimeType !== undefined ) {
1018- var downloadMime = 'image/octet-stream' ;
1019- var imageData = this . canvas . toDataURL ( mimeType ) ;
1020- imageData = imageData . replace ( mimeType , downloadMime ) ;
1021- window . location . href = imageData ;
1006+ } else {
1007+ switch ( extension . toLowerCase ( ) ) {
1008+ case 'png' :
1009+ mimeType = 'image/png' ;
1010+ break ;
1011+ case 'jpeg' :
1012+ mimeType = 'image/jpeg' ;
1013+ break ;
1014+ case 'jpg' :
1015+ mimeType = 'image/jpeg' ;
1016+ break ;
1017+ default :
1018+ mimeType = 'image/png' ;
1019+ break ;
1020+ }
10221021 }
1022+ var downloadMime = 'image/octet-stream' ;
1023+ var imageData = this . canvas . toDataURL ( mimeType ) ;
1024+ imageData = imageData . replace ( mimeType , downloadMime ) ;
1025+ p5 . prototype . downloadFile ( imageData , filename , extension ) ;
10231026 } ;
10241027 return p5 . Image ;
10251028 } ( { } , core , filters ) ;
@@ -3510,6 +3513,46 @@ var outputfiles = function (require, core) {
35103513 p5 . prototype . saveBytes = function ( ) {
35113514 throw 'not yet implemented' ;
35123515 } ;
3516+ p5 . prototype . save = function ( object , _filename , _options ) {
3517+ var args = arguments ;
3518+ var cnv = this . _curElement . elt ;
3519+ if ( args . length === 0 ) {
3520+ p5 . prototype . saveCanvas ( null , null , cnv ) ;
3521+ return ;
3522+ }
3523+ if ( typeof args [ 0 ] === 'string' ) {
3524+ if ( typeof args [ 2 ] === 'object' ) {
3525+ p5 . prototype . saveCanvas ( args [ 0 ] , args [ 1 ] , args [ 2 ] ) ;
3526+ } else if ( typeof args [ 1 ] === 'string' ) {
3527+ p5 . prototype . saveCanvas ( args [ 0 ] , args [ 1 ] , cnv ) ;
3528+ } else {
3529+ p5 . prototype . saveCanvas ( args [ 0 ] , null , cnv ) ;
3530+ }
3531+ return ;
3532+ } else {
3533+ var extension = _checkFileExtension ( args [ 1 ] , args [ 2 ] ) [ 1 ] ;
3534+ switch ( extension ) {
3535+ case 'json' :
3536+ p5 . prototype . saveJSON ( args [ 0 ] , args [ 1 ] , args [ 2 ] ) ;
3537+ break ;
3538+ case 'txt' :
3539+ p5 . prototype . saveStrings ( args [ 0 ] , args [ 1 ] , args [ 2 ] ) ;
3540+ break ;
3541+ default :
3542+ if ( args [ 0 ] instanceof Array ) {
3543+ p5 . prototype . saveStrings ( args [ 0 ] , args [ 1 ] , args [ 2 ] ) ;
3544+ } else if ( args [ 0 ] instanceof p5 . Table ) {
3545+ p5 . prototype . saveTable ( args [ 0 ] , args [ 1 ] , args [ 2 ] , args [ 3 ] ) ;
3546+ } else if ( args [ 0 ] instanceof p5 . Image ) {
3547+ p5 . prototype . saveCanvas ( args [ 1 ] , args [ 2 ] , args [ 0 ] . canvas ) ;
3548+ } else if ( args [ 0 ] instanceof p5 . SoundFile ) {
3549+ p5 . prototype . saveSound ( args [ 0 ] , args [ 1 ] , args [ 2 ] , args [ 3 ] ) ;
3550+ } else if ( args [ 0 ] instanceof Object ) {
3551+ p5 . prototype . saveJSON ( args [ 0 ] , args [ 1 ] , args [ 2 ] ) ;
3552+ }
3553+ }
3554+ }
3555+ } ;
35133556 p5 . prototype . saveJSON = function ( json , filename , opt ) {
35143557 var stringify ;
35153558 if ( opt ) {
@@ -3608,46 +3651,59 @@ var outputfiles = function (require, core) {
36083651 pWriter . flush ( ) ;
36093652 } ;
36103653 p5 . prototype . writeFile = function ( dataToDownload , filename , extension ) {
3611- var ext = '' ;
3612- var a = document . createElement ( 'a' ) ;
36133654 var type = 'application/octet-stream' ;
3614- if ( _isSafari ( ) ) {
3655+ if ( p5 . prototype . _isSafari ( ) ) {
36153656 type = 'text/plain' ;
36163657 }
3617- if ( filename ) {
3618- ext = _checkFileExtension ( filename ) ;
3619- } else {
3620- filename = 'untitled' ;
3621- }
3622- if ( extension ) {
3623- if ( ext !== extension ) {
3624- ext = extension ;
3625- filename = filename + '.' + ext ;
3626- }
3627- }
36283658 var blob = new Blob ( dataToDownload , { 'type' : type } ) ;
3629- a . href = window . URL . createObjectURL ( blob ) ;
3659+ var href = window . URL . createObjectURL ( blob ) ;
3660+ p5 . prototype . downloadFile ( href , filename , extension ) ;
3661+ } ;
3662+ p5 . prototype . downloadFile = function ( href , fName , extension ) {
3663+ var fx = _checkFileExtension ( fName , extension ) ;
3664+ var filename = fx [ 0 ] ;
3665+ var ext = fx [ 1 ] ;
3666+ var a = document . createElement ( 'a' ) ;
3667+ a . href = href ;
36303668 a . download = filename ;
36313669 a . onclick = destroyClickedElement ;
36323670 a . style . display = 'none' ;
36333671 document . body . appendChild ( a ) ;
3634- if ( _isSafari ( ) ) {
3672+ if ( p5 . prototype . _isSafari ( ) ) {
36353673 var aText = 'Hello, Safari user! To download this file...\n' ;
36363674 aText += '1. Go to File --> Save As.\n' ;
36373675 aText += '2. Choose "Page Source" as the Format.\n' ;
36383676 aText += '3. Name it with this extension: ."' + ext + '"' ;
36393677 alert ( aText ) ;
36403678 }
36413679 a . click ( ) ;
3642- blob = null ;
3680+ href = null ;
36433681 } ;
3644- function _checkFileExtension ( filename ) {
3645- return filename . split ( '.' ) . pop ( ) ;
3682+ function _checkFileExtension ( filename , extension ) {
3683+ if ( ! extension ) {
3684+ extension = '' ;
3685+ }
3686+ var ext = '' ;
3687+ if ( filename ) {
3688+ ext = filename . split ( '.' ) . pop ( ) ;
3689+ } else {
3690+ filename = 'untitled' ;
3691+ }
3692+ if ( extension ) {
3693+ if ( ext !== extension ) {
3694+ ext = extension ;
3695+ filename = filename + '.' + ext ;
3696+ }
3697+ }
3698+ return [
3699+ filename ,
3700+ ext
3701+ ] ;
36463702 }
3647- function _isSafari ( ) {
3703+ p5 . prototype . _isSafari = function ( ) {
36483704 var x = Object . prototype . toString . call ( window . HTMLElement ) ;
36493705 return x . indexOf ( 'Constructor' ) > 0 ;
3650- }
3706+ } ;
36513707 function destroyClickedElement ( event ) {
36523708 document . body . removeChild ( event . target ) ;
36533709 }
@@ -3656,8 +3712,109 @@ var outputfiles = function (require, core) {
36563712var outputimage = function ( require , core ) {
36573713 'use strict' ;
36583714 var p5 = core ;
3659- p5 . prototype . save = function ( ) {
3660- window . open ( this . _curElement . elt . toDataURL ( 'image/png' ) ) ;
3715+ var frames = [ ] ;
3716+ p5 . prototype . saveCanvas = function ( filename , extension , _cnv ) {
3717+ var cnv ;
3718+ if ( _cnv ) {
3719+ cnv = _cnv ;
3720+ } else if ( this . _curElement && this . _curElement . elt ) {
3721+ cnv = this . _curElement . elt ;
3722+ }
3723+ if ( p5 . prototype . _isSafari ( ) ) {
3724+ var aText = 'Hello, Safari user!\n' ;
3725+ aText += 'Now capturing a screenshot...\n' ;
3726+ aText += 'To save this image,\n' ;
3727+ aText += 'go to File --> Save As.\n' ;
3728+ alert ( aText ) ;
3729+ window . location . href = cnv . toDataURL ( ) ;
3730+ } else {
3731+ var mimeType ;
3732+ if ( ! extension ) {
3733+ extension = 'png' ;
3734+ mimeType = 'image/png' ;
3735+ } else {
3736+ switch ( extension . toLowerCase ( ) ) {
3737+ case 'png' :
3738+ mimeType = 'image/png' ;
3739+ break ;
3740+ case 'jpeg' :
3741+ mimeType = 'image/jpeg' ;
3742+ break ;
3743+ case 'jpg' :
3744+ mimeType = 'image/jpeg' ;
3745+ break ;
3746+ default :
3747+ mimeType = 'image/png' ;
3748+ break ;
3749+ }
3750+ }
3751+ var downloadMime = 'image/octet-stream' ;
3752+ var imageData = cnv . toDataURL ( mimeType ) ;
3753+ imageData = imageData . replace ( mimeType , downloadMime ) ;
3754+ p5 . prototype . downloadFile ( imageData , filename , extension ) ;
3755+ }
3756+ } ;
3757+ p5 . prototype . saveFrames = function ( fName , ext , _duration , _fps , callback ) {
3758+ var duration = _duration || 3 ;
3759+ duration = p5 . prototype . constrain ( duration , 0 , 15 ) ;
3760+ duration = duration * 1000 ;
3761+ var fps = _fps || 15 ;
3762+ fps = p5 . prototype . constrain ( fps , 0 , 22 ) ;
3763+ var count = 0 ;
3764+ var makeFrame = p5 . prototype . _makeFrame ;
3765+ var cnv = this . _curElement . elt ;
3766+ var frameFactory = setInterval ( function ( ) {
3767+ makeFrame ( fName + count , ext , cnv ) ;
3768+ count ++ ;
3769+ } , 1000 / fps ) ;
3770+ setTimeout ( function ( ) {
3771+ clearInterval ( frameFactory ) ;
3772+ if ( callback ) {
3773+ callback ( frames ) ;
3774+ } else {
3775+ for ( var i = 0 ; i < frames . length ; i ++ ) {
3776+ var f = frames [ i ] ;
3777+ p5 . prototype . downloadFile ( f . imageData , f . filename , f . ext ) ;
3778+ }
3779+ }
3780+ frames = [ ] ;
3781+ } , duration + 0.01 ) ;
3782+ } ;
3783+ p5 . prototype . _makeFrame = function ( filename , extension , _cnv ) {
3784+ var cnv ;
3785+ if ( this ) {
3786+ cnv = this . _curElement . elt ;
3787+ } else {
3788+ cnv = _cnv ;
3789+ }
3790+ var mimeType ;
3791+ if ( ! extension ) {
3792+ extension = 'png' ;
3793+ mimeType = 'image/png' ;
3794+ } else {
3795+ switch ( extension . toLowerCase ( ) ) {
3796+ case 'png' :
3797+ mimeType = 'image/png' ;
3798+ break ;
3799+ case 'jpeg' :
3800+ mimeType = 'image/jpeg' ;
3801+ break ;
3802+ case 'jpg' :
3803+ mimeType = 'image/jpeg' ;
3804+ break ;
3805+ default :
3806+ mimeType = 'image/png' ;
3807+ break ;
3808+ }
3809+ }
3810+ var downloadMime = 'image/octet-stream' ;
3811+ var imageData = cnv . toDataURL ( mimeType ) ;
3812+ imageData = imageData . replace ( mimeType , downloadMime ) ;
3813+ var thisFrame = { } ;
3814+ thisFrame . imageData = imageData ;
3815+ thisFrame . filename = filename ;
3816+ thisFrame . ext = extension ;
3817+ frames . push ( thisFrame ) ;
36613818 } ;
36623819 return p5 ;
36633820 } ( { } , core ) ;
0 commit comments