Newer
Older
pixi.js / docs / files / src_pixi_renderers_canvas_CanvasRenderer.js.html
@mathew groves mathew groves on 11 Nov 2014 22 KB Docs republished
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>src/pixi/renderers/canvas/CanvasRenderer.js - pixi.js</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.9.1/build/cssgrids/cssgrids-min.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <link rel="stylesheet" href="../assets/css/main.css" id="site_styles">
    <link rel="shortcut icon" type="image/png" href="../assets/favicon.png">
    <script src="http://yui.yahooapis.com/combo?3.9.1/build/yui/yui-min.js"></script>
</head>
<body class="yui3-skin-sam">

<div id="doc">
    <div id="hd" class="yui3-g header">
        <div class="yui3-u-3-4">
            
                <h1><img src="http://www.goodboydigital.com/pixijs/logo_small.png" title="pixi.js"></h1>
            
        </div>
        <div class="yui3-u-1-4 version">
            <em>API Docs for: 2.1.0</em>
        </div>
    </div>
    <div id="bd" class="yui3-g">

        <div class="yui3-u-1-4">
            <div id="docs-sidebar" class="sidebar apidocs">
                <div id="api-list">
    <h2 class="off-left">APIs</h2>
    <div id="api-tabview" class="tabview">
        <ul class="tabs">
            <li><a href="#api-classes">Classes</a></li>
            <li><a href="#api-modules">Modules</a></li>
        </ul>

        <div id="api-tabview-filter">
            <input type="search" id="api-filter" placeholder="Type to filter APIs">
        </div>

        <div id="api-tabview-panel">
            <ul id="api-classes" class="apis classes">
            
                <li><a href="../classes/AbstractFilter.html">AbstractFilter</a></li>
            
                <li><a href="../classes/AjaxRequest.html">AjaxRequest</a></li>
            
                <li><a href="../classes/AlphaMaskFilter.html">AlphaMaskFilter</a></li>
            
                <li><a href="../classes/AsciiFilter.html">AsciiFilter</a></li>
            
                <li><a href="../classes/AssetLoader.html">AssetLoader</a></li>
            
                <li><a href="../classes/AtlasLoader.html">AtlasLoader</a></li>
            
                <li><a href="../classes/autoDetectRecommendedRenderer.html">autoDetectRecommendedRenderer</a></li>
            
                <li><a href="../classes/autoDetectRenderer.html">autoDetectRenderer</a></li>
            
                <li><a href="../classes/BaseTexture.html">BaseTexture</a></li>
            
                <li><a href="../classes/BitmapFontLoader.html">BitmapFontLoader</a></li>
            
                <li><a href="../classes/BitmapText.html">BitmapText</a></li>
            
                <li><a href="../classes/BlurFilter.html">BlurFilter</a></li>
            
                <li><a href="../classes/BlurXFilter.html">BlurXFilter</a></li>
            
                <li><a href="../classes/BlurYFilter.html">BlurYFilter</a></li>
            
                <li><a href="../classes/CanvasBuffer.html">CanvasBuffer</a></li>
            
                <li><a href="../classes/CanvasGraphics.html">CanvasGraphics</a></li>
            
                <li><a href="../classes/CanvasMaskManager.html">CanvasMaskManager</a></li>
            
                <li><a href="../classes/CanvasRenderer.html">CanvasRenderer</a></li>
            
                <li><a href="../classes/CanvasTinter.html">CanvasTinter</a></li>
            
                <li><a href="../classes/Circle.html">Circle</a></li>
            
                <li><a href="../classes/ColorMatrixFilter.html">ColorMatrixFilter</a></li>
            
                <li><a href="../classes/ColorStepFilter.html">ColorStepFilter</a></li>
            
                <li><a href="../classes/ComplexPrimitiveShader.html">ComplexPrimitiveShader</a></li>
            
                <li><a href="../classes/ConvolutionFilter.html">ConvolutionFilter</a></li>
            
                <li><a href="../classes/CrossHatchFilter.html">CrossHatchFilter</a></li>
            
                <li><a href="../classes/DisplacementFilter.html">DisplacementFilter</a></li>
            
                <li><a href="../classes/DisplayObject.html">DisplayObject</a></li>
            
                <li><a href="../classes/DisplayObjectContainer.html">DisplayObjectContainer</a></li>
            
                <li><a href="../classes/DotScreenFilter.html">DotScreenFilter</a></li>
            
                <li><a href="../classes/Ellipse.html">Ellipse</a></li>
            
                <li><a href="../classes/Event.html">Event</a></li>
            
                <li><a href="../classes/EventTarget.html">EventTarget</a></li>
            
                <li><a href="../classes/FilterBlock.html">FilterBlock</a></li>
            
                <li><a href="../classes/FilterTexture.html">FilterTexture</a></li>
            
                <li><a href="../classes/Graphics.html">Graphics</a></li>
            
                <li><a href="../classes/GraphicsData.html">GraphicsData</a></li>
            
                <li><a href="../classes/GrayFilter.html">GrayFilter</a></li>
            
                <li><a href="../classes/ImageLoader.html">ImageLoader</a></li>
            
                <li><a href="../classes/InteractionData.html">InteractionData</a></li>
            
                <li><a href="../classes/InteractionManager.html">InteractionManager</a></li>
            
                <li><a href="../classes/InvertFilter.html">InvertFilter</a></li>
            
                <li><a href="../classes/JsonLoader.html">JsonLoader</a></li>
            
                <li><a href="../classes/Matrix.html">Matrix</a></li>
            
                <li><a href="../classes/MovieClip.html">MovieClip</a></li>
            
                <li><a href="../classes/NoiseFilter.html">NoiseFilter</a></li>
            
                <li><a href="../classes/NormalMapFilter.html">NormalMapFilter</a></li>
            
                <li><a href="../classes/PixelateFilter.html">PixelateFilter</a></li>
            
                <li><a href="../classes/PixiFastShader.html">PixiFastShader</a></li>
            
                <li><a href="../classes/PixiShader.html">PixiShader</a></li>
            
                <li><a href="../classes/Point.html">Point</a></li>
            
                <li><a href="../classes/Polygon.html">Polygon</a></li>
            
                <li><a href="../classes/PolyK.html">PolyK</a></li>
            
                <li><a href="../classes/PrimitiveShader.html">PrimitiveShader</a></li>
            
                <li><a href="../classes/Rectangle.html">Rectangle</a></li>
            
                <li><a href="../classes/RenderTexture.html">RenderTexture</a></li>
            
                <li><a href="../classes/RGBSplitFilter.html">RGBSplitFilter</a></li>
            
                <li><a href="../classes/Rope.html">Rope</a></li>
            
                <li><a href="../classes/Rounded Rectangle.html">Rounded Rectangle</a></li>
            
                <li><a href="../classes/SepiaFilter.html">SepiaFilter</a></li>
            
                <li><a href="../classes/SmartBlurFilter.html">SmartBlurFilter</a></li>
            
                <li><a href="../classes/Spine.html">Spine</a></li>
            
                <li><a href="../classes/SpineLoader.html">SpineLoader</a></li>
            
                <li><a href="../classes/Sprite.html">Sprite</a></li>
            
                <li><a href="../classes/SpriteBatch.html">SpriteBatch</a></li>
            
                <li><a href="../classes/SpriteSheetLoader.html">SpriteSheetLoader</a></li>
            
                <li><a href="../classes/Stage.html">Stage</a></li>
            
                <li><a href="../classes/Strip.html">Strip</a></li>
            
                <li><a href="../classes/StripShader.html">StripShader</a></li>
            
                <li><a href="../classes/Text.html">Text</a></li>
            
                <li><a href="../classes/Texture.html">Texture</a></li>
            
                <li><a href="../classes/TilingSprite.html">TilingSprite</a></li>
            
                <li><a href="../classes/TiltShiftFilter.html">TiltShiftFilter</a></li>
            
                <li><a href="../classes/TiltShiftXFilter.html">TiltShiftXFilter</a></li>
            
                <li><a href="../classes/TiltShiftYFilter.html">TiltShiftYFilter</a></li>
            
                <li><a href="../classes/TwistFilter.html">TwistFilter</a></li>
            
                <li><a href="../classes/WebGLBlendModeManager.html">WebGLBlendModeManager</a></li>
            
                <li><a href="../classes/WebGLFastSpriteBatch.html">WebGLFastSpriteBatch</a></li>
            
                <li><a href="../classes/WebGLFilterManager.html">WebGLFilterManager</a></li>
            
                <li><a href="../classes/WebGLGraphics.html">WebGLGraphics</a></li>
            
                <li><a href="../classes/WebGLGraphicsData.html">WebGLGraphicsData</a></li>
            
                <li><a href="../classes/WebGLMaskManager.html">WebGLMaskManager</a></li>
            
                <li><a href="../classes/WebGLRenderer.html">WebGLRenderer</a></li>
            
                <li><a href="../classes/WebGLShaderManager.html">WebGLShaderManager</a></li>
            
                <li><a href="../classes/WebGLSpriteBatch.html">WebGLSpriteBatch</a></li>
            
                <li><a href="../classes/WebGLStencilManager.html">WebGLStencilManager</a></li>
            
            </ul>

            <ul id="api-modules" class="apis modules">
            
                <li><a href="../modules/PIXI.html">PIXI</a></li>
            
            </ul>
        </div>
    </div>
