mirror of
https://github.com/Viglino/ol-ext.git
synced 2025-12-08 19:26:29 +00:00
95 lines
2.4 KiB
JavaScript
95 lines
2.4 KiB
JavaScript
/* Copyright (c) 2016 Jean-Marc VIGLINO,
|
|
released under the CeCILL-B license (French BSD license)
|
|
(http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt).
|
|
*/
|
|
|
|
import ol from 'ol'
|
|
import ol_filter_Base from './Base'
|
|
import ol_color from 'ol/color'
|
|
|
|
/** Mask drawing using an ol.Feature
|
|
* @constructor
|
|
* @requires ol_filter
|
|
* @extends {ol_filter_Base}
|
|
* @param {ol_filter_CropOptions} options
|
|
* - feature {ol.Feature} feature to mask with
|
|
* - fill {ol.style.Fill} style to fill with
|
|
* - inner {bool} mask inner, default false
|
|
*/
|
|
var ol_filter_Mask = function(options)
|
|
{ options = options || {};
|
|
ol_filter_Base.call(this, options);
|
|
if (options.feature)
|
|
{ switch (options.feature.getGeometry().getType())
|
|
{ case "Polygon":
|
|
case "MultiPolygon":
|
|
this.feature_ = options.feature;
|
|
break;
|
|
default: break;
|
|
}
|
|
}
|
|
this.set("inner", options.inner);
|
|
this.fillColor_ = options.fill ? ol_color.asString(options.fill.getColor()) || "rgba(0,0,0,0.2)" : "rgba(0,0,0,0.2)";
|
|
}
|
|
ol.inherits(ol_filter_Mask, ol_filter_Base);
|
|
|
|
/** Draw the feature into canvas
|
|
*/
|
|
ol_filter_Mask.prototype.drawFeaturePath_ = function(e, out)
|
|
{ var ctx = e.context;
|
|
var canvas = ctx.canvas;
|
|
var ratio = e.frameState.pixelRatio;
|
|
// Transform
|
|
var m = e.frameState.coordinateToPixelTransform;
|
|
function tr(pt)
|
|
{ return [
|
|
(pt[0]*m[0]+pt[1]*m[1]+m[4])*ratio,
|
|
(pt[0]*m[2]+pt[1]*m[3]+m[5])*ratio
|
|
];
|
|
}
|
|
// Old version
|
|
if (!m)
|
|
{ m = e.frameState.coordinateToPixelMatrix;
|
|
tr = function(pt)
|
|
{ return [
|
|
(pt[0]*m[0]+pt[1]*m[1]+m[12])*ratio,
|
|
(pt[0]*m[4]+pt[1]*m[5]+m[13])*ratio
|
|
];
|
|
}
|
|
}
|
|
// Geometry
|
|
var ll = this.feature_.getGeometry().getCoordinates();
|
|
if (this.feature_.getGeometry().getType()=="Polygon") ll = [ll];
|
|
ctx.beginPath();
|
|
if (out)
|
|
{ ctx.moveTo (0,0);
|
|
ctx.lineTo (canvas.width, 0);
|
|
ctx.lineTo (canvas.width, canvas.height);
|
|
ctx.lineTo (0, canvas.height);
|
|
ctx.lineTo (0, 0);
|
|
}
|
|
for (var l=0; l<ll.length; l++)
|
|
{ var c = ll[l];
|
|
for (var i=0; i<c.length; i++)
|
|
{ var pt = tr(c[i][0]);
|
|
ctx.moveTo (pt[0], pt[1]);
|
|
for (var j=1; j<c[i].length; j++)
|
|
{ pt = tr(c[i][j]);
|
|
ctx.lineTo (pt[0], pt[1]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
ol_filter_Mask.prototype.postcompose = function(e)
|
|
{ if (!this.feature_) return;
|
|
var ctx = e.context;
|
|
ctx.save();
|
|
this.drawFeaturePath_(e, !this.get("inner"));
|
|
ctx.fillStyle = this.fillColor_;
|
|
ctx.fill("evenodd");
|
|
ctx.restore();
|
|
}
|
|
|
|
export default ol_filter_Mask
|