Skip to content

Commit 8cce8e9

Browse files
author
daniel-lundin
committed
Fixed bug that caused multiple rAF to be queued, update code style of demos
1 parent 95ca9c7 commit 8cce8e9

File tree

5 files changed

+164
-133
lines changed

5 files changed

+164
-133
lines changed

docs/cards.js

Lines changed: 92 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
1-
1+
/* global document, randomColor, snabbt, FastClick */
22
// Constants
3+
'use strict';
4+
35
var CARD_HEIGHT = 100;
46
var CARD_WIDTH = 60;
57
var CARD_COUNT = 40;
68

79
var WIDTH = 800;
810
var HEIGHT = 600;
9-
var BOTTOM = 400;
1011

11-
var TILT = Math.PI/8;
12-
var PYTH_ANGLE = Math.PI/2 - TILT;
12+
var TILT = Math.PI / 8;
13+
var PYTH_ANGLE = Math.PI / 2 - TILT;
1314

1415
var TILTED_CARD_HEIGHT = Math.sin(PYTH_ANGLE) * CARD_HEIGHT + 2;
1516
var TILTED_CARD_WIDTH = Math.cos(PYTH_ANGLE) * CARD_HEIGHT;
1617
var CARD_SPACING = 2;
1718
var PYRAMID_WIDTH = TILTED_CARD_WIDTH * 2 + CARD_SPACING * 2;
1819