</div>

            </div>
        </div>
        <div class="yui3-u-3-4">
                <div id="api-options">
        Show:
        <label for="api-show-inherited">
            <input type="checkbox" id="api-show-inherited" checked>
            Inherited
        </label>

        <label for="api-show-protected">
            <input type="checkbox" id="api-show-protected">
            Protected
        </label>

        <label for="api-show-private">
            <input type="checkbox" id="api-show-private">
            Private
        </label>
        <label for="api-show-deprecated">
            <input type="checkbox" id="api-show-deprecated">
            Deprecated
        </label>

    </div>


            <div class="apidocs">
                <div id="docs-main">
                    <div class="content">
                        <h1 class="file-heading">File: src/pixi/renderers/canvas/CanvasRenderer.js</h1>

<div class="file">
    <pre class="code prettyprint linenums">
/**
 * @author Mat Groves http://matgroves.com/ @Doormat23
 */

/**
 * The CanvasRenderer draws the Stage and all its content onto a 2d canvas. This renderer should be used for browsers that do not support webGL.
 * Don&#x27;t forget to add the CanvasRenderer.view to your DOM or you will not see anything :)
 *
 * @class CanvasRenderer
 * @constructor
 * @param [width=800] {Number} the width of the canvas view
 * @param [height=600] {Number} the height of the canvas view
 * @param [options] {Object} The optional renderer parameters
 * @param [options.view] {HTMLCanvasElement} the canvas to use as a view, optional
 * @param [options.transparent=false] {Boolean} If the render view is transparent, default false
 * @param [options.autoResize=false] {Boolean} If the render view is automatically resized, default false
 * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2
 * @param [options.clearBeforeRender=true] {Boolean} This sets if the CanvasRenderer will clear the canvas or not before the new render pass.
 */
