forked from tparisi/WebGLBook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrssCanvasProgram.js
167 lines (141 loc) · 4.08 KB
/
rssCanvasProgram.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// RSSCanvasProgram - Draws something on a canvas
RSSCanvasProgram = function()
{
}
RSSCanvasProgram.prototype = new Object;
RSSCanvasProgram.prototype.init = function(param)
{
param = param || {};
this.url = param.url;
this.backgroundColor = param.backgroundColor || '#696969';
this.textColor = param.textColor || '#aa0000';
this.titleColor = param.titleColor || this.textColor;
this.headingColor = param.headingColor || this.textColor;
this.titlePointSize = param.titlePointSize || 20;
this.descriptionPointSize = param.descriptionPointSize || 14;
this.datePointSize = param.datePointSize || 10;
this.running = true;
this.items = [];
this.fetchRSS();
}
RSSCanvasProgram.prototype.setView = function(view)
{
this.view = view;
var texture = this.view.texture;
texture.wrapT = true;
}
RSSCanvasProgram.prototype.fetchRSS = function(d)
{
this.lastFetchTime = Date.now();
var that = this;
$.get('rssproxy.php?url=' + this.url, function(d) { that.handleRSSData(d) } );
}
RSSCanvasProgram.prototype.handleRSSData = function(d)
{
var that = this;
var itemsAdded = false;
//find each 'item' in the file and parse it
$(d).find('item').each(
function()
{
//name the current found item this for this particular loop run
var item = $(this);
that.addItem(item);
itemsAdded = true;
}
);
if (itemsAdded)
{
this.needsRedraw = true;
}
}
RSSCanvasProgram.prototype.addItem = function(item)
{
// grab the post title
var title = item.find('title').text();
// grab the post's URL
var link = item.find('link').text();
// next, the description
var description = item.find('description').text();
//don't forget the pubdate
var pubDate = item.find('pubDate').text();
this.items.push({ title : title, link : link, description : description, pubDate :
pubDate });
}
RSSCanvasProgram.prototype.run = function()
{
if (this.running)
{
var texture = this.view.texture;
texture.offset.y += 0.001;
if (this.needsRedraw)
{
this.draw();
this.needsRedraw = false;
}
}
var deltat = Date.now() - this.lastFetchTime;
if (deltat > RSSCanvasProgram.FETCH_INTERVAL)
{
this.fetchRSS();
}
}
RSSCanvasProgram.prototype.toggleRun = function()
{
this.running = !this.running;
}
RSSCanvasProgram.prototype.handleMouseUp = function(x, y)
{
this.toggleRun();
}
RSSCanvasProgram.prototype.start = function()
{
this.running = true;
}
RSSCanvasProgram.prototype.stop = function()
{
this.running = false;
}
RSSCanvasProgram.prototype.draw = function()
{
var context = this.view.context;
var canvas = this.view.canvas;
var texture = this.view.texture;
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = this.backgroundColor;
context.fillRect(0, 0, canvas.width, canvas.height);
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = this.titleColor;
context.font = "italic " + this.titlePointSize + "pt Arial";
var x = canvas.width / 2;
var y = RSSCanvasProgram.top_margin;
context.fillText("TOP STORIES (CNN)", x, y);
this.drawItems(context);
}
RSSCanvasProgram.prototype.drawItems = function(context)
{
context.textAlign = "left";
context.textBaseline = "middle";
var x = RSSCanvasProgram.left_margin ;
var y = RSSCanvasProgram.top_margin + this.titlePointSize + 10;
var i, len = this.items.length;
for (i = 0; i < len; i++)
{
var item = this.items[i];
context.font = "italic " + this.titlePointSize + "pt Arial";
context.fillStyle = this.headingColor;
context.fillText(item.title, x, y);
y += (this.titlePointSize + 6);
context.font = this.descriptionPointSize + "pt Arial Narrow";
context.fillStyle = this.textColor;
context.fillText(item.description, x, y);
y += (this.descriptionPointSize + 6);
context.font = this.datePointSize + "pt Arial Narrow";
context.fillText(item.pubDate, x, y);
y += (this.datePointSize + 10);
}
}
RSSCanvasProgram.left_margin = 10;
RSSCanvasProgram.top_margin = 60;
RSSCanvasProgram.FETCH_INTERVAL = 300 * 1000; // 300 seconds i.e. 5 mins