Skip to content

Commit

Permalink
Merge pull request #62 from binh-dam-ibigroup/transitive-labels
Browse files Browse the repository at this point in the history
Rendering tweaks
  • Loading branch information
landonreed authored Apr 6, 2021
2 parents 468ec01 + 448ad95 commit 450fb2a
Show file tree
Hide file tree
Showing 9 changed files with 453 additions and 13 deletions.
6 changes: 5 additions & 1 deletion lib/display/canvas-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ export default class CanvasDisplay extends Display {
// Apply linecap style
this.ctx.lineCap = attrs['stroke-linecap'] || 'butt'

// Apply 'round' line join for smooth edge rendering.
// (The default join is 'miter', which causes jagged edges when zoomed away.)
this.ctx.lineJoin = 'round'

// Draw the path
this.ctx.beginPath()
this.ctx.moveTo(renderData[0].x, renderData[0].y)
Expand All @@ -111,7 +115,7 @@ export default class CanvasDisplay extends Display {
// For equivalence w/ SVG text rendering
this.ctx.textBaseline = 'top'

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

if (attrs['stroke']) {
Expand Down
4 changes: 3 additions & 1 deletion lib/labeler/labeler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import measureText from 'measure-text'
import d3 from 'd3' // TODO: replace w/ other quadtree library

import SegmentLabel from './segmentlabel'
import { getFontSizeWithUnit } from '../util'

/**
* Labeler object
Expand Down Expand Up @@ -320,7 +321,8 @@ export default class Labeler {

const textDimensions = measureText({
text: labelText,
fontSize: label.fontSize + 'px',
// Append 'px' if a unit was not specified in font-size.
fontSize: getFontSizeWithUnit(label.fontSize),
fontFamily: label.fontFamily || 'sans-serif',
lineHeight: 1.2
})
Expand Down
4 changes: 3 additions & 1 deletion lib/labeler/pointlabel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Label from './label'
import { getFontSizeWithUnit } from '../util'

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

Expand Down
7 changes: 6 additions & 1 deletion lib/labeler/segmentlabel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Label from './label'
import { getFontSizeWithUnit } from '../util'

/**
* SegmentLabel object
Expand Down Expand Up @@ -28,6 +29,8 @@ export default class SegmentLabel extends Label {
rx: this.containerHeight / 2,
ry: this.containerHeight / 2
})

const fontSize = display.styler.compute2('segment_labels', 'font-size', this.parent)
// Offset text location by padding
display.drawText(text, {
x: x + this.getPadding(),
Expand All @@ -36,7 +39,9 @@ export default class SegmentLabel extends Label {
}, {
// text color
fill: display.styler.compute2('segment_labels', 'color', this.parent),
'font-size': this.fontSize
'font-family': display.styler.compute2('segment_labels', 'font-family', this.parent),
// Append 'px' if a unit was not specified in font-size.
'font-size': getFontSizeWithUnit(fontSize)
})
}

Expand Down
11 changes: 10 additions & 1 deletion lib/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ function renderDataToSvgPath (renderData) {
// An instance of the SphericalMercator converter
const sm = new SphericalMercator()

/**
* @param {*} fontSize A CSS font size or a numerical (pixel) font size.
* @returns A CSS font size ending with the provided CSS unit or 'px' if none provided.
*/
function getFontSizeWithUnit (fontSize) {
return fontSize + (isFinite(fontSize) ? 'px' : '')
}

export {
fuzzyEquals,
distance,
Expand All @@ -212,5 +220,6 @@ export {
addVectors,
otpModeToGtfsType,
renderDataToSvgPath,
sm
sm,
getFontSizeWithUnit
}
17 changes: 17 additions & 0 deletions stories/Transitive.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ Itinerary.args = {
styles: undefined
}

export const ItineraryWithAlternativeStyling = Template.bind({})
ItineraryWithAlternativeStyling.args = {
center: [28.5459257, -81.3467216],
companies,
itinerary: require('./data/fdot-itin.json'),
styles: {
labels: {
"font-size": "18px",
"font-family": "serif"
},
segment_labels: {
color: "#FFDD00",
"font-size": "24px"
}
}
}

export const Profile = Template.bind({})
Profile.args = {
center: [38.885, -77.0369],
Expand Down
1 change: 1 addition & 0 deletions stories/TransitiveMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const TransitiveMap = ({
zoom={zoom}
>
<TransitiveOverlay
styles={styles}
transitiveData={transitiveData}
visible
/>
Expand Down
Loading

0 comments on commit 450fb2a

Please sign in to comment.