PIXI.CanvasRenderer = function(width, height, options)
{
    if(options)
    {
        for (var i in PIXI.defaultRenderOptions)
        {
            if (typeof options[i] === &quot;undefined&quot;) options[i] = PIXI.defaultRenderOptions[i];
        }
    }
    else
    {
        options = PIXI.defaultRenderOptions;
    }

    if(!PIXI.defaultRenderer)
    {
        PIXI.sayHello(&quot;Canvas&quot;);
        PIXI.defaultRenderer = this;
    }

    /**
     * The renderer type.
     *
     * @property type
     * @type Number
     */
    this.type = PIXI.CANVAS_RENDERER;

    /**
     * The resolution of the canvas.
     *
     * @property resolution
     * @type Number
     */
    this.resolution = options.resolution;

    /**
     * This sets if the CanvasRenderer will clear the canvas or not before the new render pass.
     * If the Stage is NOT transparent Pixi will use a canvas sized fillRect operation every frame to set the canvas background color.
     * If the Stage is transparent Pixi will use clearRect to clear the canvas every frame.
     * Disable this by setting this to false. For example if your game has a canvas filling background image you often don&#x27;t need this set.
     *
     * @property clearBeforeRender
     * @type Boolean
     * @default
     */
    this.clearBeforeRender = options.clearBeforeRender;

    /**
     * Whether the render view is transparent
     *
     * @property transparent
     * @type Boolean
     */
    this.transparent = options.transparent;

    /**
     * Whether the render view should be resized automatically
     *
     * @property autoResize
     * @type Boolean
     */
    this.autoResize = options.autoResize || false;


    /**
     * The width of the canvas view
     *
     * @property width
     * @type Number
     * @default 800
     */
    this.width = width || 800;

    /**
     * The height of the canvas view
     *
     * @property height
     * @type Number
     * @default 600
     */
    this.height = height || 600;

    this.width *= this.resolution;
    this.height *= this.resolution;

    /**
     * The canvas element that everything is drawn to.
     *
     * @property view
     * @type HTMLCanvasElement
     */
    this.view = options.view || document.createElement( &quot;canvas&quot; );

    /**
     * The canvas 2d context that everything is drawn with
     * @property context
     * @type CanvasRenderingContext2D
     */
    this.context = this.view.getContext( &quot;2d&quot;, { alpha: this.transparent } );

    /**
     * Boolean flag controlling canvas refresh.
     *
     * @property refresh
     * @type Boolean
     */
    this.refresh = true;

    this.view.width = this.width * this.resolution;
    this.view.height = this.height * this.resolution;

    /**
     * Internal var.
     *
     * @property count
     * @type Number
     */
    this.count = 0;

    /**
     * Instance of a PIXI.CanvasMaskManager, handles masking when using the canvas renderer
     * @property CanvasMaskManager
     * @type CanvasMaskManager
     */
    this.maskManager = new PIXI.CanvasMaskManager();

    /**
     * The render session is just a bunch of parameter used for rendering
     * @property renderSession
     * @type Object
     */
    this.renderSession = {
        context: this.context,
        maskManager: this.maskManager,
        scaleMode: null,
        smoothProperty: null,
        /**
         * If true Pixi will Math.floor() x/y values when rendering, stopping pixel interpolation.
         * Handy for crisp pixel art and speed on legacy devices.
         *
         */
        roundPixels: false
    };

    this.mapBlendModes();
    
    this.resize(width, height);

    if(&quot;imageSmoothingEnabled&quot; in this.context)
        this.renderSession.smoothProperty = &quot;imageSmoothingEnabled&quot;;
    else if(&quot;webkitImageSmoothingEnabled&quot; in this.context)
        this.renderSession.smoothProperty = &quot;webkitImageSmoothingEnabled&quot;;
    else if(&quot;mozImageSmoothingEnabled&quot; in this.context)
        this.renderSession.smoothProperty = &quot;mozImageSmoothingEnabled&quot;;
    else if(&quot;oImageSmoothingEnabled&quot; in this.context)
        this.renderSession.smoothProperty = &quot;oImageSmoothingEnabled&quot;;
    else if (&quot;msImageSmoothingEnabled&quot; in this.context)
        this.renderSession.smoothProperty = &quot;msImageSmoothingEnabled&quot;;
};

