Skip to content

Commit ba3fb29

Browse files
author
Lauren McCarthy
committed
Merge pull request #363 from therewasaguy/io
saveCanvas() figures out file extension
2 parents 62e8e86 + 35db1dd commit ba3fb29

File tree

5 files changed

+31
-15
lines changed

5 files changed

+31
-15
lines changed

examples/p5.Image/saving-images.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function setup() {
88

99
setTimeout(function(){
1010
console.log("Save image")
11-
save(img, "unicorn.png");
11+
save(img, "unicorn.jpeg");
1212
}, 1000);
1313

1414
});

lib/p5.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! p5.js v0.3.5 September 09, 2014 */
1+
/*! p5.js v0.3.5 September 10, 2014 */
22
var shim = function (require) {
33
window.requestDraw = function () {
44
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback, element) {
@@ -3579,7 +3579,7 @@ var outputfiles = function (require, core) {
35793579
} else if (args[0] instanceof p5.Table) {
35803580
p5.prototype.saveTable(args[0], args[1], args[2], args[3]);
35813581
} else if (args[0] instanceof p5.Image) {
3582-
p5.prototype.saveCanvas(args[0].canvas, args[1], args[2]);
3582+
p5.prototype.saveCanvas(args[0].canvas, args[1]);
35833583
} else if (args[0] instanceof p5.SoundFile) {
35843584
p5.prototype.saveSound(args[0], args[1], args[2], args[3]);
35853585
} else if (args[0] instanceof Object) {
@@ -3718,11 +3718,12 @@ var outputfiles = function (require, core) {
37183718
if (!extension) {
37193719
extension = '';
37203720
}
3721+
if (!filename) {
3722+
filename = 'untitled';
3723+
}
37213724
var ext = '';
3722-
if (filename) {
3725+
if (filename && filename.indexOf('.') > -1) {
37233726
ext = filename.split('.').pop();
3724-
} else {
3725-
filename = 'untitled';
37263727
}
37273728
if (extension) {
37283729
if (ext !== extension) {
@@ -3735,6 +3736,7 @@ var outputfiles = function (require, core) {
37353736
ext
37363737
];
37373738
}
3739+
p5.prototype._checkFileExtension = _checkFileExtension;
37383740
p5.prototype._isSafari = function () {
37393741
var x = Object.prototype.toString.call(window.HTMLElement);
37403742
return x.indexOf('Constructor') > 0;
@@ -3749,6 +3751,12 @@ var outputimage = function (require, core) {
37493751
var p5 = core;
37503752
var frames = [];
37513753
p5.prototype.saveCanvas = function (_cnv, filename, extension) {
3754+
if (!extension) {
3755+
extension = p5.prototype._checkFileExtension(filename, extension)[1];
3756+
if (extension === '') {
3757+
extension = 'png';
3758+
}
3759+
}
37523760
var cnv;
37533761
if (_cnv) {
37543762
cnv = _cnv;
@@ -3764,7 +3772,7 @@ var outputimage = function (require, core) {
37643772
window.location.href = cnv.toDataURL();
37653773
} else {
37663774
var mimeType;
3767-
if (!extension) {
3775+
if (typeof extension === 'undefined') {
37683776
extension = 'png';
37693777
mimeType = 'image/png';
37703778
} else {

lib/p5.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/output/files.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ define(function (require) {
215215
p5.prototype.saveTable(args[0], args[1], args[2], args[3]);
216216
}
217217
else if (args[0] instanceof p5.Image) {
218-
p5.prototype.saveCanvas(args[0].canvas, args[1], args[2]);
218+
p5.prototype.saveCanvas(args[0].canvas, args[1]);
219219
}
220220
else if (args[0] instanceof p5.SoundFile) {
221221
p5.prototype.saveSound(args[0], args[1], args[2], args[3]);
@@ -520,12 +520,13 @@ define(function (require) {
520520
if (!extension) {
521521
extension = '';
522522
}
523+
if (!filename) {
524+
filename = 'untitled';
525+
}
523526
var ext = '';
524527
// make sure the file will have a name, see if filename needs extension
525-
if (filename) {
528+
if (filename && filename.indexOf('.') > -1) {
526529
ext = filename.split('.').pop();
527-
} else {
528-
filename = 'untitled';
529530
}
530531
// append extension if it doesn't exist
531532
if (extension) {
@@ -536,6 +537,7 @@ define(function (require) {
536537
}
537538
return [filename, ext];
538539
}
540+
p5.prototype._checkFileExtension = _checkFileExtension;
539541

540542
/**
541543
* Returns true if the browser is Safari, false if not.

src/output/image.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ define(function (require) {
1717
* specific html5 canvas (optional).
1818
*/
1919
p5.prototype.saveCanvas = function(_cnv, filename, extension) {
20+
if (!extension) {
21+
extension = p5.prototype._checkFileExtension(filename, extension)[1];
22+
if (extension === '') {
23+
extension = 'png';
24+
}
25+
}
2026
var cnv;
2127
if (_cnv) {
2228
cnv = _cnv;
@@ -33,7 +39,7 @@ define(function (require) {
3339
window.location.href = cnv.toDataURL();
3440
} else {
3541
var mimeType;
36-
if (!extension) {
42+
if (typeof(extension) === 'undefined') {
3743
extension = 'png';
3844
mimeType = 'image/png';
3945
}

0 commit comments

Comments
 (0)