From 7193f5b445efd9ace67cb45be93be622d74b4281 Mon Sep 17 00:00:00 2001 From: Yansong Hong Date: Fri, 7 Feb 2020 16:03:40 -0800 Subject: [PATCH] 1540 Add support for xy point object and mixed array --- src/tiledimage.js | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/tiledimage.js b/src/tiledimage.js index 2e829cd5..e0e5b30c 100644 --- a/src/tiledimage.js +++ b/src/tiledimage.js @@ -677,14 +677,54 @@ $.extend($.TiledImage.prototype, $.EventSource.prototype, /** @lends OpenSeadrag /** * Sets an array of polygons to crop the TiledImage during draw tiles. * The render function will use the default non-zero winding rule. - * @param Polygons represented in an array of pair array in pixels. + * @param Polygons represented in an array of pair array or point object in pixels. * Example format: [ * [[197,172],[226,172],[226,198],[197,198]], // First polygon * [[328,200],[330,199],[332,201],[329,202]] // Second polygon + * [{x: 321, y:201}, {x: 356, y:205}, {x: 341, y:250}] // Third polygon * ] + * Argument may mix array and point objects. + * Point objects will be convert to array. */ setCroppingPolygons: function( polygons ) { - this._croppingPolygons = polygons; + + var isXYObject = function(obj) { + return obj instanceof $.Point || (typeof obj.x === 'number' && typeof obj.y === 'number'); + }; + var isArray = function(obj) { + return Object.prototype.toString.call(obj) === '[object Array]'; + }; + var isXYPair = function(obj) { + return obj && isArray(obj) && obj.length === 2; + }; + var convertXYObjectsToArrayIfNeeded = function(objs) { + return objs.map(function(obj) { + try { + if (isXYObject(obj)) { + return [obj.x, obj.y]; + } else if (isXYPair(obj)) { + return obj; + } else { + throw new Error(); + } + } catch(e) { + throw new Error('A Provided cropping polygon point is not supported'); + } + }); + }; + + try { + if (!isArray(polygons)) { + throw new Error('Provided cropping polygon is not an array'); + } + this._croppingPolygons = polygons.map(function(polygon){ + return convertXYObjectsToArrayIfNeeded(polygon); + }); + } catch (e) { + $.console.error('[TiledImage.setCroppingPolygons] Cropping polygon format not supported'); + $.console.error(e); + this._croppingPolygons = null; + } }, /**