Namespace: Utils

Cocoon. Utils

This namespace holds different utilities.

Methods

<static> captureScreen(fileName, storageType, captureType, saveToGallery)

Captures a image of the screen synchronously and saves it to a file. Sync mode allows to capture the screen in the middle of a frame rendering.

Parameters:
Name Type Description
fileName string

Desired file name and format (png or jpg). If no value is passed, "capture.png" value is used by default

storageType Cocoon.App.StorageType

The developer can specify the storage where it is stored. If no value is passed, the Cocoon.Utils.StorageType.TMP_STORAGE value is used by default.

captureType Cocoon.Utils.CaptureType

Optional value to choose capture type. See Cocoon.Utils.CaptureType.

  • 0: Captures everything.
  • 1: Only captures cocoon surface.
  • 2: Only captures system views.
saveToGallery boolean

Optional value to specify if the capture image should be stored in the device image gallery or not.

Throws:
exception if the image fails to be stored or there is another error.
Returns:

The URL of the saved file.

Example
Cocoon.Utils.captureScreen("myScreenshot.png");

<static> captureScreenAsync(fileName, storageType, captureType, saveToGallery, callback)

Captures a image of the screen asynchronously and saves it to a file. Async mode captures a final frame as soon as possible.

Parameters:
Name Type Description
fileName string

Desired file name and format (png or jpg). If no value is passed, "capture.png" value is used by default

storageType Cocoon.App.StorageType

The developer can specify the storage where it is stored. If no value is passed, the {@see Cocoon.Utils.StorageType.TMP_STORAGE} value is used by default.

captureType Cocoon.Utils.CaptureType

Optional value to choose capture type. See Cocoon.Utils.CaptureType.

  • 0: Captures everything.
  • 1: Only captures cocoon surface.
  • 2: Only captures system views.
saveToGallery boolean

Optional value to specify if the capture image should be stored in the device image gallery or not.

callback function

Response callback, check the error property to monitor errors. Check the 'url' property to get the URL of the saved Image

Example
Cocoon.Utils.captureScreenAsync("myScreenshot.png", Cocoon.Utils.StorageType.TMP_STORAGE, false, Cocoon.Utils.CaptureType.EVERYTHING, function(url, error){
...
});

<static> existsPath(path, storageType)

Queries if a file exists in the specified path and storage type. If none or unknown storage type is specified, the TEMPORARY_STORAGE is used as default.

Parameters:
Name Type Description
path string

The relative path to look for inside the storage of the underlying system.

storageType Cocoon.App.StorageType

The storage type where to look for the specified path inside the system.

Example
console.log(Cocoon.Utils.existsPath("file.txt"));

<static> isWebGLEnabled()

Checks if WebGL is enabled in Canvas+.

Example
var enabled = Cocoon.Utils.isWebGLEnabled();

<static> logMemoryInfo()

Prints in the console the memory usage of the currently alive textures.

Example
Cocoon.Utils.logMemoryInfo();

<static> markAsMusic(filePath)

Marks a audio file to be used as music by the system. Cocoon, internally, differentiates among music files and sound files. Music files are usually bigger in size and longer in duration that sound files. There can only be just one music file playing at a specific given time. The developer can mark as many files as he/she wants to be treated as music. When the corresponding HTML5 audio object is used, the system will automatically know how to treat the audio resource as music or as sound. Note that it is not mandatory to use this function. The system automatically tries to identify if a file is suitable to be treated as music or as sound by checking file size and duration thresholds. It is recommended, though, that the developer specifies him/herself what he/she considers to be music.

Parameters:
Name Type Description
filePath string

File path to be marked as music

Example
Cocoon.Utils.markAsMusic("path/to/file.mp3");

<static> setAntialias(enable)

Activates or deactivates the antialas functionality from the Cocoon rendering.

Parameters:
Name Type Description
enable boolean

A boolean value to enable (true) or disable (false) antialias.

Example
Cocoon.Utils.setAntialias(true);

