Texture2D Class
Extends RawAsset
This class allows to easily create OpenGL or Canvas 2D textures from images, text or raw data.                                    
The created cc.Texture2D object will always have power-of-two dimensions.                                                
Depending on how you create the cc.Texture2D object, the actual image area of the texture might be smaller than the texture dimensions 
 i.e. "contentSize" != (pixelsWide, pixelsHigh) and (maxS, maxT) != (1.0, 1.0).                                           
Be aware that the content of the generated textures will be upside-down! 
Index
Properties
- url- StringThe url of the texture, this coule be empty if the texture wasn't created via a file.
- loaded- BooleanWhether the texture is loaded or not
- width- NumberTexture width in pixel
- height- NumberTexture height in pixel
- pixelFormat- NumberPixel format of the texture.
- pixelWidth- NumberWidth in pixels.
- pixelHeight- NumberHeight in pixels.
- _uuid- String
- _name- String
- _objFlags- Number
- name- StringThe name of the object.
- isValid- BooleanIndicates whether the object is not yet destroyed.
Methods
- updateUpdate texture options, not available in Canvas render mode. image, format, premultiplyAlpha can not be updated in native.
- getPixelWidthGet width in pixels.
- getPixelHeightGet height of in pixels.
- getContentSizeGet content size.
- getContentSizeInPixelsGet content size in pixels.
- initWithElementInit with HTML element.
- initWithDataIntializes with a texture2d with data.
- initWithImageInitializes a texture from a UIImage object. Extensions to make it easy to create a CCTexture2D object from an image file. Note that RGBA type textures will have their alpha premultiplied - use the blending mode (gl.ONE, gl.ONE_MINUS_SRC_ALPHA).
- getHtmlElementObjHTMLElement Object getter, available only on web. In most case, it will return null, because we are recycling the dom image element for better loading performance and lower image cache memory usage.
- isLoadedCheck whether texture is loaded.
- handleLoadedTextureHandler of texture loaded event.
- descriptionDescription of cc.Texture2D.
- releaseTextureRelease texture.
- getPixelFormatPixel format of the texture.
- hasPremultipliedAlphaWhether or not the texture has their Alpha premultiplied, support only in WebGl rendering mode.
- hasMipmapsWhether or not use mipmap, support only in WebGl rendering mode.
- setTexParametersSets the min filter, mag filter, wrap s and wrap t texture parameters.
 If the texture size is NPOT (non power of 2), then in can only use gl.CLAMPTO_EDGE in gl.TEXTURE_WRAP{S,T}.
- setAntiAliasTexParameterssets antialias texture parameters:- GL_TEXTURE_MIN_FILTER = GL_NEAREST           
- GL_TEXTURE_MAG_FILTER = GL_NEAREST           
 supported only in native or WebGl rendering mode
 
- GL_TEXTURE_MIN_FILTER = GL_NEAREST           
- setAliasTexParametersSets alias texture parameters:
 GL_TEXTURE_MIN_FILTER = GL_NEAREST
 GL_TEXTURE_MAG_FILTER = GL_NEAREST
 supported only in native or WebGl rendering mode
- onRegister an callback of a specific event type on the EventTarget.
- offRemoves the listeners previously registered with the same type, callback, target and or useCapture, if only type is passed as parameter, all listeners registered with that type will be removed.
- targetOffRemoves all callbacks previously registered with the same target (passed as parameter). This is not for removing all listeners in the current event target, and this is not for removing all listeners the target parameter have registered. It's only for removing all listeners (callback and target couple) registered on the current event target by the target parameter.
- onceRegister an callback of a specific event type on the EventTarget, the callback will remove itself after the first time it is triggered.
- dispatchEventDispatches an event into the event flow. The event target is the EventTarget object upon which the dispatchEvent() method is called.
- emitSend an event to this object directly, this method will not propagate the event to any other objects. The event will be created from the supplied message, you can get the "detail" argument from event.detail.
- destroyDestroy this Object, and release all its own references to other objects.
 Actual object destruction will delayed until before rendering.
 After destroy, this CCObject is not usable any more. You can use cc.isValid(obj) to check whether the object is destroyed before accessing it.
