Skip to content

Commit

Permalink
add mask in Layer.toJSON, fix #1013
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzhenn committed Nov 13, 2019
1 parent 898ed9c commit f6fb0e2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/layer/Layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Eventable from '../core/Eventable';
import JSONAble from '../core/JSONAble';
import Renderable from '../renderer/Renderable';
import CanvasRenderer from '../renderer/layer/CanvasRenderer';
import Geometry from '../geometry/Geometry';

/**
* @property {Object} [options=null] - base options of layer.
Expand Down Expand Up @@ -64,6 +65,9 @@ class Layer extends JSONAble(Eventable(Renderable(Class))) {
this.setId(id);
if (options) {
this.setZIndex(options.zIndex);
if (options.mask) {
this.setMask(Geometry.fromJSON(options.mask));
}
}
}

Expand Down Expand Up @@ -384,6 +388,7 @@ class Layer extends JSONAble(Eventable(Renderable(Class))) {
}
mask._bindLayer(this);
this._mask = mask;
this.options.mask = mask.toJSON();
if (!this.getMap() || this.getMap().isZooming()) {
return this;
}
Expand All @@ -400,6 +405,7 @@ class Layer extends JSONAble(Eventable(Renderable(Class))) {
*/
removeMask() {
delete this._mask;
delete this.options.mask;
if (!this.getMap() || this.getMap().isZooming()) {
return this;
}
Expand Down
17 changes: 17 additions & 0 deletions test/layer/MaskSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,21 @@ describe('Spec of Masks', function () {
});
});

it('mask with Layer.toJSON', function (done) {
vlayer.setMask(new maptalks.MultiPolygon([
new maptalks.Circle(map.getCenter(), 5).getShell(),
new maptalks.Circle(map.locate(map.getCenter(), 10, 0), 5).getShell()
]));
// map.addLayer(vlayer);
var json = vlayer.toJSON();
var layer2 = maptalks.Layer.fromJSON(json).addTo(map);
var canvas = layer2.getMap().getRenderer().canvas;
var c = new maptalks.Point(canvas.width / 2, canvas.height / 2);
layer2.once('layerload', function () {
expect(isDrawn(canvas, c.add(-11, 0))).not.to.be.ok();
expect(isDrawn(canvas, c.add(0, 0))).to.be.ok();
done();
});
});

});

0 comments on commit f6fb0e2

Please sign in to comment.