@@ -99,15 +99,17 @@ Canvas.prototype.createJPEGStream = function(options){
9999} ;
100100
101101/**
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 ).
103103 *
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).
107110 * @return {String } data URL if synchronous (callback omitted)
108111 * @api public
109112 */
110-
111113Canvas . prototype . toDataURL = function ( a1 , a2 , a3 ) {
112114 // valid arg patterns (args -> [type, opts, fn]):
113115 // [] -> ['image/png', null, null]
@@ -123,6 +125,9 @@ Canvas.prototype.toDataURL = function(a1, a2, a3){
123125 // ['image/jpeg', opts, fn] -> ['image/jpeg', opts, fn]
124126 // ['image/jpeg', qual, fn] -> ['image/jpeg', {quality: qual}, fn]
125127 // ['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]
126131
127132 var type = 'image/png' ;
128133 var opts = { } ;
@@ -131,7 +136,7 @@ Canvas.prototype.toDataURL = function(a1, a2, a3){
131136 if ( 'function' === typeof a1 ) {
132137 fn = a1 ;
133138 } else {
134- if ( 'string' === typeof a1 && FORMATS . indexOf ( a1 . toLowerCase ( ) ) !== - 1 ) {
139+ if ( 'string' === typeof a1 && FORMATS . includes ( a1 . toLowerCase ( ) ) ) {
135140 type = a1 . toLowerCase ( ) ;
136141 }
137142
@@ -141,7 +146,7 @@ Canvas.prototype.toDataURL = function(a1, a2, a3){
141146 if ( 'object' === typeof a2 ) {
142147 opts = a2 ;
143148 } 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 ) ) } ;
145150 }
146151
147152 if ( 'function' === typeof a3 ) {
@@ -156,40 +161,19 @@ Canvas.prototype.toDataURL = function(a1, a2, a3){
156161 // Per spec, if the bitmap has no pixels, return this string:
157162 var str = "data:," ;
158163 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 ;
172166 } 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 ;
179168 }
169+ }
180170
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' ) } `
194178 }
195179} ;
0 commit comments