Skip to content

Commit

Permalink
Merge branch 'master' into screenshot-adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
manthey authored Apr 10, 2018
2 parents 7fcee27 + a4e9530 commit 08c99bb
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/gl/lineFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ var gl_lineFeature = function (arg) {
*/
function createGLLines(onlyStyle) {
var data = m_this.data(),
i, j, k, v, v2, lidx,
d, i, j, k, v, v2, lidx,
numSegments = 0, len,
lineItemList, lineItem, lineItemData,
vert = [{}, {}], v1 = vert[1],
Expand Down Expand Up @@ -416,22 +416,23 @@ var gl_lineFeature = function (arg) {
posFunc = m_this.position();
lineItemList = new Array(data.length);
for (i = 0; i < data.length; i += 1) {
lineItem = m_this.line()(data[i], i);
d = data[i];
lineItem = m_this.line()(d, i);
lineItemList[i] = lineItem;
if (lineItem.length < 2) {
continue;
}
numSegments += lineItem.length - 1;
for (j = 0; j < lineItem.length; j += 1) {
pos = posFunc(lineItem[j], j, lineItem, i);
pos = posFunc(lineItem[j], j, d, i);
position.push(pos.x);
position.push(pos.y);
position.push(pos.z || 0.0);
if (!j) {
firstpos = pos;
}
}
if (lineItem.length > 2 && (closedVal === undefined ? closedFunc(data[i], i) : closedVal)) {
if (lineItem.length > 2 && (closedVal === undefined ? closedFunc(d, i) : closedVal)) {
/* line is closed */
if (pos.x !== firstpos.x || pos.y !== firstpos.y ||
pos.z !== firstpos.z) {
Expand Down Expand Up @@ -487,6 +488,7 @@ var gl_lineFeature = function (arg) {
if (lineItem.length < 2) {
continue;
}
d = data[i];
firstPosIdx3 = posIdx3;
for (j = 0; j < lineItem.length + (closed[i] === 2 ? 1 : 0); j += 1, posIdx3 += 3) {
lidx = j;
Expand All @@ -510,11 +512,11 @@ var gl_lineFeature = function (arg) {
(j !== lidx ? firstPosIdx3 + 3 : firstPosIdx3 + 6 - closed[i] * 3) :
posIdx3);
}
v1.strokeWidth = strokeWidthVal === undefined ? strokeWidthFunc(lineItemData, lidx, lineItem, i) : strokeWidthVal;
v1.strokeColor = strokeColorVal === undefined ? strokeColorFunc(lineItemData, lidx, lineItem, i) : strokeColorVal;
v1.strokeOpacity = strokeOpacityVal === undefined ? strokeOpacityFunc(lineItemData, lidx, lineItem, i) : strokeOpacityVal;
v1.strokeWidth = strokeWidthVal === undefined ? strokeWidthFunc(lineItemData, lidx, d, i) : strokeWidthVal;
v1.strokeColor = strokeColorVal === undefined ? strokeColorFunc(lineItemData, lidx, d, i) : strokeColorVal;
v1.strokeOpacity = strokeOpacityVal === undefined ? strokeOpacityFunc(lineItemData, lidx, d, i) : strokeOpacityVal;
if (updateFlags) {
v1.strokeOffset = (strokeOffsetVal === undefined ? strokeOffsetFunc(lineItemData, lidx, lineItem, i) : strokeOffsetVal) || 0;
v1.strokeOffset = (strokeOffsetVal === undefined ? strokeOffsetFunc(lineItemData, lidx, d, i) : strokeOffsetVal) || 0;
if (v1.strokeOffset) {
/* we use 11 bits to store the offset, and we want to store values
* from -1 to 1, so multiply our values by 1023, and use some bit
Expand All @@ -525,9 +527,9 @@ var gl_lineFeature = function (arg) {
v1.posStrokeOffset = v1.negStrokeOffset = 0;
}
if (!closed[i] && (!j || j === lineItem.length - 1)) {
v1.flags = flagsLineCap[lineCapVal === undefined ? lineCapFunc(lineItemData, lidx, lineItem, i) : lineCapVal] || flagsLineCap.butt;
v1.flags = flagsLineCap[lineCapVal === undefined ? lineCapFunc(lineItemData, lidx, d, i) : lineCapVal] || flagsLineCap.butt;
} else {
v1.flags = flagsLineJoin[lineJoinVal === undefined ? lineJoinFunc(lineItemData, lidx, lineItem, i) : lineJoinVal] || flagsLineJoin.miter;
v1.flags = flagsLineJoin[lineJoinVal === undefined ? lineJoinFunc(lineItemData, lidx, d, i) : lineJoinVal] || flagsLineJoin.miter;
}
}

Expand Down
134 changes: 134 additions & 0 deletions tutorials/lines/index.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
extends ../common/index.pug

block mainTutorial
:markdown-it
# Tutorial - Lines
Add a line feature to a geospatial context. Lines can vary many
properties, including stroke width, color, opacity, and offset, line join
and cap, and closure.

First, create a map and a feature layer.

+codeblock('javascript', 1).
var map = geo.map({
node: "#map",
center: {x: 25, y: 20},
zoom: 5
});
// create a layer that supports drawing line features
var layer = map.createLayer('feature', {features: ['line']});

:markdown-it
Specify some data. A line feature is an array of data elements, each of
which has a list of vertices.

+codeblock('javascript', 2).
var lineData = [
[{x: 20, y: 10}, {x: 25, y: 10}],
[{x: 30, y: 10}, {x: 35, y: 12}, {x: 32, y: 15}],
[{x: 30, y: 20}, {x: 35, y: 22}, {x: 32, y: 25}],
[{x: 30, y: 30}, {x: 35, y: 32}, {x: 32, y: 35}, {x: 30, y: 30}],
[{x: 20, y: 20}, {x: 22, y: 20}, {x: 24, y: 20}, {x: 26, y: 20}],
[{x: 20, y: 30}, {x: 25, y: 32}, {x: 22, y: 35}, {x: 20, y: 35}]
];

:markdown-it
Create the line feature and set some styles on it.

+codeblock('javascript', 3).
line = layer.createFeature('line')
// set the data to our example data
.data(lineData)
// add some styles
.style({
// use a function for determining if the line should be closed based
// on where it is in our list of data.
closed: function (item, itemIndex) {
return itemIndex === 1 || itemIndex === 3 || itemIndex === 5;
},
strokeColor: 'black',
strokeWidth: 2
});
// draw the feature
line.draw();
+codeblock_test('map has a feature layer with six lines', [
'map.layers().length === 1',
'map.layers()[0] instanceof geo.featureLayer',
'map.layers()[0].features()[0] instanceof geo.lineFeature',
'map.layers()[0].features()[0].data().length === 6'
])

:markdown-it
Specify some more complex data. Our data elements are now objects with
various properties, which will require ``line`` and ``position`` functions
for the line feature to access.

+codeblock('javascript', 4, 1, false, 'Step 2-B').
var lineData = [{
coord: [[20, 10], [25, 10]]
}, {
coord: [[30, 10], [35, 12], [32, 15]],
closed: true
}, {
coord: [[30, 20], [35, 22], [32, 25]],
strokeColor: 'blue',
strokeWidth: 5
}, {
coord: [[30, 30], [35, 32], [32, 35], [30, 30]],
closed: true
}, {
coord: [
{x: 20, y: 20, width: 20},
{x: 22, y: 20, width: 5},
{x: 24, y: 20, width: 2},
{x: 26, y: 20, width: 2}
]
}, {
coord: [[20, 30], [25, 32], [22, 35], [20, 35]],
strokeWidth: 10,
lineJoin: 'bevel',
closed: true
}];

:markdown-it
Create the line feature and specify functions to access the data and set
styles. Our style functions are more complex, as they get information from
the line data items.

+codeblock('javascript', 5, undefined, true, 'Step 3-B').
line = layer.createFeature('line')
// set the data to our example data
.data(lineData)
// get the vertex list from the coord element of the line item
.line(function (item, itemIdx) {
return item.coord;
})
// the position can be an object or an array of [x, y]
.position(function (vertex, vertexIndex, item, itemIndex) {
return vertex.x === undefined ? {x: vertex[0], y: vertex[1]} : vertex;
})
// for styles, use the value specified by the line item if present
.style({
closed: function (item, itemIndex) {
return item.closed;
},
lineJoin: function (vertex, vertexIndex, item, itemIndex) {
return item.lineJoin;
},
strokeColor: function (vertex, vertexIndex, item, itemIndex) {
return item.strokeColor === undefined ? 'black' : item.strokeColor;
},
// stroke width might change by vertex
strokeWidth: function (vertex, vertexIndex, item, itemIndex) {
return vertex.width === undefined ? (
item.strokeWidth === undefined ? 1 : item.strokeWidth) : vertex.width;
}
});
// draw the feature
line.draw();
+codeblock_test('map has a feature layer with six lines', [
'map.layers().length === 1',
'map.layers()[0] instanceof geo.featureLayer',
'map.layers()[0].features()[0] instanceof geo.lineFeature',
'map.layers()[0].features()[0].data().length === 6'
])
Binary file added tutorials/lines/thumb.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions tutorials/lines/tutorial.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"title": "Lines",
"hideNavbar": true,
"level": 0,
"about": {
"text": "Place lines with different styles in a geospatial context."
}
}

0 comments on commit 08c99bb

Please sign in to comment.