1 /** 2 * Copyright (C) 2009-2011 Klaus Reimer <k@ailis.de> 3 * See LICENSE.txt for licensing information. 4 * 5 * @require twodee.js 6 * @require twodee/SceneNode.js 7 */ 8 9 /** 10 * Constructs a new polygon node. 11 * 12 * @param {twodee.Polygon} polygon 13 * Optional polygon. If not set then it must be set with the 14 * setPolygon() method. 15 * @param {string=} fillStyle 16 * Optional fill style. null means no filling. Default is a white 17 * color. 18 * @param {string=} strokeStyle 19 * Optional stroke style. null means no stroking and this is the 20 * default. 21 * 22 * @constructor 23 * @extends {twodee.SceneNode} 24 * @class 25 * A node which draws a polygon. 26 */ 27 twodee.PolygonNode = function(polygon, fillStyle, strokeStyle) 28 { 29 twodee.SceneNode.call(this); 30 if (polygon) this.setPolygon(polygon); 31 if (fillStyle !== undefined) this.fillStyle = fillStyle; 32 if (strokeStyle !== undefined) this.strokeStyle = strokeStyle; 33 twodee.PolygonNode.counter++; 34 }; 35 twodee.inherit(twodee.PolygonNode, twodee.SceneNode); 36 37 /** 38 * Instance counter. 39 * 40 * @type {number} 41 */ 42 twodee.PolygonNode.counter = 0; 43 44 /** 45 * The polygon. 46 * 47 * @private 48 * @type {twodee.Polygon} 49 */ 50 twodee.PolygonNode.prototype.polygon = null; 51 52 /** 53 * The fill style. 54 * 55 * @private 56 * @type {?string} 57 */ 58 twodee.PolygonNode.prototype.fillStyle = "#fff"; 59 60 /** 61 * The stroke style. 62 * 63 * @private 64 * @type {?string} 65 */ 66 twodee.PolygonNode.prototype.strokeStyle = null; 67 68 /** 69 * Sets the polygon. 70 * 71 * @param {twodee.Polygon} polygon 72 * The polygon to set 73 */ 74 twodee.PolygonNode.prototype.setPolygon = function(polygon) 75 { 76 this.setBounds(polygon); 77 this.polygon = this.getBounds(); 78 }; 79 80 /** 81 * Returns the polygon. 82 * 83 * @return {twodee.Polygon} The polygon 84 */ 85 twodee.PolygonNode.prototype.getPolygon = function() 86 { 87 return this.polygon; 88 }; 89 90 /** 91 * Sets the fill style. null means no filling. 92 * 93 * @param {?string} fillStyle 94 * The fill style to set 95 */ 96 twodee.PolygonNode.prototype.setFillStyle = function(fillStyle) 97 { 98 this.fillStyle = fillStyle; 99 }; 100 101 /** 102 * Returns the fill style. 103 * 104 * @return {?string} The fill style 105 */ 106 twodee.PolygonNode.prototype.getFillStyle = function() 107 { 108 return this.fillStyle; 109 }; 110 111 /** 112 * Sets the stroke style. null means no stroking. 113 * 114 * @param {?string} strokeStyle 115 * The stroke style to set 116 */ 117 twodee.PolygonNode.prototype.setStrokeStyle = function(strokeStyle) 118 { 119 this.strokeStyle = strokeStyle; 120 }; 121 122 /** 123 * Returns the stroke style. 124 * 125 * @return {?string} The stroke style 126 */ 127 128 twodee.PolygonNode.prototype.getStrokeStyle = function() 129 { 130 return this.strokeStyle; 131 }; 132 133 /** 134 * @see twodee.SceneNode#render 135 * 136 * @param {CanvasRenderingContext2D} g 137 * The graphics context 138 * @override 139 */ 140 twodee.PolygonNode.prototype.render = function(g) 141 { 142 var polygon, fillStyle, strokeStyle; 143 144 fillStyle = this.fillStyle; 145 strokeStyle = this.strokeStyle; 146 147 // Only do something when fill and stroke color is present 148 if (fillStyle || strokeStyle) 149 { 150 // Apply the polygon to the canvas 151 polygon = this.polygon; 152 polygon.apply(g); 153 154 // Fill the polygon if fill color is set 155 if (fillStyle) 156 { 157 g.fillStyle = fillStyle; 158 g.fill(); 159 } 160 161 // Stroke the polygon if stroke color is set 162 if (strokeStyle) 163 { 164 g.strokeStyle = strokeStyle; 165 g.stroke(); 166 } 167 } 168 }; 169