Cocos Creator API

1.5.x

Cocos Creator is the game engine for the future.

Texture2D

Extends RawAsset
Module: cc
Parent Module: cc

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!

Properties

PIXEL_FORMAT_RGBA8888 Number static

32-bit texture: RGBA8888

PIXEL_FORMAT_RGB888 Number static

24-bit texture: RGB888, not supported yet

PIXEL_FORMAT_RGB565 Number static

16-bit texture without Alpha channel, not supported yet

PIXEL_FORMAT_A8 Number static

8-bit textures used as masks, not supported yet

PIXEL_FORMAT_I8 Number static

8-bit intensity texture, not supported yet

PIXEL_FORMAT_AI88 Number static

16-bit textures used as masks, not supported yet

PIXEL_FORMAT_RGBA4444 Number static

16-bit textures: RGBA4444, not supported yet

PIXEL_FORMAT_RGB5A1 Number static

16-bit textures: RGB5A1, not supported yet

PIXEL_FORMAT_PVRTC4 Number static

4-bit PVRTC-compressed texture: PVRTC4, not supported yet

PIXEL_FORMAT_PVRTC2 Number static

2-bit PVRTC-compressed texture: PVRTC2, not supported yet

PIXEL_FORMAT_DEFAULT Number static

Default texture format: RGBA8888

defaultPixelFormat Number static

The default pixel format

name WebGLTexture readOnly

WebGLTexture Object.

url String readOnly

The source file's url for the texture, it could be empty if the texture wasn't created via a file.

pixelFormat Number readOnly

Pixel format of the texture.

pixelWidth Number readOnly

Width in pixels.

pixelHeight Number readOnly

Height in pixels.

width Number

Content width in points.

height Number

Content height in points.

_uuid String private

Inherited from RawAsset:

_name String private

_objFlags Number private

isValid Boolean readOnly

Indicates whether the object is not yet destroyed.

examples:

cc.log(obj.isValid);

There are no properties that match your current filter settings. You can change your filter settings in the index section on this page. index

Methods

getPixelWidth ( ) Number

Get width in pixels.

returns:

type: Number

getPixelHeight ( ) Number

Get height of in pixels.

returns:

type: Number

getContentSize ( ) Size

Get content size.

returns:

type: Size

getContentSizeInPixels ( ) Size

Get content size in pixels.

returns:

type: Size

initWithElement
(
  • element
)

Init with HTML element.

name type description
element HTMLImageElement | HTMLCanvasElement

examples:

var img = new Image();
img.src = dataURL;
texture.initWithElement(img);
texture.handleLoadedTexture();

initWithData
(
  • data
  • pixelFormat
  • pixelsWide
  • pixelsHigh
  • contentSize
)
Boolean

Intializes with a texture2d with data.

name type description
data Array
pixelFormat Number
pixelsWide Number
pixelsHigh Number
contentSize Size

returns:

type: Boolean

initWithImage
(
  • uiImage
)
Boolean

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).

name type description
uiImage HTMLImageElement

returns:

type: Boolean

getHtmlElementObj ( ) HTMLImageElement | HTMLCanvasElement

HTMLElement Object getter.

returns:

type: HTMLImageElement | HTMLCanvasElement

isLoaded ( ) Boolean

Check whether texture is loaded.

returns:

type: Boolean

handleLoadedTexture
(
  • [premultiplied ]
)

Handler of texture loaded event.

name type description
premultiplied optional Boolean

description ( ) String

Description of cc.Texture2D.

returns:

type: String

releaseTexture ( )

Release texture.

getPixelFormat ( ) Number

Pixel format of the texture.

returns:

type: Number

hasPremultipliedAlpha ( ) Boolean

Whether or not the texture has their Alpha premultiplied, support only in WebGl rendering mode.

returns:

type: Boolean

hasMipmaps ( ) Boolean

Whether or not use mipmap, support only in WebGl rendering mode.

returns:

type: Boolean

setTexParameters
(
  • texParams
  • [magFilter ]
  • [wrapS ]
  • [wrapT ]
)

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.CLAMP_TO_EDGE in gl.TEXTURE_WRAP_{S,T}.

name type description
texParams Object | Number

texParams object or minFilter

magFilter optional Number
wrapS optional Texture2D.WrapMode
wrapT optional Texture2D.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

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

on
(
  • type
  • callback
  • [target ]
  • [useCapture =false]
)
Function

Inherited from EventTarget:

Register an callback of a specific event type on the EventTarget.

name type description
type String

A string representing the event type to listen for.

callback Function

The callback that will be invoked when the event is dispatched. The callback is ignored if it is a duplicate (the callbacks are unique).

target optional Object

The target to invoke the callback, can be null

useCapture optional Boolean false

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.

returns:

type: Function

Just returns the incoming callback so you can save the anonymous function easier.

examples:

node.on(cc.Node.EventType.TOUCH_END, function (event) {
    cc.log("this is callback");
}, node);

off
(
  • type
  • callback
  • [target ]
  • [useCapture =false]
)

Inherited from EventTarget:

Removes the callback previously registered with the same type, callback, target and or useCapture.

name type description
type String

A string representing the event type being removed.

callback Function

The callback to remove.

target optional Object

The target to invoke the callback, if it's not given, only callback without target will be removed

useCapture optional Boolean false

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 touchEnd eventListener
node.off(cc.Node.EventType.TOUCH_END, touchEnd, node);

targetOff
(
  • target
)

Inherited from EventTarget:

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.

name type description
target Object

The target to be searched for all related listeners

once
(
  • type
  • callback
  • [target ]
  • [useCapture =false]
)

Inherited from EventTarget:

Register an callback of a specific event type on the EventTarget, the callback will remove itself after the first time it is triggered.

name type description
type String

A string representing the event type to listen for.

callback Function

The callback that will be invoked when the event is dispatched. The callback is ignored if it is a duplicate (the callbacks are unique).

target optional Object

The target to invoke the callback, can be null

useCapture optional Boolean false

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
(
  • event
)

Inherited from EventTarget:

Dispatches an event into the event flow. The event target is the EventTarget object upon which the dispatchEvent() method is called.

name type description
event Event

The Event object that is dispatched into the event flow

emit
(
  • message
  • [detail ]
)

Inherited from EventTarget:

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.

name type description
message String

the message to send

detail optional Any

whatever argument the message needs

destroy ( ) Boolean

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.

returns:

type: Boolean

whether it is the first time the destroy being called

examples:

obj.destroy();

_destruct ( ) private

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; } } }

_onPreDestroy ( ) private

Called before the object being destroyed.

_serialize
(
  • exporting
)
object private

The customized serialization for this object. (Editor Only)

name type description
exporting Boolean

returns:

type: object

the serialized json data object

_deserialize
(
  • data
  • ctx
)
private

Init this object from the custom serialized data.

name type description
data Object

the serialized json data

ctx _Deserializer

There are no methods that match your current filter settings. You can change your filter settings in the index section on this page. index