Skip to content

Commit 08c99bb

Browse files
authored
Merge branch 'master' into screenshot-adjustments
2 parents 7fcee27 + a4e9530 commit 08c99bb

File tree

4 files changed

+154
-10
lines changed

4 files changed

+154
-10
lines changed

src/gl/lineFeature.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ var gl_lineFeature = function (arg) {
372372
*/
373373
function createGLLines(onlyStyle) {
374374
var data = m_this.data(),
375-
i, j, k, v, v2, lidx,
375+
d, i, j, k, v, v2, lidx,
376376
numSegments = 0, len,
377377
lineItemList, lineItem, lineItemData,
378378
vert = [{}, {}], v1 = vert[1],
@@ -416,22 +416,23 @@ var gl_lineFeature = function (arg) {
416416
posFunc = m_this.position();
417417
lineItemList = new Array(data.length);
418418
for (i = 0; i < data.length; i += 1) {
419-
lineItem = m_this.line()(data[i], i);
419+
d = data[i];
420+
lineItem = m_this.line()(d, i);
420421
lineItemList[i] = lineItem;
421422
if (lineItem.length < 2) {
422423
continue;
423424
}
424425
numSegments += lineItem.length - 1;
425426
for (j = 0; j < lineItem.length; j += 1) {
426-
pos = posFunc(lineItem[j], j, lineItem, i);
427+
pos = posFunc(lineItem[j], j, d, i);
427428
position.push(pos.x);
428429
position.push(pos.y);
429430
position.push(pos.z || 0.0);
430431
if (!j) {
431432
firstpos = pos;
432433
}
433434
}
434-
if (lineItem.length > 2 && (closedVal === undefined ? closedFunc(data[i], i) : closedVal)) {
435+
if (lineItem.length > 2 && (closedVal === undefined ? closedFunc(d, i) : closedVal)) {
435436
/* line is closed */
436437
if (pos.x !== firstpos.x || pos.y !== firstpos.y ||
437438
pos.z !== firstpos.z) {
@@ -487,6 +488,7 @@ var gl_lineFeature = function (arg) {
487488
if (lineItem.length < 2) {
488489
continue;
489490
}
491+
d = data[i];
490492
firstPosIdx3 = posIdx3;
491493
for (j = 0; j < lineItem.length + (closed[i] === 2 ? 1 : 0); j += 1, posIdx3 += 3) {
492494
lidx = j;
@@ -510,11 +512,11 @@ var gl_lineFeature = function (arg) {
510512
(j !== lidx ? firstPosIdx3 + 3 : firstPosIdx3 + 6 - closed[i] * 3) :
511513
posIdx3);
512514
}
513-
v1.strokeWidth = strokeWidthVal === undefined ? strokeWidthFunc(lineItemData, lidx, lineItem, i) : strokeWidthVal;
514-
v1.strokeColor = strokeColorVal === undefined ? strokeColorFunc(lineItemData, lidx, lineItem, i) : strokeColorVal;
515-
v1.strokeOpacity = strokeOpacityVal === undefined ? strokeOpacityFunc(lineItemData, lidx, lineItem, i) : strokeOpacityVal;
515+
v1.strokeWidth = strokeWidthVal === undefined ? strokeWidthFunc(lineItemData, lidx, d, i) : strokeWidthVal;
516+
v1.strokeColor = strokeColorVal === undefined ? strokeColorFunc(lineItemData, lidx, d, i) : strokeColorVal;
517+
v1.strokeOpacity = strokeOpacityVal === undefined ? strokeOpacityFunc(lineItemData, lidx, d, i) : strokeOpacityVal;
516518
if (updateFlags) {
517-
v1.strokeOffset = (strokeOffsetVal === undefined ? strokeOffsetFunc(lineItemData, lidx, lineItem, i) : strokeOffsetVal) || 0;
519+
v1.strokeOffset = (strokeOffsetVal === undefined ? strokeOffsetFunc(lineItemData, lidx, d, i) : strokeOffsetVal) || 0;
518520
if (v1.strokeOffset) {
519521
/* we use 11 bits to store the offset, and we want to store values
520522
* from -1 to 1, so multiply our values by 1023, and use some bit
@@ -525,9 +527,9 @@ var gl_lineFeature = function (arg) {
525527
v1.posStrokeOffset = v1.negStrokeOffset = 0;
526528
}
527529
if (!closed[i] && (!j || j === lineItem.length - 1)) {
528-
v1.flags = flagsLineCap[lineCapVal === undefined ? lineCapFunc(lineItemData, lidx, lineItem, i) : lineCapVal] || flagsLineCap.butt;
530+
v1.flags = flagsLineCap[lineCapVal === undefined ? lineCapFunc(lineItemData, lidx, d, i) : lineCapVal] || flagsLineCap.butt;
529531
} else {
530-
v1.flags = flagsLineJoin[lineJoinVal === undefined ? lineJoinFunc(lineItemData, lidx, lineItem, i) : lineJoinVal] || flagsLineJoin.miter;
532+
v1.flags = flagsLineJoin[lineJoinVal === undefined ? lineJoinFunc(lineItemData, lidx, d, i) : lineJoinVal] || flagsLineJoin.miter;
531533
}
532534
}
533535

tutorials/lines/index.pug

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
extends ../common/index.pug
2+
3+
block mainTutorial
4+
:markdown-it
5+
# Tutorial - Lines
6+
Add a line feature to a geospatial context. Lines can vary many
7+
properties, including stroke width, color, opacity, and offset, line join
8+
and cap, and closure.
9+
10+
First, create a map and a feature layer.
11+
12+
+codeblock('javascript', 1).
13+
var map = geo.map({
14+
node: "#map",
15+
center: {x: 25, y: 20},
16+
zoom: 5
17+
});
18+
// create a layer that supports drawing line features
19+
var layer = map.createLayer('feature', {features: ['line']});
20+
21+
:markdown-it
22+
Specify some data. A line feature is an array of data elements, each of
23+
which has a list of vertices.
24+
25+
+codeblock('javascript', 2).
26+
var lineData = [
27+
[{x: 20, y: 10}, {x: 25, y: 10}],
28+
[{x: 30, y: 10}, {x: 35, y: 12}, {x: 32, y: 15}],
29+
[{x: 30, y: 20}, {x: 35, y: 22}, {x: 32, y: 25}],
30+
[{x: 30, y: 30}, {x: 35, y: 32}, {x: 32, y: 35}, {x: 30, y: 30}],
31+
[{x: 20, y: 20}, {x: 22, y: 20}, {x: 24, y: 20}, {x: 26, y: 20}],
32+
[{x: 20, y: 30}, {x: 25, y: 32}, {x: 22, y: 35}, {x: 20, y: 35}]
33+
];
34+
35+
:markdown-it
36+
Create the line feature and set some styles on it.
37+
38+
+codeblock('javascript', 3).
39+
line = layer.createFeature('line')
40+
// set the data to our example data
41+
.data(lineData)
42+
// add some styles
43+
.style({
44+
// use a function for determining if the line should be closed based
45+
// on where it is in our list of data.
46+
closed: function (item, itemIndex) {
47+
return itemIndex === 1 || itemIndex === 3 || itemIndex === 5;
48+
},
49+
strokeColor: 'black',
50+
strokeWidth: 2
51+
});
52+
// draw the feature
53+
line.draw();
54+
+codeblock_test('map has a feature layer with six lines', [
55+
'map.layers().length === 1',
56+
'map.layers()[0] instanceof geo.featureLayer',
57+
'map.layers()[0].features()[0] instanceof geo.lineFeature',
58+
'map.layers()[0].features()[0].data().length === 6'
59+
])
60+
61+
:markdown-it
62+
Specify some more complex data. Our data elements are now objects with
63+
various properties, which will require ``line`` and ``position`` functions
64+
for the line feature to access.
65+
66+
+codeblock('javascript', 4, 1, false, 'Step 2-B').
67+
var lineData = [{
68+
coord: [[20, 10], [25, 10]]
69+
}, {
70+
coord: [[30, 10], [35, 12], [32, 15]],
71+
closed: true
72+
}, {
73+
coord: [[30, 20], [35, 22], [32, 25]],
74+
strokeColor: 'blue',
75+
strokeWidth: 5
76+
}, {
77+
coord: [[30, 30], [35, 32], [32, 35], [30, 30]],
78+
closed: true
79+
}, {
80+
coord: [
81+
{x: 20, y: 20, width: 20},
82+
{x: 22, y: 20, width: 5},
83+
{x: 24, y: 20, width: 2},
84+
{x: 26, y: 20, width: 2}
85+
]
86+
}, {
87+
coord: [[20, 30], [25, 32], [22, 35], [20, 35]],
88+
strokeWidth: 10,
89+
lineJoin: 'bevel',
90+
closed: true
91+
}];
92+
93+
:markdown-it
94+
Create the line feature and specify functions to access the data and set
95+
styles. Our style functions are more complex, as they get information from
96+
the line data items.
97+
98+
+codeblock('javascript', 5, undefined, true, 'Step 3-B').
99+
line = layer.createFeature('line')
100+
// set the data to our example data
101+
.data(lineData)
102+
// get the vertex list from the coord element of the line item
103+
.line(function (item, itemIdx) {
104+
return item.coord;
105+
})
106+
// the position can be an object or an array of [x, y]
107+
.position(function (vertex, vertexIndex, item, itemIndex) {
108+
return vertex.x === undefined ? {x: vertex[0], y: vertex[1]} : vertex;
109+
})
110+
// for styles, use the value specified by the line item if present
111+
.style({
112+
closed: function (item, itemIndex) {
113+
return item.closed;
114+
},
115+
lineJoin: function (vertex, vertexIndex, item, itemIndex) {
116+
return item.lineJoin;
117+
},
118+
strokeColor: function (vertex, vertexIndex, item, itemIndex) {
119+
return item.strokeColor === undefined ? 'black' : item.strokeColor;
120+
},
121+
// stroke width might change by vertex
122+
strokeWidth: function (vertex, vertexIndex, item, itemIndex) {
123+
return vertex.width === undefined ? (
124+
item.strokeWidth === undefined ? 1 : item.strokeWidth) : vertex.width;
125+
}
126+
});
127+
// draw the feature
128+
line.draw();
129+
+codeblock_test('map has a feature layer with six lines', [
130+
'map.layers().length === 1',
131+
'map.layers()[0] instanceof geo.featureLayer',
132+
'map.layers()[0].features()[0] instanceof geo.lineFeature',
133+
'map.layers()[0].features()[0].data().length === 6'
134+
])

tutorials/lines/thumb.jpg

72.3 KB
Loading

tutorials/lines/tutorial.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"title": "Lines",
3+
"hideNavbar": true,
4+
"level": 0,
5+
"about": {
6+
"text": "Place lines with different styles in a geospatial context."
7+
}
8+
}

0 commit comments

Comments
 (0)