@@ -99,15 +99,17 @@ Canvas.prototype.createJPEGStream = function(options){
99
99
} ;
100
100
101
101
/**
102
- * Return a data url . Pass a function for async support (required for "image/jpeg" ).
102
+ * Returns a data URI . Pass a function for async operation (non-standard ).
103
103
*
104
- * @param {String } type, optional, one of "image/png" or "image/jpeg", defaults to "image/png"
105
- * @param {Object|Number } encoderOptions, optional, options for jpeg compression (see documentation for Canvas#jpegStream) or the JPEG encoding quality from 0 to 1.
106
- * @param {Function } fn, optional, callback for asynchronous operation. Required for type "image/jpeg".
104
+ * @param {"image/png"|"image/jpeg" } [type="image/png"] Type.
105
+ * @param {Object|Number } [encoderOptions] A number between 0 and 1 indicating
106
+ * image quality if the requested type is image/jpeg (standard), or an options
107
+ * object for image encoding (see documentation for Canvas#toBuffer)
108
+ * (non-standard).
109
+ * @param {Function } [fn] Callback for asynchronous operation (non-standard).
107
110
* @return {String } data URL if synchronous (callback omitted)
108
111
* @api public
109
112
*/
110
-
111
113
Canvas . prototype . toDataURL = function ( a1 , a2 , a3 ) {
112
114
// valid arg patterns (args -> [type, opts, fn]):
113
115
// [] -> ['image/png', null, null]
@@ -123,6 +125,9 @@ Canvas.prototype.toDataURL = function(a1, a2, a3){
123
125
// ['image/jpeg', opts, fn] -> ['image/jpeg', opts, fn]
124
126
// ['image/jpeg', qual, fn] -> ['image/jpeg', {quality: qual}, fn]
125
127
// ['image/jpeg', undefined, fn] -> ['image/jpeg', null, fn]
128
+ // ['image/jpeg'] -> ['image/jpeg', null, fn]
129
+ // ['image/jpeg', opts] -> ['image/jpeg', opts, fn]
130
+ // ['image/jpeg', qual] -> ['image/jpeg', {quality: qual}, fn]
126
131
127
132
var type = 'image/png' ;
128
133
var opts = { } ;
@@ -131,7 +136,7 @@ Canvas.prototype.toDataURL = function(a1, a2, a3){
131
136
if ( 'function' === typeof a1 ) {
132
137
fn = a1 ;
133
138
} else {
134
- if ( 'string' === typeof a1 && FORMATS . indexOf ( a1 . toLowerCase ( ) ) !== - 1 ) {
139
+ if ( 'string' === typeof a1 && FORMATS . includes ( a1 . toLowerCase ( ) ) ) {
135
140
type = a1 . toLowerCase ( ) ;
136
141
}
137
142
@@ -141,7 +146,7 @@ Canvas.prototype.toDataURL = function(a1, a2, a3){
141
146
if ( 'object' === typeof a2 ) {
142
147
opts = a2 ;
143
148
} else if ( 'number' === typeof a2 ) {
144
- opts = { quality : Math . max ( 0 , Math . min ( 1 , a2 ) ) * 100 } ;
149
+ opts = { quality : Math . max ( 0 , Math . min ( 1 , a2 ) ) } ;
145
150
}
146
151
147
152
if ( 'function' === typeof a3 ) {
@@ -156,40 +161,19 @@ Canvas.prototype.toDataURL = function(a1, a2, a3){
156
161
// Per spec, if the bitmap has no pixels, return this string:
157
162
var str = "data:," ;
158
163
if ( fn ) {
159
- setTimeout ( function ( ) {
160
- fn ( null , str ) ;
161
- } ) ;
162
- }
163
- return str ;
164
- }
165
-
166
- if ( 'image/png' === type ) {
167
- if ( fn ) {
168
- this . toBuffer ( function ( err , buf ) {
169
- if ( err ) return fn ( err ) ;
170
- fn ( null , 'data:image/png;base64,' + buf . toString ( 'base64' ) ) ;
171
- } ) ;
164
+ setTimeout ( ( ) => fn ( null , str ) ) ;
165
+ return ;
172
166
} else {
173
- return 'data:image/png;base64,' + this . toBuffer ( ) . toString ( 'base64' ) ;
174
- }
175
-
176
- } else if ( 'image/jpeg' === type ) {
177
- if ( undefined === fn ) {
178
- throw new Error ( 'Missing required callback function for format "image/jpeg"' ) ;
167
+ return str ;
179
168
}
169
+ }
180
170
181
- var stream = this . jpegStream ( opts ) ;
182
- // note that jpegStream is synchronous
183
- var buffers = [ ] ;
184
- stream . on ( 'data' , function ( chunk ) {
185
- buffers . push ( chunk ) ;
186
- } ) ;
187
- stream . on ( 'error' , function ( err ) {
188
- fn ( err ) ;
189
- } ) ;
190
- stream . on ( 'end' , function ( ) {
191
- var result = 'data:image/jpeg;base64,' + Buffer . concat ( buffers ) . toString ( 'base64' ) ;
192
- fn ( null , result ) ;
193
- } ) ;
171
+ if ( fn ) {
172
+ this . toBuffer ( ( err , buf ) => {
173
+ if ( err ) return fn ( err ) ;
174
+ fn ( null , `data:${ type } ;base64,${ buf . toString ( 'base64' ) } ` ) ;
175
+ } , type , opts )
176
+ } else {
177
+ return `data:${ type } ;base64,${ this . toBuffer ( type ) . toString ( 'base64' ) } `
194
178
}
195
179
} ;
0 commit comments