// constructor
PIXI.CanvasRenderer.prototype.constructor = PIXI.CanvasRenderer;

/**
 * Renders the Stage to this canvas view
 *
 * @method render
 * @param stage {Stage} the Stage element to be rendered
 */
PIXI.CanvasRenderer.prototype.render = function(stage)
{
    stage.updateTransform();

    this.context.setTransform(1,0,0,1,0,0);

    this.context.globalAlpha = 1;

    this.renderSession.currentBlendMode = PIXI.blendModes.NORMAL;
    this.context.globalCompositeOperation = PIXI.blendModesCanvas[PIXI.blendModes.NORMAL];

    if (navigator.isCocoonJS &amp;&amp; this.view.screencanvas) {
        this.context.fillStyle = &quot;black&quot;;
        this.context.clear();
    }
    
    if (this.clearBeforeRender)
    {
        if (this.transparent)
        {
            this.context.clearRect(0, 0, this.width, this.height);
        }
        else
        {
            this.context.fillStyle = stage.backgroundColorString;
            this.context.fillRect(0, 0, this.width , this.height);
        }
    }
    
    this.renderDisplayObject(stage);

    // run interaction!
    if(stage.interactive)
    {
        //need to add some events!
        if(!stage._interactiveEventsAdded)
        {
            stage._interactiveEventsAdded = true;
            stage.interactionManager.setTarget(this);
        }
    }
};

/**
 * Removes everything from the renderer and optionally removes the Canvas DOM element.
 *
 * @method destroy
 * @param [removeView=true] {boolean} Removes the Canvas element from the DOM.
 */
