Animate Your Name

Preview

In this project, we'll write a program that animates your name. When you move your mouse over your name, bubbles will scatter away and then reassemble.

To create this project, we'll first learn JavaScript, a programming language. Then we'll apply what we've learned to write this program.

<!-- index.html -->
<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="http://s3.amazonaws.com/codecademy-content/courses/hour-of-code/js/alphabet.js"></script>
  </head>
  <body>
    <canvas id="myCanvas"></canvas>
    <script type="text/javascript" src="http://s3.amazonaws.com/codecademy-content/courses/hour-of-code/js/bubbles.js"></script>
    <script type="text/javascript" src="main.js"></script>
  </body>
</html>
<!-- main.js -->
var myName = "Codecademy";

var red = [0, 100, 63];
var orange = [40, 100, 60];
var green = [75, 100, 40];
var blue = [196, 77, 55];
var purple = [280, 50, 60];
var letterColors = [red, orange, green, blue, purple];

drawName(myName, letterColors);

if(10 < 3)
{
    bubbleShape = 'square';
}
else
{
    bubbleShape = 'circle';
}

bounceBubbles();

Put in your name

Before we get started, let's see what the animation looks like with your name. In line 1, replace "Codecademy" with your name.

What's your name?

In order to write a program that animates your name, we need to learn the programming language JavaScript.

Let's get started by getting to know each other. What's your name?

Discover the length

Very good! You just wrote a string.
A string can contain letters, numbers, spaces, and symbols. Strings are surrounded with quotes.

These are all strings:

"Ryan"
"4"
"What is your name?"

In our code, we're using document.write() simply to display the string of your name in the preview window. The important stuff is inside the parentheses, so let's just focus on that.

To discover the length of a string, write the string within quotes. Then write a period (full stop) and the word length like this:

”Ryan".length.

Numbers

Great job! Now, let's do some math. You can add, subtract, multiply, and divide numbers in JavaScript, like this:

Addition: 2 + 3
Subtraction: 6 - 3
Multiplication: 3 * 4
Division: 10 / 5

Booleans

Nice job! Next let's look at Boolean expressions. A Boolean expression is either true or false.

For example, comparing two numbers returns a true or false result:

23 > 10 is true
5 < 4 is false

What we'll do

In the previous section, we started learning the JavaScript programming language. So far we've used:

strings (e.g. "dogs go woof!")
numbers (e.g. 4, 10)
booleans (e.g. false, 5 > 4)

In this section, we'll write a program to animate your name. Move your mouse over "Codecademy" in the preview window. You're going to make this!

Variables

So far, we've been typing in strings, numbers, and booleans into the editor. To do more complex coding, we need a way to "save" these values. We can do this using variables. A variable stores a string, number, or boolean, and gives it a specific, case-sensitive name.

Examples:

var myName = "Beyonce";
var myAge = 32;
var isEven = true

Functions

Excellent! Your name is drawn as a collection of bubbles. How did this happen?

In line 1, you created a variable named myName in which you stored a string of your name
In line 5, a function named drawName() took your name string and drew it on the screen.
Hang on, what's a function? A function takes in an input, does something with it, and then returns an output. In our code, the input was your name, and the output was the picture of your name as a bunch of bubbles.

var myName = "Ademiya Lee";



drawName(myName, blue);

Arrays

Looking good! Your name is now drawn as a collection of blue bubbles. But wouldn't it be cooler if we could use more than one color?

Variables, like myName, can store numbers or strings. But so far, we've only been able to store one number or one string at a time. Good thing we have arrays. Arrays store lists of data.

Examples:

var names = ["Beyonce", "Jay Z", "Blue Ivy"];

var blue = [196, 77, 55];

Anytime you see data surrounded by [], it is an array.

In fact, computers can understand colors as an array of numbers. In lines 1-5, I've added five colors. They are simply variables storing arrays of 3 numbers. Let's use them now to make the bubbles in your name more colorful.

Change the bubble shape

Colorful! What did we just do?
We gave the function drawName() two inputs. One was your name. The other was an array of colors. The output was a multi-colored picture of your name.

Now let's change the shape of the bubbles from circles to squares. There's a variable called bubbleShape that lets you control the shape of the bubbles.

When bubbleShape = "square", the bubbles are shaped as squares
When bubbleShape = "circle", the bubbles are shaped as circles

Decisions, decisions

Great job! Next let's use booleans to decide whether a block of code should run. Take a look at this code:

if(10 < 3) {
    bubbleShape = "circle";
}

else {
    bubbleShape = "square";
}

Now imagine we have a robot that starts in line 1 of this code, and walks down it line by line. If the condition (in this case, 10 < 3) evaluates to true, then the robot runs the code inside the first pair of curly braces {}, which will make the bubbles circle-shaped.

Else the condition is false (which it is, in this case), so the robot skips the code in the first block of curly braces entirely, and runs the code in the second block. Therefore, the bubbles will be square-shaped.

This is called an if/else statement.

Bounce the name

Whew! Great work with if/else statements!

Let's make the bubbles in your name more interactive. It would be cool if you could mouse over your name and cause it to move in some way. We have a function called bounceName() that shakes your name around when a mouse comes close to it. Note that bounceName() does not take any inputs, unlike drawName() which took two inputs.

Bounce the bubbles

Sweet! Your name rattles around when you move your mouse near it.

Now as cool as it is to see your name shake about, it would be even cooler to bounce the bubbles themselves. We have another function called bounceBubbles() that does this. Similar to `bounceName(), this function does not take any inputs.

Congratulations!

Awesome!! The bubbles in your name scatter about and regroup when you move your mouse over them.

You started this track learning JavaScript, and some of the basic ingredients to make a program, such as strings, numbers, and booleans.

Then you connected those concepts together with variables, arrays, and an if/else statement to wire a program that animates your name. Congratulations on all your hard work!!

We want to thank Google, Rob Hawkes, and Mark Brenig-Jones & Emile Petrone. This course was inspired by and built on their work.

var red = [0, 100, 63];
var orange = [40, 100, 60];
var green = [75, 100, 40];
var blue = [196, 77, 55];
var purple = [280, 50, 60];

var myName = "Ademiya Lee";
var letterColors = [red, orange, blue];

bubbleShape = "circle";

drawName(myName, letterColors);
bounceBubbles();
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,607评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,047评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,496评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,405评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,400评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,479评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,883评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,535评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,743评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,544评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,612评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,309评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,881评论 3 306
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,891评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,136评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,783评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,316评论 2 342

推荐阅读更多精彩内容