View 类型

模块: _decorator 父模块: cc

cc.view is the singleton object which represents the game window.
It's main task include:

  • Apply the design resolution policy
  • Provide interaction with the window, like resize event on web, retina display support, etc...
  • Manage the game view port which can be different with the window
  • Manage the content scale and translation

    Since the cc.view is a singleton, you don't need to call any constructor or create functions,
    the standard way to use it is by calling:
  • cc.view.methodName();

索引

方法
  • setTargetDensityDPI

    Sets view's target-densitydpi for android mobile browser. it can be set to:

  • cc.macro.DENSITYDPI_DEVICE, value is "device-dpi"
  • cc.macro.DENSITYDPI_HIGH, value is "high-dpi" (default value)
  • cc.macro.DENSITYDPI_MEDIUM, value is "medium-dpi" (browser's default value)
  • cc.macro.DENSITYDPI_LOW, value is "low-dpi"
  • Custom value, e.g: "480"
    </p>
  • getTargetDensityDPI Returns the current target-densitydpi value of cc.view.
  • resizeWithBrowserSize Sets whether resize canvas automatically when browser's size changed.
    Useful only on web.
  • setResizeCallback Sets the callback function for cc.view's resize action,
    this callback will be invoked before applying resolution policy,
    so you can do any additional modifications within the callback.
    Useful only on web.
  • setOrientation Sets the orientation of the game, it can be landscape, portrait or auto. When set it to landscape or portrait, and screen w/h ratio doesn't fit, cc.view will automatically rotate the game canvas using CSS. Note that this function doesn't have any effect in native, in native, you need to set the application orientation in native project settings
  • adjustViewPort Sets whether the engine modify the "viewport" meta in your web page.
    It's enabled by default, we strongly suggest you not to disable it.
    And even when it's enabled, you can still set your own "viewport" meta, it won't be overridden
    Only useful on web
  • enableRetina Retina support is enabled by default for Apple device but disabled for other devices,
    it takes effect only when you called setDesignResolutionPolicy
    Only useful on web
  • isRetinaEnabled Check whether retina display is enabled.
    Only useful on web
  • enableAntiAlias 控制抗锯齿是否开启
  • isAntiAliasEnabled 返回当前是否抗锯齿
  • enableAutoFullScreen If enabled, the application will try automatically to enter full screen mode on mobile devices
    You can pass true as parameter to enable it and disable it by passing false.
    Only useful on web
  • isAutoFullScreenEnabled Check whether auto full screen is enabled.
    Only useful on web
  • isViewReady Get whether render system is ready(no matter opengl or canvas),
    this name is for the compatibility with cocos2d-x, subclass must implement this method.
  • setContentTranslateLeftTop Sets the resolution translate on View.
  • getContentTranslateLeftTop Returns the resolution translate on View
  • getFrameSize Returns the frame size of the view.
    On native platforms, it returns the screen size since the view is a fullscreen view.
    On web, it returns the size of the canvas's outer DOM element.
  • setFrameSize On native, it sets the frame size of view.
    On web, it sets the size of the canvas's outer DOM element.
  • getVisibleSize Returns the visible area size of the view port.
  • getVisibleSizeInPixel Returns the visible area size of the view port.
  • getVisibleOrigin Returns the visible origin of the view port.
  • getVisibleOriginInPixel Returns the visible origin of the view port.
  • canSetContentScaleFactor Returns whether developer can set content's scale factor.
  • getResolutionPolicy Returns the current resolution policy
  • setResolutionPolicy Sets the current resolution policy
  • setDesignResolutionSize Sets the resolution policy with designed view size in points.
    The resolution policy include:
    [1] ResolutionExactFit Fill screen by stretch-to-fit: if the design resolution ratio of width to height is different from the screen resolution ratio, your game view will be stretched.
    [2] ResolutionNoBorder Full screen without black border: if the design resolution ratio of width to height is different from the screen resolution ratio, two areas of your game view will be cut.
    [3] ResolutionShowAll Full screen with black border: if the design resolution ratio of width to height is different from the screen resolution ratio, two black borders will be shown.
    [4] ResolutionFixedHeight Scale the content's height to screen's height and proportionally scale its width
    [5] ResolutionFixedWidth Scale the content's width to screen's width and proportionally scale its height
    [cc.ResolutionPolicy] [Web only feature] Custom resolution policy, constructed by cc.ResolutionPolicy
  • getDesignResolutionSize Returns the designed size for the view. Default resolution size is the same as 'getFrameSize'.
  • setRealPixelResolution Sets the container to desired pixel resolution and fit the game content to it. This function is very useful for adaptation in mobile browsers. In some HD android devices, the resolution is very high, but its browser performance may not be very good. In this case, enabling retina display is very costy and not suggested, and if retina is disabled, the image may be blurry. But this API can be helpful to set a desired pixel resolution which is in between. This API will do the following:
    1. Set viewport's width to the desired width in pixel
    2. Set body width to the exact pixel resolution
    3. The resolution policy will be reset with designed view size in points.
  • setViewPortInPoints Sets view port rectangle with points.
  • setScissorInPoints Sets Scissor rectangle with points.
  • isScissorEnabled Returns whether GL_SCISSOR_TEST is enable
  • getScissorRect Returns the current scissor rectangle
  • setViewName Sets the name of the view
  • getViewName Returns the name of the view
  • getViewPortRect Returns the view port rectangle.
  • getScaleX Returns scale factor of the horizontal direction (X axis).
  • getScaleY Returns scale factor of the vertical direction (Y axis).
  • getDevicePixelRatio Returns device pixel ratio for retina display.
  • convertToLocationInView Returns the real location in view for a translation based on a related position
  • _getInstance

Details

方法

setTargetDensityDPI

Sets view's target-densitydpi for android mobile browser. it can be set to:
1. cc.macro.DENSITYDPI_DEVICE, value is "device-dpi"
2. cc.macro.DENSITYDPI_HIGH, value is "high-dpi" (default value)
3. cc.macro.DENSITYDPI_MEDIUM, value is "medium-dpi" (browser's default value)
4. cc.macro.DENSITYDPI_LOW, value is "low-dpi"
5. Custom value, e.g: "480"

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:239
参数列表
getTargetDensityDPI

Returns the current target-densitydpi value of cc.view.

meta description
返回 String
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:257
resizeWithBrowserSize

Sets whether resize canvas automatically when browser's size changed.
Useful only on web.

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:266
参数列表
  • enabled Boolean Whether enable automatic resize with browser's resize event
setResizeCallback

Sets the callback function for cc.view's resize action,
this callback will be invoked before applying resolution policy,
so you can do any additional modifications within the callback.
Useful only on web.

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:290
参数列表
  • callback Function | Null The callback function
setOrientation

Sets the orientation of the game, it can be landscape, portrait or auto. When set it to landscape or portrait, and screen w/h ratio doesn't fit, cc.view will automatically rotate the game canvas using CSS. Note that this function doesn't have any effect in native, in native, you need to set the application orientation in native project settings

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:304
参数列表
  • orientation Number Possible values: cc.macro.ORIENTATION_LANDSCAPE | cc.macro.ORIENTATION_PORTRAIT | cc.macro.ORIENTATION_AUTO
adjustViewPort

Sets whether the engine modify the "viewport" meta in your web page.
It's enabled by default, we strongly suggest you not to disable it.
And even when it's enabled, you can still set your own "viewport" meta, it won't be overridden
Only useful on web

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:419
参数列表
  • enabled Boolean Enable automatic modification to "viewport" meta
enableRetina

Retina support is enabled by default for Apple device but disabled for other devices,
it takes effect only when you called setDesignResolutionPolicy
Only useful on web

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:431
参数列表
  • enabled Boolean Enable or disable retina display
isRetinaEnabled

Check whether retina display is enabled.
Only useful on web

meta description
返回 Boolean
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:442
enableAntiAlias

控制抗锯齿是否开启

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:452
参数列表
  • enabled Boolean Enable or not anti-alias
isAntiAliasEnabled

返回当前是否抗锯齿

meta description
返回 Boolean
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:492
enableAutoFullScreen

If enabled, the application will try automatically to enter full screen mode on mobile devices
You can pass true as parameter to enable it and disable it by passing false.
Only useful on web

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:501
参数列表
  • enabled Boolean Enable or disable auto full screen on mobile devices
isAutoFullScreenEnabled

Check whether auto full screen is enabled.
Only useful on web

meta description
返回 Boolean
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:522
isViewReady

Get whether render system is ready(no matter opengl or canvas),
this name is for the compatibility with cocos2d-x, subclass must implement this method.

meta description
返回 Boolean
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:532
setContentTranslateLeftTop

Sets the resolution translate on View.

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:552
参数列表
getContentTranslateLeftTop

Returns the resolution translate on View

meta description
返回 Size | Object
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:562
getFrameSize

Returns the frame size of the view.
On native platforms, it returns the screen size since the view is a fullscreen view.
On web, it returns the size of the canvas's outer DOM element.

meta description
返回 Size
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:605
setFrameSize

On native, it sets the frame size of view.
On web, it sets the size of the canvas's outer DOM element.

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:616
参数列表
getVisibleSize

Returns the visible area size of the view port.

meta description
返回 Size
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:632
getVisibleSizeInPixel

Returns the visible area size of the view port.

meta description
返回 Size
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:641
getVisibleOrigin

Returns the visible origin of the view port.

meta description
返回 Vec2
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:651
getVisibleOriginInPixel

Returns the visible origin of the view port.

meta description
返回 Vec2
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:660
canSetContentScaleFactor

Returns whether developer can set content's scale factor.

meta description
返回 Boolean
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:670
getResolutionPolicy

Returns the current resolution policy

meta description
返回 ResolutionPolicy
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:679
setResolutionPolicy

Sets the current resolution policy

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:689
参数列表
setDesignResolutionSize

Sets the resolution policy with designed view size in points.
The resolution policy include:
[1] ResolutionExactFit Fill screen by stretch-to-fit: if the design resolution ratio of width to height is different from the screen resolution ratio, your game view will be stretched.
[2] ResolutionNoBorder Full screen without black border: if the design resolution ratio of width to height is different from the screen resolution ratio, two areas of your game view will be cut.
[3] ResolutionShowAll Full screen with black border: if the design resolution ratio of width to height is different from the screen resolution ratio, two black borders will be shown.
[4] ResolutionFixedHeight Scale the content's height to screen's height and proportionally scale its width
[5] ResolutionFixedWidth Scale the content's width to screen's width and proportionally scale its height
[cc.ResolutionPolicy] [Web only feature] Custom resolution policy, constructed by cc.ResolutionPolicy

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:716
参数列表
getDesignResolutionSize

Returns the designed size for the view. Default resolution size is the same as 'getFrameSize'.

meta description
返回 Size
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:806
setRealPixelResolution

Sets the container to desired pixel resolution and fit the game content to it. This function is very useful for adaptation in mobile browsers. In some HD android devices, the resolution is very high, but its browser performance may not be very good. In this case, enabling retina display is very costy and not suggested, and if retina is disabled, the image may be blurry. But this API can be helpful to set a desired pixel resolution which is in between. This API will do the following:

1. Set viewport's width to the desired width in pixel
2. Set body width to the exact pixel resolution
3. The resolution policy will be reset with designed view size in points.
meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:816
参数列表
setViewPortInPoints

Sets view port rectangle with points.

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:847
参数列表
setScissorInPoints

Sets Scissor rectangle with points.

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:863
参数列表
isScissorEnabled

Returns whether GL_SCISSOR_TEST is enable

meta description
返回 Boolean
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:892
getScissorRect

Returns the current scissor rectangle

meta description
返回 Rect
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:901
setViewName

Sets the name of the view

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:921
参数列表
getViewName

Returns the name of the view

meta description
返回 String
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:932
getViewPortRect

Returns the view port rectangle.

meta description
返回 Rect
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:941
getScaleX

Returns scale factor of the horizontal direction (X axis).

meta description
返回 Number
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:950
getScaleY

Returns scale factor of the vertical direction (Y axis).

meta description
返回 Number
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:959
getDevicePixelRatio

Returns device pixel ratio for retina display.

meta description
返回 Number
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:968
convertToLocationInView

Returns the real location in view for a translation based on a related position

meta description
返回 Vec2
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:977
参数列表
  • tx Number The X axis translation
  • ty Number The Y axis translation
  • relatedPos Object The related position object including "left", "top", "width", "height" informations
_getInstance
meta description
返回 View
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCView.js:1019

条与 "" 相匹配的结果

    没有与 "" 匹配的结果