<static> setMaxMemory(memoryInMBs)

Sets a max memory threshold in Canvas+ for canvas2D contexts. If the maxMemory is enabled, Cocoon checks the total amount of texture sizes (images and canvases). When the memory size reaches the max memory threshold Cocoon disposes least recently used textures until the memory fits the threshold. It disposes textures used for JS Image objects (which can be reloaded later if needed). It doesn't dispose canvas objects because they cannot be reconstructed if they are used again in a render operation.

Parameters:
Name Type Description
memoryInMBs number

max memory in megabytes

Example
Cocoon.Utils.setMaxMemory(75);

<static> setNPOTEnabled(enabled)

Enables NPOT (not power of two) textures in Canvas+. Canvas+ uses POT (power of two) textures by default. Enabling NPOT improves memory usage but may affect performance on old GPUs.

Parameters:
Name Type Description
enabled boolean

true to enable NPOT Textures

Example
Cocoon.Utils.setNPOTEnabled(true);

<static> setTextCacheSize(size)

Setups the internal text texture cache size. In order to improve the performance of fill and stroke operations, a text texture cache is used internally. Once a text is drawn a texture is stored that matches that text and that text configuration. If the same text is called to be drawn, this cached texture would be used. This function allows to set the size of the cache. A value of 0 would mean that no cache will be used.

Parameters:
Name Type Description
size number

The size of the text cache.

Example
Cocoon.Utils.setTextCacheSize(32);

<static> setTextureReduction(sizeThreshold, applyTo, forbidFor)

Sets the texture reduction options. The texture reduction is a process that allows big images to be reduced/scaled down when they are loaded. Although the quality of the images may decrease, it can be very useful in low end devices or those with limited amount of memory. The function sets the threshold on image size (width or height) that will be used in order to know if an image should be reduced or not. It also allows to specify a list of strings to identify in which images file paths should be applied (when they meet the size threshold requirement) The developer will still think that the image is of the original size. Cocoon handles all of the internals to be able to show the image correctly. IMPORTANT NOTE: This function should be called when the application is initialized before any image is set to be loaded for obvious reasons ;). and in which sould be forbid (even if they meet the threshold requirement).

Parameters:
Name Type Description
sizeThreshold number

This parameter specifies the minimun size (either width or height) that an image should have in order to be reduced.

applyTo string | array

This parameter can either be a string or an array of strings. It's purpose is to specify one (the string) or more (the array) substring(s) that will be compared against the file path of an image to be loaded in order to know if the reduction should be applied or not. If the image meets the threshold size requirement and it's file path contains this string (or strings), it will be reduced. This parameter can also be null.

forbidFor string | array

This parameter can either be a string or an array of strings. It's purpose is to specify one (the string) or more (the array) substring(s) that will be compared against the file path of an image to be loaded in order to know if the reduction should be applied or not. If the image meets the threshold size requirement and it's file path contains this string (or strings), it won't be reduced. This parameter should be used in order to mantain the quality of some images even they meet the size threshold requirement.

Example
Cocoon.Utils.textureReduction(64);

<static> setWebGLEnabled(enabled)

Activates or deactivates the webgl functionality from the Cocoon Canvas+ rendering.

Parameters:
Name Type Description
enabled boolean

A boolean value to enable (true) or disable (false) webgl in Canvas+.

Example
Cocoon.Utils.setWebGLEnabled(true);

Members

<static> CaptureType

Properties:
Name Type Description
Cocoon.Utils.CaptureType string

The base object

Properties
Name Type Description
EVERYTHING string

Captures everything, both the Cocoon GL hardware accelerated surface and the system views (like the WebView).

COCOONJS_GL_SURFACE string

Captures just the Cocoon GL hardware accelerated surface.

JUST_SYSTEM_VIEWS string

Captures just the sustem views (like the webview)

Cocoon Canvas+ - JavaScript Documentation by Ludei, DocStrap Copyright © 2012-2013
The contributors to the JSDoc3 and DocStrap projects.