Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bd6f5b0

Browse files
author
Pete Goodman
committed
Created a proto 'creature3' that contains the basics of creating speech bubbles
1 parent 459a2e1 commit bd6f5b0

File tree

1 file changed

+205
-0
lines changed

1 file changed

+205
-0
lines changed

javascript/creature3.js

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/*
2+
* Async JavaScript Jungle
3+
*
4+
* Allowing a creature to create speech bubbles
5+
*
6+
* creature: *****
7+
* Code: ******
8+
* Design: *****
9+
*
10+
* Public methods:
11+
* - *****
12+
*/
13+
jj.createCreature('___creatureName', function (_creature) {
14+
15+
// internal vars
16+
var width = 100,
17+
height = 50,
18+
left = 0,
19+
top = 0,
20+
isTalking = false,
21+
canAnimate = false,
22+
animationstring = 'animation',
23+
worldSize,
24+
browser,
25+
keyframeprefix,
26+
$speechBubble,
27+
$speechBubbleArrow,
28+
$creature,
29+
creature,
30+
transitionEndFunc,
31+
speechQueue = [];
32+
33+
// get creature element
34+
$creature = _creature.el;
35+
creature = $creature[0];
36+
37+
38+
// set initial size & position of creature here!
39+
worldSize = jj.size();
40+
top = (worldSize.height-height)/2;
41+
left = 100;
42+
_creature.size({width: width, height: height});
43+
_creature.position({top: top, left: left});
44+
45+
46+
// make your creature look nice here!
47+
$creature.css({background:"#f90"});
48+
49+
50+
// add creature data
51+
_creature.data({
52+
kingdom: "",
53+
phylum: "",
54+
"class": "",
55+
order: "",
56+
family: "",
57+
genus: "",
58+
species: "",
59+
sight: 30,
60+
hearing: 60,
61+
smell: 90
62+
});
63+
64+
65+
// on frame
66+
jj.bind('tick', function (frame) {
67+
// nothing to see here
68+
});
69+
70+
71+
// on time
72+
jj.bind('clock', function (hour, min) {
73+
74+
if (min % 15 == 0) {
75+
speechQueue.push("your words here");
76+
speak();
77+
}
78+
79+
});
80+
81+
82+
83+
84+
85+
86+
87+
// private, only you can explicitly tell your creature to speak
88+
var speak = function() {
89+
90+
var $arrow, $speech, speech, left, top, sentence, animEnd;
91+
92+
// get latest sentence
93+
sentence = speechQueue.pop();
94+
if (!sentence) { return; }
95+
96+
isTalking = true;
97+
98+
// create roar
99+
$arrow = $speechBubbleArrow.clone();
100+
$speech = $speechBubble
101+
.clone()
102+
.html(sentence)
103+
.append($arrow)
104+
.appendTo($creature);
105+
106+
speech = $speech[0];
107+
108+
// position bubble after append - centre based on length
109+
left = (width/2) - ($speech.outerWidth() / 2) + "px";
110+
top = -($speech.outerHeight() + 10) + "px";
111+
$speech.css({top:top, left:left});
112+
113+
// animate: scroll up, fade in, pause, scroll up, fade out, remove el
114+
speech.style[ animationstring ] = 'speech-bubble 1.5s linear 1';
115+
116+
// fix end animation type
117+
animEnd = (browser === "moz") ? "animationend" : browser+"AnimationEnd";
118+
119+
// after word, do another?
120+
speech.addEventListener(animEnd, function(){
121+
$speech.remove();
122+
isTalking = false;
123+
124+
// load next words?
125+
if (speechQueue.length > 0) {
126+
speak();
127+
} else {
128+
isTalking = false;
129+
}
130+
}, false);
131+
};
132+
133+
134+
135+
// cross-browser compatible preparation for CSS anims
136+
// see: http://hacks.mozilla.org/2011/09/detecting-and-generating-css-animations-in-javascript/
137+
var _checkForCSSAnimation = function() {
138+
var domPrefixes = 'Webkit Moz O ms Khtml'.split(' '),
139+
pfx = '';
140+
141+
if( creature.style.animationName ) { canAnimate = true; }
142+
143+
if( canAnimate === false ) {
144+
for( var i = 0; i < domPrefixes.length-1; i++ ) {
145+
if( creature.style[ domPrefixes[i] + 'AnimationName' ] !== undefined ) {
146+
pfx = domPrefixes[ i ];
147+
browser = pfx.toLowerCase();
148+
animationstring = pfx + 'Animation';
149+
keyframeprefix = '-' + pfx.toLowerCase() + '-';
150+
canAnimate = true;
151+
break;
152+
}
153+
}
154+
}
155+
}();
156+
157+
158+
// prep speech bubbles
159+
var _prepareSpeechBubbles = function() {
160+
$speechBubble = jj.jQuery("<div/>")
161+
.attr("class", "speech-bubble")
162+
.css({
163+
"position":"absolute",
164+
"background":"#fff",
165+
"minWidth":width/2,
166+
"maxWidth":width*2,
167+
"padding":"10px",
168+
"textAlign":"center",
169+
"borderRadius":"20px",
170+
"opacity":0,
171+
"zIndex":1
172+
});
173+
174+
$speechBubbleArrow = jj.jQuery("<div/>")
175+
.attr("class", "speech-bubble-arrow")
176+
.css({
177+
"position":"absolute",
178+
"bottom":"-10px",
179+
"left":"50%",
180+
"marginLeft":"-10px",
181+
"border": "10px solid transparent",
182+
"borderTopColor": "#fff",
183+
"borderBottom": "none",
184+
"zIndex":2
185+
});
186+
187+
var keyframes = '@' + keyframeprefix + 'keyframes speech-bubble { '+
188+
'0% { opacity:0; margin-top:20px; }'+
189+
'10% { opacity:1; margin-top:0; }'+
190+
'90% { opacity:1; margin-top:0; }'+
191+
'100% { opacity:0; margin-top:-20px }'+
192+
'}';
193+
194+
// append styles
195+
if( document.styleSheets && document.styleSheets.length ) {
196+
document.styleSheets[0].insertRule( keyframes, 0 );
197+
} else {
198+
var s = document.createElement( 'style' );
199+
s.innerHTML = keyframes;
200+
document.getElementsByTagName( 'head' )[ 0 ].appendChild( s );
201+
}
202+
}();
203+
204+
205+
});

0 commit comments

Comments
 (0)