1 /**
  2  * Hilo
  3  * Copyright 2015 alibaba.com
  4  * Licensed under the MIT License
  5  */
  6 
  7 /**
  8  * @language=zh
  9  * <iframe src='../../../examples/Graphics.html?noHeader' width = '320' height = '400' scrolling='no'></iframe>
 10  * <br/>
 11  * @class Graphics类包含一组创建矢量图形的方法。
 12  * @augments View
 13  * @mixes CacheMixin
 14  * @param {Object} properties 创建对象的属性参数。可包含此类所有可写属性。
 15  * @module hilo/view/Graphics
 16  * @requires hilo/core/Hilo
 17  * @requires hilo/core/Class
 18  * @requires hilo/view/View
 19  * @requires hilo/view/CacheMixin
 20  * @property {Number} lineWidth 笔画的线条宽度。默认为1。只读属性。
 21  * @property {Number} lineAlpha 笔画的线条透明度。默认为1。只读属性。
 22  * @property {String} lineCap 笔画的线条端部样式。可选值有:butt、round、square等,默认为null。只读属性。
 23  * @property {String} lineJoin 笔画的线条连接样式。可选值有:miter、round、bevel等,默认为null。只读属性。
 24  * @property {Number} miterLimit 斜连线长度和线条宽度的最大比率。此属性仅当lineJoin为miter时有效。默认值为10。只读属性。
 25  * @property {String} strokeStyle 笔画边框的样式。默认值为'0',即黑色。只读属性。
 26  * @property {String} fillStyle 内容填充的样式。默认值为'0',即黑色。只读属性。
 27  * @property {Number} fillAlpha 内容填充的透明度。默认值为0。只读属性。
 28  */
 29 var Graphics = (function(){
 30 
 31 var canvas = document.createElement('canvas');
 32 var helpContext = canvas.getContext && canvas.getContext('2d');
 33 
 34 return Class.create(/** @lends Graphics.prototype */{
 35     Extends: View,
 36     Mixes:CacheMixin,
 37     constructor: function(properties){
 38         properties = properties || {};
 39         this.id = this.id || properties.id || Hilo.getUid('Graphics');
 40         Graphics.superclass.constructor.call(this, properties);
 41 
 42         this._actions = [];
 43     },
 44 
 45     lineWidth: 1,
 46     lineAlpha: 1,
 47     lineCap: null, //'butt', 'round', 'square'
 48     lineJoin: null, //'miter', 'round', 'bevel'
 49     miterLimit: 10,
 50     hasStroke: false,
 51     strokeStyle: '0',
 52     hasFill: false,
 53     fillStyle: '0',
 54     fillAlpha: 0,
 55 
 56     /**
 57      * @language=zh
 58      * 指定绘制图形的线条样式。
 59      * @param {Number} thickness 线条的粗细值。默认为1。
 60      * @param {String} lineColor 线条的CSS颜色值。默认为黑色,即'0'。
 61      * @param {Number} lineAlpha 线条的透明度值。默认为不透明,即1。
 62      * @param {String} lineCap 线条的端部样式。可选值有:butt、round、square等,默认值为null。
 63      * @param {String} lineJoin 线条的连接样式。可选值有:miter、round、bevel等,默认值为null。
 64      * @param {Number} miterLimit 斜连线长度和线条宽度的最大比率。此属性仅当lineJoin为miter时有效。默认值为10。
 65      * @returns {Graphics} Graphics对象本身。
 66      */
 67     lineStyle: function(thickness, lineColor, lineAlpha, lineCap, lineJoin, miterLimit){
 68         var me = this, addAction = me._addAction;
 69 
 70         addAction.call(me, ['lineWidth', (me.lineWidth = thickness || 1)]);
 71         addAction.call(me, ['strokeStyle', (me.strokeStyle = lineColor || '0')]);
 72         addAction.call(me, ['lineAlpha', (me.lineAlpha = lineAlpha || 1)]);
 73         if(lineCap != undefined) addAction.call(me, ['lineCap', (me.lineCap = lineCap)]);
 74         if(lineJoin != undefined) addAction.call(me, ['lineJoin', (me.lineJoin = lineJoin)]);
 75         if(miterLimit != undefined) addAction.call(me, ['miterLimit', (me.miterLimit = miterLimit)]);
 76         me.hasStroke = true;
 77         return me;
 78     },
 79 
 80     /**
 81      * @language=zh
 82      * 指定绘制图形的填充样式和透明度。
 83      * @param {String} fill 填充样式。可以是color、gradient或pattern。
 84      * @param {Number} alpha 透明度。
 85      * @returns {Graphics} Graphics对象本身。
 86      */
 87     beginFill: function(fill, alpha){
 88         var me = this, addAction = me._addAction;
 89 
 90         addAction.call(me, ['fillStyle', (me.fillStyle = fill)]);
 91         addAction.call(me, ['fillAlpha', (me.fillAlpha = alpha || 1)]);
 92         me.hasFill = true;
 93         return me;
 94     },
 95 
 96     /**
 97      * @language=zh
 98      * 应用并结束笔画的绘制和图形样式的填充。
 99      * @returns {Graphics} Graphics对象本身。
100      */
101     endFill: function(){
102         var me = this, addAction = me._addAction;
103 
104         if(me.hasStroke) addAction.call(me, ['stroke']);
105         if(me.hasFill) addAction.call(me, ['fill']);
106         me.setCacheDirty(true);
107         return me;
108     },
109 
110     /**
111      * @language=zh
112      * 指定绘制图形的线性渐变填充样式。
113      * @param {Number} x0 渐变的起始点的x轴坐标。
114      * @param {Number} y0 渐变的起始点的y轴坐标。
115      * @param {Number} x1 渐变的结束点的x轴坐标。
116      * @param {Number} y1 渐变的结束点的y轴坐标。
117      * @param {Array} colors 渐变中使用的CSS颜色值数组。
118      * @param {Array} ratios 渐变中开始与结束之间的位置数组。需与colors数组里的颜色值一一对应,介于0.0与1.0之间的值。
119      * @returns {Graphics} Graphics对象本身。
120      */
121     beginLinearGradientFill: function(x0, y0, x1, y1, colors, ratios){
122         var me = this, gradient = helpContext.createLinearGradient(x0, y0, x1, y1);
123 
124         for (var i = 0, len = colors.length; i < len; i++){
125             gradient.addColorStop(ratios[i], colors[i]);
126         }
127         me.hasFill = true;
128         return me._addAction(['fillStyle', (me.fillStyle = gradient)]);
129     },
130 
131     /**
132      * @language=zh
133      * 指定绘制图形的放射性渐变填充样式。
134      * @param {Number} x0 渐变的起始圆的x轴坐标。
135      * @param {Number} y0 渐变的起始圆的y轴坐标。
136      * @param {Number} r0 渐变的起始圆的半径。
137      * @param {Number} x1 渐变的结束圆的x轴坐标。
138      * @param {Number} y1 渐变的结束圆的y轴坐标。
139      * @param {Number} r1 渐变的结束圆的半径。
140      * @param {Array} colors 渐变中使用的CSS颜色值数组。
141      * @param {Array} ratios 渐变中开始与结束之间的位置数组。需与colors数组里的颜色值一一对应,介于0.0与1.0之间的值。
142      * @returns {Graphics} Graphics对象本身。
143      */
144     beginRadialGradientFill: function(x0, y0, r0, x1, y1, r1, colors, ratios){
145         var me = this, gradient = helpContext.createRadialGradient(x0, y0, r0, x1, y1, r1);
146         for (var i = 0, len = colors.length; i < len; i++)
147         {
148             gradient.addColorStop(ratios[i], colors[i]);
149         }
150         me.hasFill = true;
151         return me._addAction(['fillStyle', (me.fillStyle = gradient)]);
152     },
153 
154     /**
155      * @language=zh
156      * 开始一个位图填充样式。
157      * @param {HTMLImageElement} image 指定填充的Image对象。
158      * @param {String} repetition 指定填充的重复设置参数。它可以是以下任意一个值:repeat, repeat-x, repeat-y, no-repeat。默认为''。
159      * @returns {Graphics} Graphics对象本身。
160      */
161     beginBitmapFill: function(image, repetition){
162         var me = this, pattern = helpContext.createPattern(image, repetition || '');
163         me.hasFill = true;
164         return me._addAction(['fillStyle', (me.fillStyle = pattern)]);
165     },
166 
167     /**
168      * @language=zh
169      * 开始一个新的路径。
170      * @returns {Graphics} Graphics对象本身。
171      */
172     beginPath: function(){
173         return this._addAction(['beginPath']);
174     },
175 
176     /**
177      * @language=zh
178      * 关闭当前的路径。
179      * @returns {Graphics} Graphics对象本身。
180      */
181     closePath: function(){
182         return this._addAction(['closePath']);
183     },
184 
185     /**
186      * @language=zh
187      * 将当前绘制位置移动到点(x, y)。
188      * @param {Number} x x轴坐标。
189      * @param {Number} y y轴坐标。
190      * @returns {Graphics} Graphics对象本身。
191      */
192     moveTo: function(x, y){
193         return this._addAction(['moveTo', x, y]);
194     },
195 
196     /**
197      * @language=zh
198      * 绘制从当前位置开始到点(x, y)结束的直线。
199      * @param {Number} x x轴坐标。
200      * @param {Number} y y轴坐标。
201      * @returns {Graphics} Graphics对象本身。
202      */
203     lineTo: function(x, y){
204         return this._addAction(['lineTo', x, y]);
205     },
206 
207     /**
208      * @language=zh
209      * 绘制从当前位置开始到点(x, y)结束的二次曲线。
210      * @param {Number} cpx 控制点cp的x轴坐标。
211      * @param {Number} cpy 控制点cp的y轴坐标。
212      * @param {Number} x x轴坐标。
213      * @param {Number} y y轴坐标。
214      * @returns {Graphics} Graphics对象本身。
215      */
216     quadraticCurveTo: function(cpx, cpy, x, y){
217         return this._addAction(['quadraticCurveTo', cpx, cpy, x, y]);
218     },
219 
220     /**
221      * @language=zh
222      * 绘制从当前位置开始到点(x, y)结束的贝塞尔曲线。
223      * @param {Number} cp1x 控制点cp1的x轴坐标。
224      * @param {Number} cp1y 控制点cp1的y轴坐标。
225      * @param {Number} cp2x 控制点cp2的x轴坐标。
226      * @param {Number} cp2y 控制点cp2的y轴坐标。
227      * @param {Number} x x轴坐标。
228      * @param {Number} y y轴坐标。
229      * @returns {Graphics} Graphics对象本身。
230      */
231     bezierCurveTo: function(cp1x, cp1y, cp2x, cp2y, x, y){
232         return this._addAction(['bezierCurveTo', cp1x, cp1y, cp2x, cp2y, x, y]);
233     },
234 
235     /**
236      * @language=zh
237      * 绘制一个矩形。
238      * @param {Number} x x轴坐标。
239      * @param {Number} y y轴坐标。
240      * @param {Number} width 矩形的宽度。
241      * @param {Number} height 矩形的高度。
242      * @returns {Graphics} Graphics对象本身。
243      */
244     drawRect: function(x, y, width, height){
245         return this._addAction(['rect', x, y, width, height]);
246     },
247 
248     /**
249      * @language=zh
250      * 绘制一个复杂的圆角矩形。
251      * @param {Number} x x轴坐标。
252      * @param {Number} y y轴坐标。
253      * @param {Number} width 圆角矩形的宽度。
254      * @param {Number} height 圆角矩形的高度。
255      * @param {Number} cornerTL 圆角矩形的左上圆角大小。
256      * @param {Number} cornerTR 圆角矩形的右上圆角大小。
257      * @param {Number} cornerBR 圆角矩形的右下圆角大小。
258      * @param {Number} cornerBL 圆角矩形的左下圆角大小。
259      * @returns {Graphics} Graphics对象本身。
260      */
261     drawRoundRectComplex: function(x, y, width, height, cornerTL, cornerTR, cornerBR, cornerBL){
262         var me = this, addAction = me._addAction;
263         addAction.call(me, ['moveTo', x + cornerTL, y]);
264         addAction.call(me, ['lineTo', x + width - cornerTR, y]);
265         addAction.call(me, ['arc', x + width - cornerTR, y + cornerTR, cornerTR, -Math.PI/2, 0, false]);
266         addAction.call(me, ['lineTo', x + width, y + height - cornerBR]);
267         addAction.call(me, ['arc', x + width - cornerBR, y + height - cornerBR, cornerBR, 0, Math.PI/2, false]);
268         addAction.call(me, ['lineTo', x + cornerBL, y + height]);
269         addAction.call(me, ['arc', x + cornerBL, y + height - cornerBL, cornerBL, Math.PI/2, Math.PI, false]);
270         addAction.call(me, ['lineTo', x, y + cornerTL]);
271         addAction.call(me, ['arc', x + cornerTL, y + cornerTL, cornerTL, Math.PI, Math.PI*3/2, false]);
272         return me;
273     },
274 
275     /**
276      * @language=zh
277      * 绘制一个圆角矩形。
278      * @param {Number} x x轴坐标。
279      * @param {Number} y y轴坐标。
280      * @param {Number} width 圆角矩形的宽度。
281      * @param {Number} height 圆角矩形的高度。
282      * @param {Number} cornerSize 圆角矩形的圆角大小。
283      * @returns {Graphics} Graphics对象本身。
284      */
285     drawRoundRect: function(x, y, width, height, cornerSize){
286         return this.drawRoundRectComplex(x, y, width, height, cornerSize, cornerSize, cornerSize, cornerSize);
287     },
288 
289     /**
290      * @language=zh
291      * 绘制一个圆。
292      * @param {Number} x x轴坐标。
293      * @param {Number} y y轴坐标。
294      * @param {Number} radius 圆的半径。
295      * @returns {Graphics} Graphics对象本身。
296      */
297     drawCircle: function(x, y, radius){
298         return this._addAction(['arc', x + radius, y + radius, radius, 0, Math.PI * 2, 0]);
299     },
300 
301     /**
302      * @language=zh
303      * 绘制一个椭圆。
304      * @param {Number} x x轴坐标。
305      * @param {Number} y y轴坐标。
306      * @param {Number} width 椭圆的宽度。
307      * @param {Number} height 椭圆的高度。
308      * @returns {Graphics} Graphics对象本身。
309      */
310     drawEllipse: function(x, y, width, height){
311         var me = this;
312         if(width == height) return me.drawCircle(x, y, width);
313 
314         var addAction = me._addAction;
315         var w = width / 2, h = height / 2, C = 0.5522847498307933, cx = C * w, cy = C * h;
316         x = x + w;
317         y = y + h;
318 
319         addAction.call(me, ['moveTo', x + w, y]);
320         addAction.call(me, ['bezierCurveTo', x + w, y - cy, x + cx, y - h, x, y - h]);
321         addAction.call(me, ['bezierCurveTo', x - cx, y - h, x - w, y - cy, x - w, y]);
322         addAction.call(me, ['bezierCurveTo', x - w, y + cy, x - cx, y + h, x, y + h]);
323         addAction.call(me, ['bezierCurveTo', x + cx, y + h, x + w, y + cy, x + w, y]);
324         return me;
325     },
326 
327     /**
328      * @language=zh
329      * 根据参数指定的SVG数据绘制一条路径。
330      * 代码示例:
331      * <p>var path = 'M250 150 L150 350 L350 350 Z';</p>
332      * <p>var shape = new Hilo.Graphics({width:500, height:500});</p>
333      * <p>shape.drawSVGPath(path).beginFill('#0ff').endFill();</p>
334      * @param {String} pathData 要绘制的SVG路径数据。
335      * @returns {Graphics} Graphics对象本身。
336      */
337     drawSVGPath: function(pathData){
338         var me = this, addAction = me._addAction,
339             path = pathData.split(/,| (?=[a-zA-Z])/);
340 
341         addAction.call(me, ['beginPath']);
342         for(var i = 0, len = path.length; i < len; i++){
343             var str = path[i], cmd = str[0].toUpperCase(), p = str.substring(1).split(/,| /);
344             if(p[0].length == 0) p.shift();
345 
346             switch(cmd){
347                 case 'M':
348                     addAction.call(me, ['moveTo', p[0], p[1]]);
349                     break;
350                 case 'L':
351                     addAction.call(me, ['lineTo', p[0], p[1]]);
352                     break;
353                 case 'C':
354                     addAction.call(me, ['bezierCurveTo', p[0], p[1], p[2], p[3], p[4], p[5]]);
355                     break;
356                 case 'Z':
357                     addAction.call(me, ['closePath']);
358                     break;
359             }
360         }
361         return me;
362     },
363 
364     /**
365      * @language=zh
366      * 执行全部绘制动作。内部私有方法。
367      * @private
368      */
369     _draw: function(context){
370         var me = this, actions = me._actions, len = actions.length, i;
371 
372         context.beginPath();
373         for(i = 0; i < len; i++){
374             var action = actions[i],
375                 f = action[0],
376                 args = action.length > 1 ? action.slice(1) : null;
377 
378             if(typeof(context[f]) == 'function') context[f].apply(context, args);
379             else context[f] = action[1];
380         }
381     },
382 
383     /**
384      * @language=zh
385      * 重写渲染实现。
386      * @private
387      */
388     render: function(renderer, delta){
389         var me = this, canvas = renderer.canvas;
390         if(renderer.renderType === 'canvas'){
391             me._draw(renderer.context);
392         }else{
393             me.cache();
394             renderer.draw(me);
395         }
396     },
397 
398     /**
399      * @language=zh
400      * 清除所有绘制动作并复原所有初始状态。
401      * @returns {Graphics} Graphics对象本身。
402      */
403     clear: function(){
404         var me = this;
405 
406         me._actions.length = 0;
407         me.lineWidth = 1;
408         me.lineAlpha = 1;
409         me.lineCap = null;
410         me.lineJoin = null;
411         me.miterLimit = 10;
412         me.hasStroke = false;
413         me.strokeStyle = '0';
414         me.hasFill = false;
415         me.fillStyle = '0';
416         me.fillAlpha = 1;
417 
418         me.setCacheDirty(true);
419         return me;
420     },
421 
422     /**
423      * @language=zh
424      * 添加一个绘制动作。内部私有方法。
425      * @private
426      */
427     _addAction: function(action){
428         var me = this;
429         me._actions.push(action);
430         return me;
431     }
432 
433 });
434 
435 })();
436