19-
function update_sizes() {
20-
var c = document.getElementById('container');
21-
WIDTH = c.clientWidth;
22-
HEIGHT = c.clientHeight;
20+
function updateSizes() {
21+
var container = document.getElementById('container');
22+
WIDTH = container.clientWidth;
23+
HEIGHT = container.clientHeight;
2324
CARD_WIDTH = WIDTH * 0.05;
2425
CARD_HEIGHT = HEIGHT * 0.15;
2526
TILTED_CARD_HEIGHT = Math.sin(PYTH_ANGLE) * CARD_HEIGHT + 2;
2627
TILTED_CARD_WIDTH = Math.cos(PYTH_ANGLE) * CARD_HEIGHT;
2728
PYRAMID_WIDTH = TILTED_CARD_WIDTH * 2 + CARD_SPACING * 2;
28-
for(var i=0;i<Deck.cards.length;++i) {
29-
Deck.card_at(i).style.height = CARD_HEIGHT + 'px';
30-
Deck.card_at(i).style.width = CARD_WIDTH + 'px';
29+
for (var i = 0; i < Deck.length(); ++i) {
30+
Deck.cardAt(i).style.height = CARD_HEIGHT + 'px';
31+
Deck.cardAt(i).style.width = CARD_WIDTH + 'px';
3132
}
3233
}
3334

3435
var COLORS = randomColor({
3536
count: 40,
36-
luminosity: 'dark',
37+
luminosity: 'dark'
3738
});
3839

3940

@@ -44,13 +45,13 @@ var WALL = 3;
4445
var CYLINDER = 4;
4546
var current_mode;
4647

47-
var formation_builders = {};
48-
formation_builders[PILE] = pile_positions;
49-
formation_builders[HOUSE] = house_positions;
50-
formation_builders[WALL] = wall_positions;
51-
formation_builders[CYLINDER] = cylinder_positions;
48+
var formationBuilders = {};
49+
formationBuilders[PILE] = pile_positions;
50+
formationBuilders[HOUSE] = house_positions;
51+
formationBuilders[WALL] = wall_positions;
52+
formationBuilders[CYLINDER] = cylinder_positions;
5253

53-
function create_card(container, index) {
54+
function createCard(container, index) {
5455
var card = document.createElement('div');
5556
card.className = 'card';
5657
card.style.background = COLORS[index % COLORS.length];
@@ -61,34 +62,36 @@ function create_card(container, index) {
6162

6263
// Deck
6364
var Deck = (function() {
64-
this.cards = [];
65-
this.card_index = [];
65+
var cards = [];
66+
var cardIndex = 0;
6667

67-
for(var i=0;i<CARD_COUNT;++i) {
68+
for (var i = 0; i < CARD_COUNT; ++i) {
6869
var container = document.getElementById('surface');
69-
this.cards.push(create_card(container, i));
70+
cards.push(createCard(container, i));
7071
}
7172

72-
this.next_card = function() {
73-
if(this.card_index > 51)
74-
return;
75-
return this.cards[this.card_index++];
76-
};
77-
78-
this.card_at = function(index) {
79-
return this.cards[index];
80-
};
81-
82-
this.reset = function() {
83-
this.card_index = 0;
84-
};
85-
return this;
73+
return {
74+
nextCard: function() {
75+
if (cardIndex > 51)
76+
return;
77+
return cards[cardIndex++];
78+
},
79+
cardAt: function(index) {
80+
return cards[index];
81+
},
82+
reset: function() {
83+
cardIndex = 0;
84+
},
85+
length: function() {
86+
return cards.length;
87+
}
88+
}
8689
})();
8790

8891
function build_formation(positions) {
8992
Deck.reset();
90-
for(i=0;i<positions.length;++i) {
91-
snabbt(Deck.next_card(), {
93+
for (var i = 0; i < positions.length; ++i) {
94+
snabbt(Deck.nextCard(), {
9295
position: positions[i].position,
9396
rotation: positions[i].rotation,
9497
easing: 'ease',
@@ -97,43 +100,47 @@ function build_formation(positions) {
97100
}
98101
}
99102

100-
function set_mode(mode) {
101-
update_sizes();
102-
if(mode == current_mode) {
103+
function setMode(mode) {
104+
updateSizes();
105+
if (mode === current_mode) {
103106
return;
104107
}
105108

106-
positions = formation_builders[mode]();
109+
var positions = formationBuilders[mode]();
107110
build_formation(positions);
108111

109112
current_mode = mode;
110113
}
111114

112-
function rotate_container() {
115+
function rotateContainer() {
113116
var container = document.getElementById('surface');
114117
snabbt(container, {
115-
rotation: [0, 2*Math.PI, 0],
118+
fromRotation: [0, 0, 0],
119+
rotation: [0, 2 * Math.PI, 0],
116120
duration: 10000,
117121
perspective: 2000,
118-
loop: Infinity
122+
complete: function() {
123+
console.log('compoete');
124+
rotateContainer();
125+
}
119126
});
120127
}
121128

122129
function pile_positions() {
123130
Deck.reset();
124131
var positions = [];
125132

126-
var i=0;
127-
var card=Deck.next_card();
128-
var center = (WIDTH - CARD_WIDTH)/2;
129-
var y = HEIGHT - HEIGHT*0.2;
130-
while(card) {
133+
var i = 0;
134+
var card = Deck.nextCard();
135+
var center = (WIDTH - CARD_WIDTH) / 2;
136+
var y = HEIGHT - HEIGHT * 0.2;
137+
while (card) {
131138
positions.push({
132-
position: [center, y - i*0.5, WIDTH*0.1],
133-
rotation: [Math.PI/2, 0, 0],
139+
position: [center, y - i * 0.5, WIDTH * 0.1],
140+
rotation: [Math.PI / 2, 0, 0]
134141
});
135142
++i;
136-
card = Deck.next_card();
143+
card = Deck.nextCard();
137144
}
138145
return positions;
139146
}
@@ -142,13 +149,13 @@ function house_positions() {
142149
Deck.reset();
143150

144151
var floors = 5;
145-
var y = (floors - 1) * TILTED_CARD_HEIGHT + TILTED_CARD_HEIGHT/2;
152+
var y = (floors - 1) * TILTED_CARD_HEIGHT + TILTED_CARD_HEIGHT / 2;
146153
var z = -WIDTH * 0.2;
147154
var x = (WIDTH - PYRAMID_WIDTH * floors) / 2 - TILTED_CARD_WIDTH;
148155

149156
var positions = [];
150157
var i;
151-
for(i=0;i<floors;++i) {
158+
for (i = 0; i < floors; ++i) {
152159
var _x = x + i * TILTED_CARD_WIDTH + i * CARD_SPACING;
153160
var _y = y - i * TILTED_CARD_HEIGHT - i * CARD_SPACING;
154161
positions = positions.concat(house_row_positions(floors - i, _x, _y, z));
@@ -161,37 +168,37 @@ function house_row_positions(count, x, y, z) {
161168
var positions = [];
162169
var i;
163170
// Tilted cards
164-
for(i=0;i<count;++i) {
165-
card_positions = pyramid_postions(x + i*PYRAMID_WIDTH, y, z);
171+
for (i = 0; i < count; ++i) {
172+
var cardPositions = pyramid_postions(x + i * PYRAMID_WIDTH, y, z);
166173
positions.push({
167-
position: card_positions[0].position,
168-
rotation: card_positions[0].rotation,
174+
position: cardPositions[0].position,
175+
rotation: cardPositions[0].rotation
169176
});
170177
positions.push({
171-
position: card_positions[1].position,
172-
rotation: card_positions[1].rotation,
178+
position: cardPositions[1].position,
179+
rotation: cardPositions[1].rotation
173180
});
174181
}
175182
// Bridge cards
176-
for(i=0;i<count-1;++i) {
183+
for (i = 0; i < count - 1; ++i) {
177184
positions.push({
178-
position: [x + i*PYRAMID_WIDTH + TILTED_CARD_WIDTH, y - TILTED_CARD_HEIGHT/2 - CARD_SPACING/2, z],
179-
rotation: [Math.PI/2, Math.PI/2, 0],
185+
position: [x + i * PYRAMID_WIDTH + TILTED_CARD_WIDTH, y - TILTED_CARD_HEIGHT / 2 - CARD_SPACING / 2, z],
186+
rotation: [Math.PI / 2, Math.PI / 2, 0]
180187
});
181188
}
182189
return positions;
183190
}
184191

185192
function pyramid_postions(x, y, z) {
186193
// Firefox flickers if elements overlap
187-
var spacing = (TILTED_CARD_WIDTH / 2) + CARD_SPACING/2;
194+
var spacing = TILTED_CARD_WIDTH / 2 + CARD_SPACING / 2;
188195

189196
return [{
190197
position: [x - spacing, y, z],
191-
rotation: [-TILT, Math.PI/2, 0],
198+
rotation: [-TILT, Math.PI / 2, 0]
192199
}, {
193200
position: [x + spacing, y, z],
194-
rotation: [TILT, Math.PI/2, 0],
201+
rotation: [TILT, Math.PI / 2, 0]
195202
}];
196203
}
197204

@@ -201,9 +208,9 @@ function wall_positions() {
201208
var h = CARD_HEIGHT + 10;
202209
var start_x = (WIDTH - 10 * w) / 2;
203210
var start_y = (HEIGHT - 4 * h) / 2;
204-
for(var i=0;i<CARD_COUNT;++i) {
205-
var x = (i % 10) * w + start_x;
206-
var y = (Math.floor(i/10)) * h + start_y;
211+
for (var i = 0; i < CARD_COUNT; ++i) {
212+
var x = i % 10 * w + start_x;
213+
var y = Math.floor(i / 10) * h + start_y;
207214
positions.push({
208215
position: [x, y, 0],
209216
rotation: [0, 0, 0]
@@ -216,47 +223,35 @@ function cylinder_positions() {
216223
var positions = [];
217224
var start_x = WIDTH / 2;
218225
var start_y = HEIGHT * 0.1;
219-
var radius = WIDTH*0.2;
220-
for(var i=0;i<CARD_COUNT;++i) {
221-
var angle = ((i % 10) / 10) * 2 * Math.PI;
226+
var radius = WIDTH * 0.2;
227+
for (var i = 0; i < CARD_COUNT; ++i) {
228+
var angle = i % 10 / 10 * 2 * Math.PI;
222229
var x = Math.cos(angle) * radius + start_x;
223230
var z = Math.sin(angle) * radius;
224231
var y = Math.floor(i / 10) * 1.2 * CARD_HEIGHT + start_y;
225232
positions.push({
226233
position: [x, y, z],
227-
rotation: [0, Math.PI/2 + angle, 0],
234+
rotation: [0, Math.PI / 2 + angle, 0]
228235
});
229236
}
230237
return positions;
231238
}
232239

233-
function build_wall() {
234-
set_mode(WALL);
235-
}
236-
237-
function build_house() {
238-
set_mode(HOUSE);
239-
}
240-
241-
function build_pile() {
242-
set_mode(PILE);
243-
}
244-
245240
function init() {
246-
update_sizes();
241+
updateSizes();
247242
Deck.reset();
248-
build_pile();
249-
rotate_container();
243+
setMode(PILE);
244+
rotateContainer();
250245

251246
// Initialize fast click
252247
FastClick.attach(document.body);
253248

254249
// Event handlers
255250
var buttons = {
256-
"pile_button": PILE,
257-
"house_button": HOUSE,
258-
"wall_button": WALL,
259-
"cylinder_button": CYLINDER
251+
'pile_button': PILE,
252+
'house_button': HOUSE,
253+
'wall_button': WALL,
254+
'cylinder_button': CYLINDER
260255
};
261256

262257
var click_handler = function(key) {
@@ -265,10 +260,10 @@ function init() {
265260
document.getElementById('wall_button').className = '';
266261
document.getElementById('cylinder_button').className = '';
267262
document.getElementById(key).className = 'button-primary';
268-
set_mode(buttons[key]);
263+
setMode(buttons[key]);
269264
};
270265

271-
for(var key in buttons) {
272-
document.getElementById(key).addEventListener('click', click_handler.bind(undefined, key));
273-
}
266+
Object.keys(buttons).forEach(function(key) {
267+
document.getElementById(key).addEventListener('click', click_handler.bind(null, key));
268+
});
274269
}

docs/sticks.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
<style>
2222
body {
23-
-webkit-perspective: 2000px;
2423
overflow:hidden;
2524
}
2625
h1 {

0 commit comments

Comments
 (0)