dashed_line_sprite

Dashed Line Sprite for Cocos2d-html5

posted in: Cocos2D, HTML5, JavaScript | 0

I noticed a request on the cocos2d-html5 forum for a “dashed line” drawing class, so I wrote this class based on code posted to stackoverflow.

dashed_line_sprite

The class works with cocos2d-html5.

Usage: Add DashedlineSprite.js to your source files list in cocos2d.js e.g.

appFiles:[
‘src/resource.js’,
‘src/myApp.js’,
‘src/DashedLineSprite.js’
]

Then in use the sprite in your code like this

this.dashedLine = new DashedLineSprite(0,0,200,200,[10,5,5,5]);
this.addChild(this.dashedLine);

Note.

The array 10,5,5,5 specifies the dash patter, with pairs of indexes specifying the “on” and “off” length.

[10,5,5,5] specifies, Line for 10, space for 5, line for 5, space for 5. i.e its a alternating long and short dashed line.Also note, the colour is hard coded to white.

 

// DashedLineSprite.js

/****************************************************************************
 Copyright (c) 2012 Roger Clark
 www.rogerclark.net

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

var DashedLineSprite = cc.Sprite.extend(
    {
        _dashArray:null,
        _startX:null,
        _endX:null,
        _startY:null,
        _endY:null,
        ctor:function (startX,startY,endX,endY,dashArray)
        {
            this._super();

            this._startX= startX * cc.CONTENT_SCALE_FACTOR();
            this._startY =startY * cc.CONTENT_SCALE_FACTOR() * -1;
            this._endX =  endX * cc.CONTENT_SCALE_FACTOR();
            this._endY =endY * cc.CONTENT_SCALE_FACTOR() * -1;
            if (!dashArray)
            {
                this._dashArray=[10,5];
            }
            else
            {
                this._dashArray=dashArray;
            }
        },
        draw:function () {
            cc.renderContext.fillStyle = "rgba(255,255,255,1)";// Hardcoded colour
            cc.renderContext.strokeStyle = "rgba(255,255,255,1)";// Hardcoded colour
            cc.renderContext.beginPath();

            var x=this._startX;
            var y=this._startY;
            var x2=this._endX;
            var y2=this._endY;

            if (dashLength==0)
            {
                dashLength = 0.001;
            } // Hack for Safari
            var dashCount = this._dashArray.length;

            cc.renderContext.moveTo(x , y );
            var dx = (x2-x), dy = (y2-y);
            var slope = dy/dx;
            var distRemaining = Math.sqrt( dx * dx + dy * dy );
            var dashIndex=0, draw=true;
            while (distRemaining>=0.1)
            {
                var dashLength = this._dashArray[dashIndex++ % dashCount];
                if (dashLength > distRemaining)
                {
                    dashLength = distRemaining;
                }
                var xStep = Math.sqrt( dashLength*dashLength / (1 + slope*slope) );
                if (dx<0)
                {
                    xStep = -xStep;
                }
                x += xStep
                y += slope*xStep;
                if (draw)
                {
                    cc.renderContext.lineTo(x ,y );
                }
                else
                {
                    cc.renderContext.moveTo(x ,y );
                }
                distRemaining -= dashLength;
                draw = !draw;
            }
            cc.renderContext.closePath();
            cc.renderContext.stroke();
        }
    }
)