@@ -628,6 +628,46 @@ this.createjs = this.createjs||{};
628628 typeof WebGLRenderingContext !== 'undefined' ;
629629 } ;
630630
631+ /**
632+ * Utility used to convert the colour values the Context2D API accepts into WebGL color values.
633+ * @param {String | Number } color
634+ * @static
635+ * @return {Object } Object with r, g, b, a in 0-1 values of the color.
636+ */
637+ StageGL . colorToObj = function ( color ) {
638+ var r , g , b , a ;
639+
640+ if ( typeof color === "string" ) {
641+ if ( color . indexOf ( "#" ) === 0 ) {
642+ if ( color . length === 4 ) {
643+ color = "#" + color . charAt ( 1 ) + color . charAt ( 1 ) + color . charAt ( 2 ) + color . charAt ( 2 ) + color . charAt ( 3 ) + color . charAt ( 3 )
644+ }
645+ r = Number ( "0x" + color . slice ( 1 , 3 ) ) / 255 ;
646+ g = Number ( "0x" + color . slice ( 3 , 5 ) ) / 255 ;
647+ b = Number ( "0x" + color . slice ( 5 , 7 ) ) / 255 ;
648+ a = color . length > 7 ? Number ( "0x" + color . slice ( 7 , 9 ) ) / 255 : 1 ;
649+ } else if ( color . indexOf ( "rgba(" ) === 0 ) {
650+ var output = color . slice ( 5 , - 1 ) . split ( "," ) ;
651+ r = Number ( output [ 0 ] ) / 255 ;
652+ g = Number ( output [ 1 ] ) / 255 ;
653+ b = Number ( output [ 2 ] ) / 255 ;
654+ a = Number ( output [ 3 ] ) ;
655+ }
656+ } else { // >>> is an unsigned shift which is what we want as 0x80000000 and up are negative values
657+ r = ( ( color & 0xFF000000 ) >>> 24 ) / 255 ;
658+ g = ( ( color & 0x00FF0000 ) >>> 16 ) / 255 ;
659+ b = ( ( color & 0x0000FF00 ) >>> 8 ) / 255 ;
660+ a = ( color & 0x000000FF ) / 255 ;
661+ }
662+
663+ return {
664+ r : Math . min ( Math . max ( 0 , r ) , 1 ) ,
665+ g : Math . min ( Math . max ( 0 , g ) , 1 ) ,
666+ b : Math . min ( Math . max ( 0 , b ) , 1 ) ,
667+ a : Math . min ( Math . max ( 0 , a ) , 1 )
668+ }
669+ } ;
670+
631671// static properties:
632672 /**
633673 * The number of properties defined per vertex (x, y, textureU, textureV, textureIndex, alpha)
@@ -1764,35 +1804,64 @@ this.createjs = this.createjs||{};
17641804 * @param {String|int } [color=0x00000000] The new color to use as the background
17651805 */
17661806 p . setClearColor = function ( color ) {
1767- var r , g , b , a , output ;
1807+ this . _clearColor = StageGL . colorToObj ( color ) ;
1808+ } ;
17681809
1769- if ( typeof color === "string" ) {
1770- if ( color . indexOf ( "#" ) === 0 ) {
1771- if ( color . length === 4 ) {
1772- color = "#" + color . charAt ( 1 ) + color . charAt ( 1 ) + color . charAt ( 2 ) + color . charAt ( 2 ) + color . charAt ( 3 ) + color . charAt ( 3 )
1773- }
1774- r = Number ( "0x" + color . slice ( 1 , 3 ) ) / 255 ;
1775- g = Number ( "0x" + color . slice ( 3 , 5 ) ) / 255 ;
1776- b = Number ( "0x" + color . slice ( 5 , 7 ) ) / 255 ;
1777- a = Number ( "0x" + color . slice ( 7 , 9 ) ) / 255 ;
1778- } else if ( color . indexOf ( "rgba(" ) === 0 ) {
1779- output = color . slice ( 5 , - 1 ) . split ( "," ) ;
1780- r = Number ( output [ 0 ] ) / 255 ;
1781- g = Number ( output [ 1 ] ) / 255 ;
1782- b = Number ( output [ 2 ] ) / 255 ;
1783- a = Number ( output [ 3 ] ) ;
1810+ /**
1811+ * Returns a data url that contains a Base64-encoded image of the contents of the stage. The returned data url can
1812+ * be specified as the src value of an image element. StageGL renders differently than Context2D and the information
1813+ * of the last render is harder to get. For best results turn directDraw to false, or preserveBuffer to true and no
1814+ * backgorund color.
1815+ * @method toDataURL
1816+ * @param {String } [backgroundColor=undefined] The background color to be used for the generated image. See setClearColor
1817+ * for valid values. A value of undefined will make no adjustments to the existing background which may be significantly faster.
1818+ * @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
1819+ * is passed in, or if the browser does not support the specified MIME type, the default value will be used.
1820+ * @return {String } a Base64 encoded image.
1821+ **/
1822+ p . toDataURL = function ( backgroundColor , mimeType ) {
1823+ var dataURL , gl = this . _webGLContext ;
1824+ this . batchReason = "dataURL" ;
1825+ var clearBackup = this . _clearColor ;
1826+
1827+ if ( ! this . canvas ) { return ; }
1828+ if ( ! StageGL . isWebGLActive ( gl ) ) {
1829+ return this . Stage_toDataURL ( backgroundColor , mimeType ) ;
1830+ }
1831+
1832+ // 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
1833+ if ( ! this . _preserveBuffer || backgroundColor !== undefined ) {
1834+ // render it onto the right background
1835+ if ( backgroundColor !== undefined ) {
1836+ this . _clearColor = StageGL . colorToObj ( backgroundColor ) ;
1837+ }
1838+ this . clear ( ) ;
1839+ // if we're not using directDraw then we can just trust the last buffer content
1840+ if ( ! this . _directDraw ) {
1841+ this . _drawCover ( null , this . _bufferTextureOutput ) ;
1842+ } else {
1843+ console . log ( "No stored/useable gl render info, result may be incorrect if content was changed since render" ) ;
1844+ this . draw ( gl ) ;
1845+ }
1846+ }
1847+
1848+ // create the dataurl
1849+ dataURL = this . canvas . toDataURL ( mimeType || "image/png" ) ;
1850+
1851+ // reset the picture in the canvas
1852+ if ( ! this . _preserveBuffer || backgroundColor !== undefined ) {
1853+ if ( backgroundColor !== undefined ) {
1854+ this . _clearColor = clearBackup ;
1855+ }
1856+ this . clear ( ) ;
1857+ if ( ! this . _directDraw ) {
1858+ this . _drawCover ( null , this . _bufferTextureOutput ) ;
1859+ } else {
1860+ this . draw ( gl ) ;
17841861 }
1785- } else { // >>> is an unsigned shift which is what we want as 0x80000000 and up are negative values
1786- r = ( ( color & 0xFF000000 ) >>> 24 ) / 255 ;
1787- g = ( ( color & 0x00FF0000 ) >>> 16 ) / 255 ;
1788- b = ( ( color & 0x0000FF00 ) >>> 8 ) / 255 ;
1789- a = ( color & 0x000000FF ) / 255 ;
17901862 }
17911863
1792- this . _clearColor . r = r || 0 ;
1793- this . _clearColor . g = g || 0 ;
1794- this . _clearColor . b = b || 0 ;
1795- this . _clearColor . a = a || 0 ;
1864+ return dataURL ;
17961865 } ;
17971866
17981867 /**
0 commit comments