-
Notifications
You must be signed in to change notification settings - Fork 919
/
Page.js
129 lines (110 loc) · 3.73 KB
/
Page.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
'use strict';
var React = require('react');
var ReactCanvas = require('react-canvas');
var Group = ReactCanvas.Group;
var Image = ReactCanvas.Image;
var Text = ReactCanvas.Text;
var FontFace = ReactCanvas.FontFace;
var measureText = ReactCanvas.measureText;
var CONTENT_INSET = 14;
var TEXT_SCROLL_SPEED_MULTIPLIER = 0.6;
var TEXT_ALPHA_SPEED_OUT_MULTIPLIER = 1.25;
var TEXT_ALPHA_SPEED_IN_MULTIPLIER = 2.6;
var IMAGE_LAYER_INDEX = 2;
var TEXT_LAYER_INDEX = 1;
var Page = React.createClass({
propTypes: {
width: React.PropTypes.number.isRequired,
height: React.PropTypes.number.isRequired,
article: React.PropTypes.object.isRequired,
scrollTop: React.PropTypes.number.isRequired
},
componentWillMount: function () {
// Pre-compute headline/excerpt text dimensions.
var article = this.props.article;
var maxWidth = this.props.width - 2 * CONTENT_INSET;
var titleStyle = this.getTitleStyle();
var excerptStyle = this.getExcerptStyle();
this.titleMetrics = measureText(article.title, maxWidth, titleStyle.fontFace, titleStyle.fontSize, titleStyle.lineHeight);
this.excerptMetrics = measureText(article.excerpt, maxWidth, excerptStyle.fontFace, excerptStyle.fontSize, excerptStyle.lineHeight);
},
render: function () {
var groupStyle = this.getGroupStyle();
var imageStyle = this.getImageStyle();
var titleStyle = this.getTitleStyle();
var excerptStyle = this.getExcerptStyle();
// Layout title and excerpt below image.
titleStyle.height = this.titleMetrics.height;
excerptStyle.top = titleStyle.top + titleStyle.height + CONTENT_INSET;
excerptStyle.height = this.props.height - excerptStyle.top - CONTENT_INSET;
return (
<Group style={groupStyle}>
<Image style={imageStyle} src={this.props.article.imageUrl} fadeIn={true} useBackingStore={true} />
<Group style={this.getTextGroupStyle()} useBackingStore={true}>
<Text style={titleStyle}>{this.props.article.title}</Text>
<Text style={excerptStyle}>{this.props.article.excerpt}</Text>
</Group>
</Group>
);
},
// Styles
// ======
getGroupStyle: function () {
return {
top: 0,
left: 0,
width: this.props.width,
height: this.props.height,
};
},
getImageHeight: function () {
return Math.round(this.props.height * 0.5);
},
getImageStyle: function () {
return {
top: 0,
left: 0,
width: this.props.width,
height: this.getImageHeight(),
backgroundColor: '#eee',
zIndex: IMAGE_LAYER_INDEX
};
},
getTitleStyle: function () {
return {
top: this.getImageHeight() + CONTENT_INSET,
left: CONTENT_INSET,
width: this.props.width - 2 * CONTENT_INSET,
fontSize: 22,
lineHeight: 30,
fontFace: FontFace('Avenir Next Condensed, Helvetica, sans-serif', null, {weight: 500})
};
},
getExcerptStyle: function () {
return {
left: CONTENT_INSET,
width: this.props.width - 2 * CONTENT_INSET,
fontFace: FontFace('Georgia, serif'),
fontSize: 15,
lineHeight: 23
};
},
getTextGroupStyle: function () {
var imageHeight = this.getImageHeight();
var translateY = 0;
var alphaMultiplier = (this.props.scrollTop <= 0) ? -TEXT_ALPHA_SPEED_OUT_MULTIPLIER : TEXT_ALPHA_SPEED_IN_MULTIPLIER;
var alpha = 1 - (this.props.scrollTop / this.props.height) * alphaMultiplier;
alpha = Math.min(Math.max(alpha, 0), 1);
translateY = -this.props.scrollTop * TEXT_SCROLL_SPEED_MULTIPLIER;
return {
width: this.props.width,
height: this.props.height - imageHeight,
top: imageHeight,
left: 0,
alpha: alpha,
translateY: translateY,
zIndex: TEXT_LAYER_INDEX
};
}
});
module.exports = Page;