- _destructClear all references in the instance.
NOTE: this method will not clear the getter or setter functions which defined in the instance of CCObject. You can override the _destruct method if you need, for example: _destruct: function () { for (var key in this) { if (this.hasOwnProperty(key)) { switch (typeof this[key]) { case 'string': this[key] = ''; break; case 'object': case 'function': this[key] = null; break; } } }
- _onPreDestroyCalled before the object being destroyed.
- _serializeThe customized serialization for this object. (Editor Only)
- _deserializeInit this object from the custom serialized data.
Details
Properties
url
The url of the texture, this coule be empty if the texture wasn't created via a file.
| meta | description | 
|---|---|
| Type | String | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:223 | 
loaded
Whether the texture is loaded or not
| meta | description | 
|---|---|
| Type | Boolean | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:232 | 
width
Texture width in pixel
| meta | description | 
|---|---|
| Type | Number | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:241 | 
height
Texture height in pixel
| meta | description | 
|---|---|
| Type | Number | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:250 | 
pixelFormat
Pixel format of the texture.
| meta | description | 
|---|---|
| Type | Number | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:927 | 
pixelWidth
Width in pixels.
| meta | description | 
|---|---|
| Type | Number | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:934 | 
| Deprecated | please use width instead | 
pixelHeight
Height in pixels.
| meta | description | 
|---|---|
| Type | Number | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:942 | 
| Deprecated | please use height instead | 
_uuid
| meta | description | 
|---|---|
| Type | String | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/assets/CCRawAsset.js:49 | 
_name
| meta | description | 
|---|---|
| Type | String | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:50 | 
_objFlags
| meta | description | 
|---|---|
| Type | Number | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:57 | 
name
The name of the object.
| meta | description | 
|---|---|
| Type | String | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:208 | 
Examples
obj.name = "New Obj";
isValid
Indicates whether the object is not yet destroyed.
| meta | description | 
|---|---|
| Type | Boolean | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:225 | 
Examples
cc.log(obj.isValid);
Methods
update
Update texture options, not available in Canvas render mode. image, format, premultiplyAlpha can not be updated in native.
| meta | description | 
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:275 | 
Parameters
- optionsObject
getPixelWidth
Get width in pixels.
| meta | description | 
|---|---|
| Returns | Number | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:292 | 
| Deprecated | use width or height property instead | 
getPixelHeight
Get height of in pixels.
| meta | description | 
|---|---|
| Returns | Number | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:302 | 
| Deprecated | use width or height property instead | 
getContentSize
Get content size.
| meta | description | 
|---|---|
| Returns | Size | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:312 | 
| Deprecated | use width or height property instead | 
getContentSizeInPixels
Get content size in pixels.
| meta | description | 
|---|---|
| Returns | Size | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:322 | 
| Deprecated | use width or height property instead | 
initWithElement
Init with HTML element.
| meta | description | 
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:332 | 
Parameters
- elementHTMLImageElement | HTMLCanvasElement
Examples
var img = new Image();
img.src = dataURL;
texture.initWithElement(img);
texture.handleLoadedTexture();
initWithData
Intializes with a texture2d with data.
| meta | description | 
|---|---|
| Returns | Boolean | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:351 | 
Parameters
- dataTypedArray
- pixelFormatNumber
- pixelsWidthNumber
- pixelsHeightNumber
- contentSizeSize contentSize is deprecated and ignored
initWithImage
Initializes a texture from a UIImage object. Extensions to make it easy to create a CCTexture2D object from an image file. Note that RGBA type textures will have their alpha premultiplied - use the blending mode (gl.ONE, gl.ONE_MINUS_SRC_ALPHA).
| meta | description | 
|---|---|
| Returns | Boolean | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:366 | 
Parameters
- uiImageHTMLImageElement
getHtmlElementObj
HTMLElement Object getter, available only on web. In most case, it will return null, because we are recycling the dom image element for better loading performance and lower image cache memory usage.
| meta | description | 
|---|---|
| Returns | HTMLImageElement | HTMLCanvasElement | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:379 | 
isLoaded
Check whether texture is loaded.
| meta | description | 
|---|---|
| Returns | Boolean | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:389 | 
| Deprecated | use loaded property instead | 
handleLoadedTexture
Handler of texture loaded event.
| meta | description | 
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:399 | 
Parameters
- premultipliedBoolean
description
Description of cc.Texture2D.
| meta | description | 
|---|---|
| Returns | String | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:417 | 
releaseTexture
Release texture.
| meta | description | 
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:426 | 
getPixelFormat
Pixel format of the texture.
| meta | description | 
|---|---|
| Returns | Number | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:436 | 
hasPremultipliedAlpha
Whether or not the texture has their Alpha premultiplied, support only in WebGl rendering mode.
| meta | description | 
|---|---|
| Returns | Boolean | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:446 | 
hasMipmaps
Whether or not use mipmap, support only in WebGl rendering mode.
| meta | description | 
|---|---|
| Returns | Boolean | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:456 | 
setTexParameters
Sets the min filter, mag filter, wrap s and wrap t texture parameters. 
If the texture size is NPOT (non power of 2), then in can only use gl.CLAMPTO_EDGE in gl.TEXTURE_WRAP{S,T}.
| meta | description | 
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:465 | 
| Deprecated | use update function with filter and wrap options instead | 
Parameters
- texParamsObject | Number texParams object or minFilter
- magFilterNumber
- wrapSTexture2D.WrapMode
- wrapTTexture2D.WrapMode
setAntiAliasTexParameters
sets antialias texture parameters:              
- GL_TEXTURE_MIN_FILTER = GL_NEAREST           
- GL_TEXTURE_MAG_FILTER = GL_NEAREST           
 supported only in native or WebGl rendering mode
| meta | description | 
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:494 | 
| Deprecated | use update function with filter options instead | 
setAliasTexParameters
Sets alias texture parameters:                 
  GL_TEXTURE_MIN_FILTER = GL_NEAREST           
  GL_TEXTURE_MAG_FILTER = GL_NEAREST           
supported only in native or WebGl rendering mode
| meta | description | 
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:506 | 
| Deprecated | use update function with filter options instead | 
on
Register an callback of a specific event type on the EventTarget.
| meta | description | 
|---|---|
| Returns | Function | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:217 | 
Parameters
- typeString A string representing the event type to listen for.
- callbackFunction The callback that will be invoked when the event is dispatched.- The callback is ignored if it is a duplicate (the callbacks are unique).- eventEvent event
 
- targetObject The target (this object) to invoke the callback, can be null
- useCaptureBoolean When set to true, the capture argument prevents callback- from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked when event's eventPhase attribute value is AT_TARGET.
Examples
node.on(cc.Node.EventType.TOUCH_END, function (event) {
    cc.log("this is callback");
}, node);
off
Removes the listeners previously registered with the same type, callback, target and or useCapture, if only type is passed as parameter, all listeners registered with that type will be removed.
| meta | description | 
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:274 | 
Parameters
- typeString A string representing the event type being removed.
- callbackFunction The callback to remove.
- targetObject The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
- useCaptureBoolean Specifies whether the callback being removed was registered as a capturing callback or not.- If not specified, useCapture defaults to false. If a callback was registered twice, one with capture and one without, each must be removed separately. Removal of a capturing callback does not affect a non-capturing version of the same listener, and vice versa.
Examples
// register touchEnd eventListener
var touchEnd = node.on(cc.Node.EventType.TOUCH_END, function (event) {
    cc.log("this is callback");
}, node);
// remove touch end event listener
node.off(cc.Node.EventType.TOUCH_END, touchEnd, node);
// remove all touch end event listeners
node.off(cc.Node.EventType.TOUCH_END);
targetOff
Removes all callbacks previously registered with the same target (passed as parameter). This is not for removing all listeners in the current event target, and this is not for removing all listeners the target parameter have registered. It's only for removing all listeners (callback and target couple) registered on the current event target by the target parameter.
| meta | description | 
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:329 | 
Parameters
- targetObject The target to be searched for all related listeners
once
Register an callback of a specific event type on the EventTarget, the callback will remove itself after the first time it is triggered.
| meta | description | 
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:351 | 
Parameters
- typeString A string representing the event type to listen for.
- callbackFunction The callback that will be invoked when the event is dispatched.- The callback is ignored if it is a duplicate (the callbacks are unique).- eventEvent event
 
- targetObject The target (this object) to invoke the callback, can be null
- useCaptureBoolean When set to true, the capture argument prevents callback- from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked when event's eventPhase attribute value is AT_TARGET.
Examples
node.once(cc.Node.EventType.TOUCH_END, function (event) {
    cc.log("this is callback");
}, node);
dispatchEvent
Dispatches an event into the event flow. The event target is the EventTarget object upon which the dispatchEvent() method is called.
| meta | description | 
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:396 | 
Parameters
- eventEvent The Event object that is dispatched into the event flow
emit
Send an event to this object directly, this method will not propagate the event to any other objects. The event will be created from the supplied message, you can get the "detail" argument from event.detail.
| meta | description | 
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:410 | 
Parameters
- messageString the message to send
- detailAny whatever argument the message needs
destroy
Destroy this Object, and release all its own references to other objects.
Actual object destruction will delayed until before rendering.
After destroy, this CCObject is not usable any more.
You can use cc.isValid(obj) to check whether the object is destroyed before accessing it.
| meta | description | 
|---|---|
| Returns | Boolean | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:246 | 
Examples
obj.destroy();
_destruct
Clear all references in the instance.
NOTE: this method will not clear the getter or setter functions which defined in the instance of CCObject. You can override the _destruct method if you need, for example: _destruct: function () { for (var key in this) { if (this.hasOwnProperty(key)) { switch (typeof this[key]) { case 'string': this[key] = ''; break; case 'object': case 'function': this[key] = null; break; } } }
| meta | description | 
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:379 | 
_onPreDestroy
Called before the object being destroyed.
| meta | description | 
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:412 | 
_serialize
The customized serialization for this object. (Editor Only)
| meta | description | 
|---|---|
| Returns | object | 
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:437 | 
Parameters
- exportingBoolean
_deserialize
Init this object from the custom serialized data.
| meta | description | 
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:447 | 
Parameters
- dataObject the serialized json data
- ctx_Deserializer
