Skip to content

Commit 450fb2a

Browse files
authored
Merge pull request #62 from binh-dam-ibigroup/transitive-labels
Rendering tweaks
2 parents 468ec01 + 448ad95 commit 450fb2a

File tree

9 files changed

+453
-13
lines changed

9 files changed

+453
-13
lines changed

Diff for: lib/display/canvas-display.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ export default class CanvasDisplay extends Display {
9393
// Apply linecap style
9494
this.ctx.lineCap = attrs['stroke-linecap'] || 'butt'
9595

96+
// Apply 'round' line join for smooth edge rendering.
97+
// (The default join is 'miter', which causes jagged edges when zoomed away.)
98+
this.ctx.lineJoin = 'round'
99+
96100
// Draw the path
97101
this.ctx.beginPath()
98102
this.ctx.moveTo(renderData[0].x, renderData[0].y)
@@ -111,7 +115,7 @@ export default class CanvasDisplay extends Display {
111115
// For equivalence w/ SVG text rendering
112116
this.ctx.textBaseline = 'top'
113117

114-
this.ctx.font = `${attrs.fontSize || '14px'} ${attrs.fontFamily || 'sans-serif'}`
118+
this.ctx.font = `${attrs['font-size'] || '14px'} ${attrs['font-family'] || 'sans-serif'}`
115119
if (attrs['text-anchor']) this.ctx.textAlign = attrs['text-anchor']
116120

117121
if (attrs['stroke']) {

Diff for: lib/labeler/labeler.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import measureText from 'measure-text'
33
import d3 from 'd3' // TODO: replace w/ other quadtree library
44

55
import SegmentLabel from './segmentlabel'
6+
import { getFontSizeWithUnit } from '../util'
67

78
/**
89
* Labeler object
@@ -320,7 +321,8 @@ export default class Labeler {
320321

321322
const textDimensions = measureText({
322323
text: labelText,
323-
fontSize: label.fontSize + 'px',
324+
// Append 'px' if a unit was not specified in font-size.
325+
fontSize: getFontSizeWithUnit(label.fontSize),
324326
fontFamily: label.fontFamily || 'sans-serif',
325327
lineHeight: 1.2
326328
})

Diff for: lib/labeler/pointlabel.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Label from './label'
2+
import { getFontSizeWithUnit } from '../util'
23

34
/**
45
* Label object
@@ -68,7 +69,8 @@ export default class PointLabel extends Label {
6869
// define common style attributes for the halo and main text
6970
const attrs = {
7071
'fill': '#000',
71-
'font-size': this.fontSize,
72+
'font-family': display.styler.compute2('labels', 'font-family', this.parent),
73+
'font-size': getFontSizeWithUnit(this.fontSize),
7274
'text-anchor': this.labelPosition > 0 ? 'start' : 'end'
7375
}
7476

Diff for: lib/labeler/segmentlabel.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Label from './label'
2+
import { getFontSizeWithUnit } from '../util'
23

34
/**
45
* SegmentLabel object
@@ -28,6 +29,8 @@ export default class SegmentLabel extends Label {
2829
rx: this.containerHeight / 2,
2930
ry: this.containerHeight / 2
3031
})
32+
33+
const fontSize = display.styler.compute2('segment_labels', 'font-size', this.parent)
3134
// Offset text location by padding
3235
display.drawText(text, {
3336
x: x + this.getPadding(),
@@ -36,7 +39,9 @@ export default class SegmentLabel extends Label {
3639
}, {
3740
// text color
3841
fill: display.styler.compute2('segment_labels', 'color', this.parent),
39-
'font-size': this.fontSize
42+
'font-family': display.styler.compute2('segment_labels', 'font-family', this.parent),
43+
// Append 'px' if a unit was not specified in font-size.
44+
'font-size': getFontSizeWithUnit(fontSize)
4045
})
4146
}
4247

Diff for: lib/util/index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ function renderDataToSvgPath (renderData) {
193193
// An instance of the SphericalMercator converter
194194
const sm = new SphericalMercator()
195195

196+
/**
197+
* @param {*} fontSize A CSS font size or a numerical (pixel) font size.
198+
* @returns A CSS font size ending with the provided CSS unit or 'px' if none provided.
199+
*/
200+
function getFontSizeWithUnit (fontSize) {
201+
return fontSize + (isFinite(fontSize) ? 'px' : '')
202+
}
203+
196204
export {
197205
fuzzyEquals,
198206
distance,
@@ -212,5 +220,6 @@ export {
212220
addVectors,
213221
otpModeToGtfsType,
214222
renderDataToSvgPath,
215-
sm
223+
sm,
224+
getFontSizeWithUnit
216225
}

Diff for: stories/Transitive.stories.js

+17
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ Itinerary.args = {
2626
styles: undefined
2727
}
2828

29+
export const ItineraryWithAlternativeStyling = Template.bind({})
30+
ItineraryWithAlternativeStyling.args = {
31+
center: [28.5459257, -81.3467216],
32+
companies,
33+
itinerary: require('./data/fdot-itin.json'),
34+
styles: {
35+
labels: {
36+
"font-size": "18px",
37+
"font-family": "serif"
38+
},
39+
segment_labels: {
40+
color: "#FFDD00",
41+
"font-size": "24px"
42+
}
43+
}
44+
}
45+
2946
export const Profile = Template.bind({})
3047
Profile.args = {
3148
center: [38.885, -77.0369],

Diff for: stories/TransitiveMap.js

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export const TransitiveMap = ({
4343
zoom={zoom}
4444
>
4545
<TransitiveOverlay
46+
styles={styles}
4647
transitiveData={transitiveData}
4748
visible
4849
/>

0 commit comments

Comments
 (0)