Skip to content

Commit 4d16f6d

Browse files
committed
Added image type and quality options to all "toDataURL" APIs (thanks @dinopetrone)
1 parent dbcf2e7 commit 4d16f6d

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

src/easeljs/display/DisplayObject.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,8 @@ this.createjs = this.createjs||{};
900900
* @method getCacheDataURL
901901
* @return {String} The image data url for the cache.
902902
**/
903-
p.getCacheDataURL = function() {
904-
return this.bitmapCache?this.bitmapCache.getCacheDataURL():null;
903+
p.getCacheDataURL = function(type, encoderOptions) {
904+
return this.bitmapCache?this.bitmapCache.getCacheDataURL(type, encoderOptions):null;
905905
};
906906

907907
/**

src/easeljs/display/Stage.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,11 @@ this.createjs = this.createjs||{};
471471
* value is allowed. The default value is a transparent background.
472472
* @param {String} [mimeType="image/png"] The MIME type of the image format to be create. The default is "image/png". If an unknown MIME type
473473
* is passed in, or if the browser does not support the specified MIME type, the default value will be used.
474+
* @param {Number} [encoderOptions=0.92] A Number between 0 and 1 indicating the image quality to use for image
475+
* formats that use lossy compression such as image/jpeg and image/webp.
474476
* @return {String} a Base64 encoded image.
475477
**/
476-
p.toDataURL = function(backgroundColor, mimeType) {
478+
p.toDataURL = function(backgroundColor, mimeType, encoderOptions) {
477479
var data, ctx = this.canvas.getContext('2d'), w = this.canvas.width, h = this.canvas.height;
478480

479481
if (backgroundColor) {
@@ -485,7 +487,7 @@ this.createjs = this.createjs||{};
485487
ctx.fillRect(0, 0, w, h);
486488
}
487489

488-
var dataURL = this.canvas.toDataURL(mimeType||"image/png");
490+
var dataURL = this.canvas.toDataURL(mimeType||"image/png", encoderOptions);
489491

490492
if(backgroundColor) {
491493
ctx.putImageData(data, 0, 0);

src/easeljs/display/StageGL.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,16 +1791,18 @@ this.createjs = this.createjs||{};
17911791
* for valid values. A value of undefined will make no adjustments to the existing background which may be significantly faster.
17921792
* @param {String} [mimeType="image/png"] The MIME type of the image format to be create. The default is "image/png". If an unknown MIME type
17931793
* is passed in, or if the browser does not support the specified MIME type, the default value will be used.
1794+
* @param {Number} [encoderOptions=0.92] A Number between 0 and 1 indicating the image quality to use for image
1795+
* formats that use lossy compression such as image/jpeg and image/webp.
17941796
* @return {String} a Base64 encoded image.
17951797
**/
1796-
p.toDataURL = function(backgroundColor, mimeType) {
1798+
p.toDataURL = function(backgroundColor, mimeType, encoderOptions) {
17971799
var dataURL, gl = this._webGLContext;
17981800
this.batchReason = "dataURL";
17991801
var clearBackup = this._clearColor;
18001802

18011803
if (!this.canvas) { return; }
18021804
if (!StageGL.isWebGLActive(gl)) {
1803-
return this.Stage_toDataURL(backgroundColor, mimeType);
1805+
return this.Stage_toDataURL(backgroundColor, mimeType, encoderOptions);
18041806
}
18051807

18061808
// if the buffer is preserved and we don't want a background we can just output what we have, otherwise we'll have to render it
@@ -1820,7 +1822,7 @@ this.createjs = this.createjs||{};
18201822
}
18211823

18221824
// create the dataurl
1823-
dataURL = this.canvas.toDataURL(mimeType||"image/png");
1825+
dataURL = this.canvas.toDataURL(mimeType||"image/png", encoderOptions);
18241826

18251827
// reset the picture in the canvas
18261828
if(!this._preserveBuffer || backgroundColor !== undefined) {

src/easeljs/filters/BitmapCache.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,12 +513,12 @@ this.createjs = this.createjs||{};
513513
* @method getCacheDataURL
514514
* @return {String} The image data url for the cache.
515515
**/
516-
p.getCacheDataURL = function() {
516+
p.getCacheDataURL = function(type, encoderOptions) {
517517
var cacheCanvas = this.target && this._cacheCanvas;
518518
if (!cacheCanvas) { return null; }
519519
if (this.cacheID !== this._cacheDataURLID) {
520520
this._cacheDataURLID = this.cacheID;
521-
this._cacheDataURL = cacheCanvas.toDataURL ? cacheCanvas.toDataURL() : null;
521+
this._cacheDataURL = cacheCanvas.toDataURL ? cacheCanvas.toDataURL(type, encoderOptions) : null;
522522
}
523523
return this._cacheDataURL;
524524
};

0 commit comments

Comments
 (0)