PIXI.CanvasRenderer.prototype.destroy = function(removeView)
{
    if (typeof removeView === &quot;undefined&quot;) { removeView = true; }

    if (removeView &amp;&amp; this.view.parent)
    {
        this.view.parent.removeChild(this.view);
    }

    this.view = null;
    this.context = null;
    this.maskManager = null;
    this.renderSession = null;

};

/**
 * Resizes the canvas view to the specified width and height
 *
 * @method resize
 * @param width {Number} the new width of the canvas view
 * @param height {Number} the new height of the canvas view
 */
PIXI.CanvasRenderer.prototype.resize = function(width, height)
{
    this.width = width * this.resolution;
    this.height = height * this.resolution;

    this.view.width = this.width;
    this.view.height = this.height;

    if (this.autoResize) {
        this.view.style.width = this.width / this.resolution + &quot;px&quot;;
        this.view.style.height = this.height / this.resolution + &quot;px&quot;;
    }
};

/**
 * Renders a display object
 *
 * @method renderDisplayObject
 * @param displayObject {DisplayObject} The displayObject to render
 * @param context {CanvasRenderingContext2D} the context 2d method of the canvas
 * @private
 */
PIXI.CanvasRenderer.prototype.renderDisplayObject = function(displayObject, context)
{
    this.renderSession.context = context || this.context;
    this.renderSession.resolution = this.resolution;
    displayObject._renderCanvas(this.renderSession);
};

/**
 * Maps Pixi blend modes to canvas blend modes.
 *
 * @method mapBlendModes
 * @private
 */
PIXI.CanvasRenderer.prototype.mapBlendModes = function()
{
    if(!PIXI.blendModesCanvas)
    {
        PIXI.blendModesCanvas = [];

        if(PIXI.canUseNewCanvasBlendModes())
        {
            PIXI.blendModesCanvas[PIXI.blendModes.NORMAL]   = &quot;source-over&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.ADD]      = &quot;lighter&quot;; //IS THIS OK???
            PIXI.blendModesCanvas[PIXI.blendModes.MULTIPLY] = &quot;multiply&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.SCREEN]   = &quot;screen&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.OVERLAY]  = &quot;overlay&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.DARKEN]   = &quot;darken&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.LIGHTEN]  = &quot;lighten&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.COLOR_DODGE] = &quot;color-dodge&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.COLOR_BURN] = &quot;color-burn&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.HARD_LIGHT] = &quot;hard-light&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.SOFT_LIGHT] = &quot;soft-light&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.DIFFERENCE] = &quot;difference&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.EXCLUSION] = &quot;exclusion&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.HUE]       = &quot;hue&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.SATURATION] = &quot;saturation&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.COLOR]      = &quot;color&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.LUMINOSITY] = &quot;luminosity&quot;;
        }
        else
        {
            // this means that the browser does not support the cool new blend modes in canvas &quot;cough&quot; ie &quot;cough&quot;
            PIXI.blendModesCanvas[PIXI.blendModes.NORMAL]   = &quot;source-over&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.ADD]      = &quot;lighter&quot;; //IS THIS OK???
            PIXI.blendModesCanvas[PIXI.blendModes.MULTIPLY] = &quot;source-over&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.SCREEN]   = &quot;source-over&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.OVERLAY]  = &quot;source-over&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.DARKEN]   = &quot;source-over&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.LIGHTEN]  = &quot;source-over&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.COLOR_DODGE] = &quot;source-over&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.COLOR_BURN] = &quot;source-over&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.HARD_LIGHT] = &quot;source-over&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.SOFT_LIGHT] = &quot;source-over&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.DIFFERENCE] = &quot;source-over&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.EXCLUSION] = &quot;source-over&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.HUE]       = &quot;source-over&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.SATURATION] = &quot;source-over&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.COLOR]      = &quot;source-over&quot;;
            PIXI.blendModesCanvas[PIXI.blendModes.LUMINOSITY] = &quot;source-over&quot;;
        }
    }
};

    </pre>
</div>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>
<script src="../assets/js/yui-prettify.js"></script>
<script src="../assets/../api.js"></script>
<script src="../assets/js/api-filter.js"></script>
<script src="../assets/js/api-list.js"></script>
<script src="../assets/js/api-search.js"></script>
<script src="../assets/js/apidocs.js"></script>
</body>
</html>