navigate site menu

Start learning with our library of video tutorials taught by experts. Get started

HTML5 Projects: Photo Card Titles

HTML5 Projects: Photo Card Titles

with Joseph Lowery

 


In the previously released HTML5 Projects: Customized Photo Cards, you discovered how to import, rotate, and position graphics submitted to your site onto photo cards. In this course, part two of the project, the focus is on text—adding custom text to the cards with a selectable font, size, color, and position. By project's end, site visitors will be able to add a caption of their choosing (complete with CSS3 3D effects) to their photo card. HTML5 technologies covered include HTML5 form elements with enhanced JavaScript, Canvas, and CSS3 3D transforms.

Check out the rest of the series >here.
Topics include:
  • Setting up a caption submission form
  • Creating basic text entry fields
  • Integrating color pickers in the form
  • Adding the caption to the card with JavaScript
  • Applying text effects like shadows and gradients

show more

author
Joseph Lowery
subject
Design, Web, User Experience, Web Design, Projects
software
HTML , JavaScript , CSS
level
Intermediate
duration
1h 11m
released
May 02, 2013

Share this course

Ready to join? get started


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:00(music playing)
00:04Hello! Joe Lowery here, happy to welcome you to HTML5 Projects: Photo Card Titles.
00:09In this short project course, I'm going to show you how you can allow your
00:13website visitors to add titles to their own photo card mashups--
00:17and not just any titles, but titles that can be interactively, resized,
00:21repositioning, and colored with multicolored gradients and complete shadow control.
00:26All of this power comes courtesy of the HTML5 tag Canvas and its
00:30controlling JavaScript API.
00:32You can apply the lessons in this course to any project where you want to give
00:36your user the ability to add creative text to any image.
00:40Okay, that's it. Let's get started with HTML5 Projects: Photo Card Titles.
Collapse this transcript
About the exercise files
00:00I'm very happy to announce that any member of lynda.com has free access to the
00:05exercise files used throughout this title.
00:08Naturally, the information in this course is ultimately intended to be applied
00:12to your own webpages.
00:13However, you may find it helpful to work with the supplied exercise files to
00:18practice the steps to safely and without creating your own examples.
00:22To begin using the files from the site, download them, extract them, and then
00:25store those files in the proper convenient location. Because one part of the
00:30overall photo card project uses PHP,
00:34you'll need to store your files in a web server root folder.
00:37I am on using MAMP, so my files are found in the htdocs folder that's located
00:42within the MAMP application folder.
00:44I've created a shortcut here so I can go right to that, and there you see the
00:49Exercise Files folder.
00:50Now the Exercise Files folder is organized by chapter, and the chapters are
00:54broken down into the various lessons.
00:57Within the lesson chapters, there are a series of items--typically HTML, CSS,
01:02images, and so forth--that all together make up the practice file.
01:06For most lessons you'll also find a folder named final, which contains the
01:11completed lesson file.
01:12You can of course open it up and explore it your leisure.
01:16You can either follow the whole course chapter by chapter or jump in at any point.
01:21If you'd rather not use the exercise files, you can definitely follow along
01:24with your own assets.
Collapse this transcript
Course and browser requirements
00:00To follow along with this course the requirements are pretty straightforward.
00:04From a tool's perspective, you'll need a code editor and a number of browsers for testing.
00:08You can use which ever code editor you prefer.
00:11I am going to be working with Aptana on the Mac, a free editor for both Mac and Windows.
00:16Most of our browser demonstration and testing will be done using pretty current
00:21versions of standard-based browsers like Google chrome.
00:23As the focus in this course is on some of the more recently released
00:27technologies, you probably shouldn't depend on the techniques demonstrated if
00:30your client needs to support legacy browsers like Internet Explorer 6. That's it for tools.
00:36From a knowledge perspective, you should be pretty familiar with both HTML and
00:39CSS, with a general understanding of JavaScript.
00:42I'll walk through any advanced JavaScript we use block by block so you can
00:47understand what's going on under the hood.
00:49One last thought: the absolute best thing you can bring to this course
00:53is your imagination.
00:54Although I'm going to be going through all the details of applying HTML5
00:58technologies to a specific project, you'll get the most benefit if you keep your
01:02mind open for other ways you can apply the same lessons to your own work.
Collapse this transcript
1. Understanding the Project
What we're going to build
00:00Let's take a tour of the completed photo card titles project so you can see
00:04what we'll be building.
00:05If you're the type who likes to dive right into the code, you'll find the
00:09completed project in the Chapter 1/01_01 folder.
00:13This course builds upon an earlier course--HTML5 Projects Customized Photo Cards--
00:19but you don't have to have taken that course to benefit from this one.
00:22Our app is set in the fictional Trans Planet Airlines site, where folks can
00:26create their own space souvenir.
00:28Before we can get to the CAPTION section, where we bring in our title, we'll need
00:33to step through the basics of setting up the photo card souvenir.
00:36The first step is to select a photo background to work with.
00:40I have three to choose from. There is In Space, Weightlessness, and Moonwalk.
00:47Let's go with Moonwalk, and then I'll click Select Background and proceed.
00:51Next, the user gets to upload a file in one of two ways.
00:54They can just click the Browse button or they can use drag and drop.
00:58I'm going to go ahead and just quickly click the Browse button and then navigate
01:02to an image here. I have a couple to choose from. Let's select george.png. I'll click Open.
01:08The file uploads, which you can see as we scroll down, and now we're already to
01:12move on to the modify phase.
01:15Here, the two images are combined, and the user gets a chance to integrate them
01:19by resizing, reshaping, and repositioning the uploaded photo.
01:23Now we're ready to move into the main focus for this course, the title.
01:27I'll click CAPTION and here you see the step has changed so now it says, "Add a
01:31caption to your photo and drag it into place."
01:34And if I scroll down, you'll see that a variety of form controls have now appeared.
01:39Let's scroll back up a bit so we can see the image as we enter our text. I'll
01:43enter Moonwalk, by George and then press Tab, and there is my caption using
01:48its default settings.
01:49So let's change a couple of the options.
01:51I'll choose a different font. I have a few to choose from, so let's go with
01:56fantasy, and that will let me resize it using the Text Size slider. And I'll bring it up
01:59a little bit. That looks good.
02:01So let me scroll down to the next set of options.
02:05Now, let's go for a different base color.
02:08So I'll click right into the text-color color picker here and choose a bright green.
02:14That looks pretty good. We can also change the color Fill Type. Let's choose
02:19Linear Gradient, and there you see the gradient going from bright green to black,
02:23which is my second color. Let's change that second color to something that works
02:27a little bit better here, like an orange.
02:30Okay, that's looking pretty good.
02:31I am going to drag down my text a little bit so that we can see it as we add shadows.
02:37In order to see the shadow better against this background, I am going to change
02:41the shadow color to a dark black, and now let me switch the offset, and I'll go
02:46so that it's coming from the upper-left, and I'll increase the blur a little bit to soften it.
02:51Okay, let's scroll back up, and let me make a few last-minute adjustments
02:56position-wise by dragging it. Perfect.
02:58Now, when we're done we can save the layered graphic as one image just by clicking Save.
03:03Now it's ready to be emailed, printed, or posted online.
03:08So that's a brief overview of the project, and now you're ready to peek under
03:11the hood, so you can get a better understanding of the various technologies
03:15that are driving this app.
Collapse this transcript
Highlighted HTML5, CSS3, and JavaScript technologies
00:00Before we start the actual creation of the project, let's talk about each of the
00:04key technologies being used.
00:06There are three main technologies brought into play in this project:
00:09Canvas, the Range Form control, and some advanced JavaScript.
00:14Let's take a look at Canvas first.
00:16Canvas brings runtime graphics to the web, with a terrific degree of control.
00:21Stroke and fill are independently handled.
00:24It can work with all primitive shapes like line, circle, and rectangle, as
00:28well as text and images.
00:31Perhaps Canvas's best feature--to my mind anyway--is that it enables
00:34interactive graphics.
00:36You can, as we will in this project, combine layers of graphic elements.
00:40You can also apply graphic filters and animate any graphic element by moving,
00:45resizing, and even rotating.
00:47Happily, Canvas has great support, which you can see by going to caniuse.com and
00:53searching for the Canvas feature.
00:55To make the most of canvas, we're going to be using a dedicated library called KineticJS.
01:00KineticJS was developed by Eric Rowell and really brings a lot of advanced
01:05functionality within easy reach.
01:07You can learn more about KineticJS by going to its site.
01:11Next is the form element Range.
01:14The basic code is pretty straightforward. As you can see, it's an input type with
01:18a few additional attributes like min and max.
01:22Value serves as the default setting when the form element is first displayed.
01:26There is also an optional attribute, step, which determines the increments
01:30that the slider moves.
01:31Overall, browser support is pretty good, with the exception of Firefox.
01:36You can get a very specific picture by going to caniuse.com and searching for Range.
01:41As I said, Firefox is a problem, but as you can see, they do have it slated for
01:46implementation a little further down the road.
01:48However, for right now, there is a JavaScript helper file available that I'll
01:52introduce you to during the course which brings Firefox into the slider camp.
01:57Finally, let's take a closer look at the advanced JavaScript that we'll employ.
02:02The primary function we'll be incorporating is addEventListener.
02:04As you might suspect from the name, it makes it possible to trigger a function
02:10when a particular event occurs.
02:12It was introduced prior to HTML5 and enjoys widespread browser compatibility,
02:18with many different events supported, everything from mouse clicks and
02:21double-clicks, mouse over, mouse out, key up, key down, form element change,
02:25form submit, and more.
02:28addEventListener takes three arguments:
02:30the name of the event entered as a string in quotes, the function to be
02:33called, and a Boolean--
02:35true or false--to set the useCapture functionality.
02:39If you're working with multiple functions for the same event, you can determine
02:42their firing order via useCapture. The default setting is false.
02:47Here is some sample code for addEventListener that we'll be using in our project.
02:51In this example, the form element is a text field, the event is change, the
02:56function is textBoxChanged, and useCapture is set to false.
03:01As you'll see in the remaining lessons, the technologies involved in this
03:04project are full-featured and pretty well supported across all major
03:09modern browsers.
Collapse this transcript
2. Setting Up the Form
Creating the basic fields
00:00Let's start off the project by properly setting up the form.
00:03We'll be working with the modify.php page that holds the core image
00:07manipulation, and using a bit of JavaScript magic to display the form fields
00:12when the user clicks the caption link in the main navigation.
00:15So I have modify.php from the Chapter 2/02_01 folder open.
00:21You can see the JavaScript function that I mentioned if I scroll down a bit.
00:27That starts on line 35.
00:29After the captionNav link is clicked, a standard called to stop the browser from
00:34looking for a link is given.
00:36Next, the form is shown by changing the CSS to display block.
00:41Then the h1 tag that holds the steps is updated with the new relevant text.
00:46Let's move on to the form.
00:47I am going to scroll down towards the bottom of the page, and I want to put my
00:52form right after line 74, where the container div is placed.
00:57This is the div that holds the canvas tag which is inserted at runtime by our JavaScript.
01:03So, we want our form to be right below that.
01:08Let's start with the basic form tag.
01:10I'll give it an ID of textForm and then set the action to the same page.
01:27Now, let's make some room for that form content.
01:29Next, we'll isolate this section of the form--
01:33there will be three different sections in all--with a fieldset and legend combo.
01:44I'll enter "Create your text" as the legend here.
01:47Now I am going to put the first of three form controls, a simple text box
01:51for entering a caption.
01:53We'll use an HTML5 attribute placeholder to provide a hint for the user and
01:59leave the value blank.
02:00I am going to put each of these form controls on their own line, so I'll start
02:04the coding with a paragraph tag.
02:07We'll enter in our label first, and I'll use the for attribute and set it equal
02:14to the ID of the text box, which happens to be called textBox, and then enter my
02:19actual label, text with a semicolon,
02:22and then after the closing label tag, which Aptana has so nicely provided for us,
02:27we'll put in our input tag.
02:30And this will be a basic text input type, with, as I said, an id of textBox, a placeholder.
02:39This is an HTML5 attribute that puts a little bit of a hinting text into the
02:43middle of the text field.
02:46We'll suggest to them that they enter a caption, and I'm going to set the
02:50value to an empty string.
02:53Okay, let's close the input tag, and that's the first of our three, but let me go
02:59below it. And in fact, I am going to scroll down somewhat so we're not right at
03:04the bottom of the page anymore. There we are.
03:05Now let's make it possible for the user to change the font.
03:08Of course, you don't leave it completely up to the user.
03:12That would open the door to errors galore; we're going to use a select list.
03:15I'll limit the options to the basic fonts and then add a nonbasic one, just for fun.
03:20So we'll start with our paragraph tag, and next comes the label. Again, I'll use
03:27the for attribute, and the id of this select field is textFont, and the label is
03:38Text Font in two words with a colon.
03:43Okay, after the label, I'll enter in my opening select tag, give it my id of
03:51textFont, and then we are going to enter in a series of options.
03:56As I said, we're going to use the basic font names. Of course you can
04:00substitute other font families, but I just want to use the basics to illustrate the concept.
04:07So option value = serif, and we'll enter in serif as the font.
04:16Next option is going to be sans-serif. Following sans-serif will be cursive. Fantasy.
04:33Finally, we'll enter the last of our generic fonts: monospace.
04:43Now, for a nongeneric one I am going to use Impact, a very big bold font which
04:49works really well with certain types of effects that we'll be implementing.
04:55Okay, our second form field is now done.
04:57Again, I'll scroll down, and let's add our third paragraph tag for our third field,
05:04which will be one that will allow the user to change the font size.
05:09We'll enter in the label, with the for attribute set to textSize, and the text
05:18label itself will be Text Size:.
05:22And now we'll put in a new input tag. And initially, we're going to start it out
05:27as a standard text field, so it will be type="text" and give it an id of
05:34textSize and an initial value of 50.
05:40This last entry, as a standard text input field, is not too exciting or
05:44particularly user-friendly.
05:46However, in the next lesson, we'll convert it and other fields to an HTML5 slider
05:52and really boost the user experience.
Collapse this transcript
Adding range sliders
00:00Right now we're using a standard input text field to determine the font size of our caption.
00:05Not only is that pretty mundane, it's also fairly user-hostile.
00:10If your website visitor is web savvy, they may enter 48 pixels, but do they
00:14spell out pixels or do they enter the abbreviation, px?
00:18And if he or she is from the print world, they may think in terms of points.
00:22Most others however, would probably enter a measurement in either inches or centimeters.
00:27All in all, we're looking at the possibility of entry errors galore.
00:31Luckily, HTML5 provides a much more intuitive visual method for selecting a
00:36font size: the input type range, which is displayed in compliant browsers as a slider.
00:42In this lesson, I'll show you how to change a text input field to a slider and
00:48clue you in to a way to avoid a massive cross-browser failure.
00:52We'll start by going to the modify.php page from the Chapter 2/02_02 folder
00:59and then scroll down.
01:00I am going to go down to around line 95, which is where my current font size text field lives.
01:07Now, I am going to change the type from text to range, and now that it's a range,
01:15I can add a number of slider-relevant attributes such minimum and maximum.
01:21Those go in as min, which I am going to set to 0 and max, which I'll set to 200.
01:30Now, if I save the page and then head over to a modern browser like Chrome, there
01:35is our standard text field.
01:37But let me refresh the page, and now it's a slider.
01:41Now, that's very cool. Very clean and easy to use.
01:44Browser support for the range input type is, as I said earlier, pretty solid
01:49across the current browser board, with one major exception.
01:53Let's take a look at what happens in Firefox.
01:56Now here we are in Firefox, and as you can see, the Text Size field is still a
02:01text box. Let me refresh the page. No change.
02:05So we're back to a standard text field, which is what happens when a browser
02:09doesn't understand the range input type.
02:12Luckily, while we're waiting for the Firefox team to correct this oversight,
02:16there is a developer named Frank Yan who has come to the rescue with a
02:20JavaScript helper file.
02:21You can find out more about Frank's work and download the current implementation
02:26of HTML5 slider.js from the GitHub URL shown here.
02:31I've already downloaded my copy of HTML5 slider.js and placed it in my Scripts
02:36folder, so all I need to do is link to it.
02:39Let me scroll back up to the head of the document, and I'll put a script tag
02:44right after the script tag that calls for one of our support files imageinfo.js.
02:49Let me just quickly copy that one tag here, paste it in, and then I'll change
02:56imageinfo to html5slider.js.
03:03Here we'll get rid of that extra line I introduced, and with that done, let's
03:07save the page again and go back to Firefox and refresh the page. Excellent!
03:12Our slider now works as expected in the full slate of modern browsers.
03:18In the next lesson, you'll see how to add another advanced form control, a
03:22color picker.
Collapse this transcript
Integrating color pickers
00:00Along with allowing users to specify the caption text, the font, and the font
00:05size, we want to give them a lot of control over color, not only because it's fun,
00:10but also to make the captions stand out-- or blend in, if that's the preference--
00:15against a wide array of backgrounds. To that end, let's take advantage of
00:20Canvas's flexibility when it comes to text fill and provide an option for two
00:25different types of gradients, as well as a solid color fill.
00:30For that degree of functionality, we are going to need a select list to choose
00:35the type of fill and two different color pickers.
00:39Let's get started. I have modify.php from the chapter 2/02_03 folder open now, and
00:46first I'm going to set up my field set and legend to hold all my color options.
00:51So let's scroll down to the bottom of the page, and we'll put this field set
00:55right after the previous one. And within the fieldset tags, we'll enter in our
01:02legend, and for a legend I'm going to use "Choose a solid color or gradient."
01:10After the legend tag we'll put in the fill type selector first.
01:15So this goes on its own paragraph tag, and we enter the label with the for
01:21attribute and set that equal to fillType, and the text we'll enter for the label
01:29itself is of course fieldType with a colon.
01:34And let's start our select tag, give it an id of fillType, and there'll be
01:41three options here.
01:42The first option will be for a solid-color fill, so the value I'm going to
01:47give it is just colorFill, and the text I'll enter is Solid Color, to
01:54distinguish it from the gradients.
01:56The next option will be a linear gradient, and the value will give it is
02:05linearGradient. See if you can guess what the actual text will be. Let's enter
02:12an option for a radialGradient.
02:15Now, after this paragraph tag we'll put in the first of two color pickers.
02:20To get the color picker functionality we'll rely on a JavaScript library, but we
02:25need to get the HTML right first.
02:27So paragraph tag and then our label for textFillColor, and we'll call this one Text Color.
02:41The input tag here we're going to give a class of color--this is for our
02:46JavaScript color picker utility--and an id=textFillColor.
02:56If we don't give a type, browsers will assume that it's a text field, and that's just fine.
03:00Now let's give it an initial value.
03:03We're going to enter in a kind of a darker red that is the same red that's used
03:07in our logo, so that's B20000. Notice I do not put in an opening hash tag,
03:16and that's as per our JavaScript functionality.
03:19Okay, let's close off the input tag. The second color picker control
03:24is basically the same. So I'll just copy paste and then, make a few changes.
03:28I'm going to copy everything within the paragraph tag, paste that in, and then
03:34let's change the id to textFillColor2, so I'll change the for attribute there,
03:42and I'll change that id as well. And let's change the label to gradient,
03:48Gradient Color instead of text color, and finally, let's go with black as an initial value.
03:53Again, we don't put in the hash sign in front of a color value because
03:57that's the way our particular JavaScript library likes it.
04:01Speaking of which, time to bring it onto the scene.
04:04The library we're going to be using is by a check coder named Jan Odvarko.
04:09You can learn more about his project and download a copy at jscolor.com.
04:15It's extremely efficient and flexible and amazingly doesn't require a framework like
04:21jQuery or MooTools, although it works fine with any of them.
04:25Now, I've already downloaded a copy and I have it in my local folder, so we're
04:29going to include it with a script tag.
04:31I'll scroll back up to the head section. So, let's copy our last script tag
04:36that we have here and then paste it in and change the source to its own folder,
04:44jscolor/jscolor.js.
04:51Now let's save. Let's go browse the page to see how it worked.
04:55Let me refresh my browser page. There is my new form editions, here is Fill Type
05:03showing my three options, and hear are my Text Color fields.
05:06So if I click on one, there is my color picker.
05:09Now I can choose a hue from the main area on the left and then select a value or
05:16brightness from the smaller area on the right.
05:18Let's make that a really bright green, and let's double-check and make sure the
05:23gradient color works as well. It seems to. Let's make that purple just for fun and
05:27give it a medium value.
05:29Pretty cool, don't you think?
05:31So now we have a variety of user- friendly form controls for our caption, and
05:36in the next chapter we'll start bringing our app to live with the Canvas and
05:40JavaScript.
Collapse this transcript
3. Crafting the Caption
Setting up the JavaScript
00:00Time to enter the belly of the beast.
00:02In this chapter, we're going to be diving deep into the JavaScript that
00:05powers the Canvas tag.
00:07We'll start off in this lesson by laying a fair amount of the foundation, as we
00:11begin to make our text caption form field active.
00:15As I mentioned in an earlier chapter, the library we're using is called
00:20KineticJS. You can see the link to it on line 9. Right below that is a link to
00:26our custom Canvas JavaScript file canvas_custom.js, which is located in the
00:32chapter 3>03_01 scripts folder. That's where we'll be doing the vast majority of
00:37the rest of our work in this course.
00:39Let's head over to that file.
00:42Now, there is a fair amount of code already in place here. It's used to drive the
00:47application thus far, and you can get all the details on it in my lynda.com
00:52course HTML5 Projects: Photo Cards.
00:55We're going to begin to blend in the text oriented code, and the first thing we
01:00need to do is to set up a text layer and a slew of default variables.
01:05Let's go to line 119 and create a new Kinetic layer object.
01:12So I'll enter in var, and I'm going to call this textLayer and set that equal to
01:19new Kinetic.Layer, parentheses, open and close semicolon.
01:25Next we'll get the context of that object.
01:28Now, the getContext function that we're going to use is a basic canvas API call,
01:33and for most functions KineticJS has that all built in.
01:37However, there are a few things that are not yet covered by the library and I'll
01:42show you in a later lesson, chapter four's Crafting Linear Gradients, to be precise
01:47just how to get and set canvas properties in a basic non-KineticJS way.
01:53So we'll set up another variable, and I'm going to call this textContext and set
01:59that textLayer.getContext, and I'm going to specify this as a 2d context.
02:11Canvas, although it's not extremely robust as of yet, Canvas will eventually be
02:16able to handle 3D objects as well, and we're working in a 2D context.
02:22Put in my closing semicolon and we're ready to move on.
02:26So next we're going to define our default settings.
02:29These will take effect until the user makes a change through the form elements.
02:34But first I'm going to set up a variable for the caption, which I'm calling here
02:38the message, and we're going to set that to an empty string.
02:43Because when the user first opens the captions control, we don't want to have a
02:48default caption sitting there;
02:50we want the user to enter that themselves.
02:53Next, let's set up the default textFill color, and we'll make this equal to
02:59hash sign B20000.
03:04Next, our default fontSize, and I'll set that equal to 50.
03:10Now we'll put in a fontFace, and we'll choose serif for our default there.
03:18We need to set the default fillType, and we want a solid-color fill. The option
03:25for that is colorFill.
03:28Next, we want to set the value for our second color picker, which is called
03:33textFillColor2, and I'm going to set that to black, which is #000000.
03:44Next up is the shadowColor, and we'll make that #707070, which is kind of a
03:53grayish color, then the default shadowBlur which I'll set equal to 2, and finally
04:01the shadowOffset, which I'll make equal to 4.
04:07We're going to create the text object and set a couple of default values for it.
04:12I have a little placeholder comment here. I'll put my code right there, and we'll
04:17set the var equal to theText. And we're creating a new text object, so that's new
04:24Kinetic.Text. And after the text we enter in an open and closed parenthesis.
04:31The properties we're setting here will be within an array, so within the
04:34parentheses are a pair of curly braces, and I'll open that up to put in the
04:39first of my three properties, which will be x:.
04:43And we want this to be centered,
04:46so I'm going to get the value of the stage width, and we'll do that by
04:51referencing the stage object.
04:52This is a KineticJS concept and use the function called getWidth and then
05:00divide that by 2. Follow that with a comma for our second parameter, which
05:05is y, and very similarly, we go to stage.getHeight, also divided by 2, another
05:14comma, and then the text parameter will be set to whatever the content is,
05:19which is theMessage.
05:20The x and y values will place the text initially in the center of the stage as I
05:26said, or rather, it will start the text at the center and then the letters will be
05:32drawn out to the right.
05:34Now, to keep the text centered, we'll need to take the width of the text into
05:38account and offset the x value by half of that.
05:42The text object has a function just for that purpose.
05:47So I'm going to put this code right under my center text comment, and we'll
05:51reference our new text object, .setOffset, open and close paren, and again open
06:00and close curly braces within that. And here it will set the x to
06:03theText.getWidth () divided by 2.
06:09Okay, we're ready to integrate the new text object and layer into the stage.
06:15Placement of the code is critical here because it determines the z index of the
06:20layers and what's on top of what.
06:22So in the section with the comment Add objects to layers and layers to stage, on
06:27line 145, I'm going to bring in the text object first and add that to the text
06:33layer, and I'm going to do that after the two additions to the general layer,
06:40textLayer.add (theText).
06:45So textLayer is the layer, theText is the object that we just added to it, and
06:50then after the basic layer, the graphics layer, has been added to the stage,
06:55we'll add the text layer to stage.
06:57This will make sure that the text layer is always on top of the graphics.
07:02So stage.add (textLayer). Lastly, we need to add a key function that will
07:09initially draw our text.
07:11This will apply our default settings.
07:13To do this we're going to skip down to where the stage draw function is found,
07:20which you can see on line 185.
07:24The custom function we're going to call I've named drawText.
07:27All right, our core text JavaScript code is in place.
07:32Next, we'll set up the functions we need to make the text input field active.
Collapse this transcript
Entering custom text
00:00To bring the text field to life so users can import their own caption, we need
00:05just three more steps.
00:06We're going to need to take similar steps for all of our form elements, so we're
00:10really starting to lay down the functionality now.
00:13The first step brings in an advanced JavaScript function
00:17called addEventListener.
00:18As described in chapter 2, this function waits for an event to be fired on a
00:24specific form element and when it does, executes a function.
00:28The text field where the user inputs the caption is the only form element where
00:33we'll use two different events to cover all of our update scenarios.
00:38So I'm going to scroll down to the bottom of my canvas_custom.js file. Under line 188,
00:46I'm going to enter in the first of my addEventListener blocks.
00:50So first we'll set up a variable called formElement and set that equal to
00:58document.getElementById, and the ID we're looking for here is textBox.
01:09Now once we have that, we can add the event listener to it.
01:15So formElement.addEventListener, and you'll recall this takes three arguments, and
01:22the first event is the change event,
01:24very useful when you're working with formElements. And we'll be using that one
01:29quite a bit throughout the rest of the course. Follow that with a comma and
01:33then the name of the function that we're going to call.
01:37This is called textBoxChanged, another comma, and finally, we'll enter in the
01:43Boolean for use capture and we'll set that to false.
01:48Okay, at the end of the line I'll put in my semicolon.
01:51Now we want to add another one of these. It's pretty much the same,
01:54so I'm just going to copy this code block here and then paste it in. And the only
02:02thing I want to change is the event, and I'm going to set that to blur.
02:07If the user just puts their cursor into the text field and doesn't change it,
02:12which happens sometimes when you do a page refresh--it doesn't totally refresh
02:16everything--the caption will still appear.
02:19Okay, we referenced textBoxChanged.
02:23Let's go create that function now.
02:27So function textBoxChanged and then in parentheses e for error, open and close
02:37curly brace, target = e.target, and the variable we previously defined, theMessage,
02:46we'll set equal to the target value.
02:52And after that's all done, we're going to call our drawText function.
02:58Okay, all that's left is to create the function we just referenced, drawText.
03:04This will eventually hold all of our update functionality, but for now we just
03:09need to set the text to whatever the message is that the user enters,
03:14define the font size using the default font size value, and put in a set fill
03:22method that will use the default color and finally draw our text layer.
03:27You need to specifically draw the layer that the text is on for any changes to take place.
03:33So let's scroll down a tad. And under the comment on line 203, this is where our
03:40function drawText will live. And as I said, the first one will be referencing the
03:47text object we created called theText, and we'll set the text to the message.
03:57We'll set the font size, and we'll set that to the variable fontSize.
04:09Then we want to set the fill--that's the color of the object--and we want a set
04:17that to the variable textFillColor.
04:20Finally, I'm going to make a little room for this because this should be the
04:24last item within the function, we want a specifically draw the text layer,
04:29so the textLayer.draw. Okay, I don't know about you, but I am ready for some gratification.
04:38Let's save the page and I'm going to went over to my browser where I have it set
04:43up. I'll hit refresh. There is my photo card.
04:48If you're not familiar with the previous application that we're building on, the
04:52photo card options--the two images-- were saved using an HTML5 feature called
04:58local storage that saves the values specifically for the browser that's used.
05:03So all you have to do, once you've gone through those initial steps, is just go to
05:08the modify page and hit refresh.
05:11Now though, we want to see if we can get some text on the page.
05:16So let's enter Moonwalk with Me! Press tab and there's our text.
05:22You'll notice that the text is not centered and in fact is partly off-screen.
05:26We'll take care of that issue in the lesson called "Setting text size."
Collapse this transcript
Selecting the font
00:00The choice of a fontface can really make a statement about a caption,
00:05whether it's a serious, whimsical, or evocative.
00:08In this lesson, we'll add the necessary JavaScript code so the users can
00:13decide which font they would like to use for themselves.
00:16I have a canvas_custom.js from the chapter 3/03_03 scripts folder open, and I'm
00:23going to scroll down to where my form element listeners are defined,
00:29which is almost all the way down towards the bottom there, starting on line 188, and
00:36we'll add in the first of our code chunks.
00:39For this one we're going to be listening to the text font form element that you
00:44may recall is a select list with a variety of fonts as options.
00:48Now I'm just going to go ahead and copy the first one here and then paste it
00:54down below it, and let's change the element ID from textBox to textFont, and likewise,
01:03the function that is called from textBoxChanged to textFontChanged.
01:08Next, we'll need to define the textFontChanged function.
01:12This code is quite similar to what we use for the textBoxChanged.
01:16So again, I can do a copy-paste, saving a little bit of time, and we want to make
01:22it textFontChanged and then change the variable.
01:27Instead of theMessage, it's going to be fontFace.
01:34Finally, let's put that fontFace variable to use in our drawText function.
01:39I'll put this bit of code between setText and setFontSize so that it will
01:45mirror our form layout.
01:51So we'll invoke the text object and enter setFontFamily and set that to fontFace.
02:01Okay, let's check it out. I'll save the file and then switch to the browser, hit
02:07refresh, and let's scroll down. Now, I'll enter in a simple caption, and let's try
02:16changing our text font. Let's try sans-serif. It seems to wor. Let's go to
02:21cursive, looking good, fantasy, monospace, and then impact.
02:29All right, we're cooking with gas now.
02:32In the next lesson, we'll make it possible for the user to quickly change the
02:36caption size by using the slider control.
Collapse this transcript
Setting text size
00:00In this lesson we're going to power up the font size slider so that our users
00:05can quickly and easily adjust their caption size as needed.
00:09Part of the code that we'll insert will ensure that the text is nicely centered
00:14when it first comes on the page, because, well, neatness counts.
00:18Let's begin by adding our event listener code to the canvas_custom.js file found
00:24in the Chapter 3/03_04 scripts folder.
00:27We'll need to scroll down to around line 197. And again, this is very similar to
00:35what we've done before, so we can do our copy-paste operation, change the ID
00:42that we're looking for from textFont to textSize, and then change that function
00:48name from textFontChanged to textSizeChanged.
00:56Now, let's copy previously done function, paste that in, change the name of the
01:04function to textSizeChanged, and then the variable from fontFace to fontSize.
01:12At this point, we'd normally add in the fontSize variable code to the drawText
01:18routine, but we've already done that. That's on line 224.
01:23We did it because it was necessary to demo the text display.
01:26However, what we don't have yet is a way of accurately centering the text that
01:31takes its width into account.
01:34So let's add that code just below where fontSize is defined.
01:38Let's being that up to the center of the page there, and we'll in theText.setOff,
01:47open and close paren, open and close curly brace. And we want to set the x value
01:54to theText.getWidth, open and close paren, divided by 3.
02:02Now you saw, all this code before when we set up the default values,
02:06basically the same syntax.
02:08Okay let's check our work.
02:10I'll save the JavaScript file and then head over to the browser.
02:14Let me click refresh, make sure we've got the most current code, and I'll enter
02:21in an appropriate caption, and now our text is nicely centered.
02:28So let's try the slider now, and that seems to be working really well.
02:34Now the user can easily and very visually adjust the size of the caption via the range control.
02:40You may be thinking, that's nice, but could we maybe have a different color than the red?
02:45You bet. Implementing color choice is coming up next.
Collapse this transcript
Choosing a color
00:00For me, color choice is pretty critical, especially when you're dealing with a
00:05variety of backgrounds.
00:06So we want to make this operation particularly smooth and of course visual.
00:11Let's put our snazzy color pickers to work.
00:13To pull this off, we only need to add our two basic code blocks: one for the
00:18event listener and the other for the function.
00:21So I'll start with the event listener and put it below the others at the
00:24bottom of the page.
00:26I'll scroll down towards that. Let's grab the last one and copy it, paste it in,
00:33change the element id from textSize to textFillColor, and then change the
00:40function called from textSizeChanged to textFillColorChanged.
00:49Next, we'll do another copy-paste and change name with a function to
00:54textFillColorChanged and fontSize to textFillColor.
01:01Now I need to make one other change here, as we're writing out the
01:05textFillColorChanged function.
01:08When it comes time to define the variable, we're going to need to do a little
01:12string manipulation.
01:13The JavaScript color picker wants the color value without the leading hash
01:18sign, but KineticJS on the other hand needs the hash sign.
01:22So I'm going to add that in as a character in quotes and then concatenate the target value.
01:29So we'll enter in quote, hash sign, quote and then a plus sign to
01:34concatenate or bring together the two strings, and now that's done.
01:38Because we already have our setFill color drawText, as you can see on line to 237,
01:44we're ready to rock and roll.
01:46I'll save the page, switch to the browser, refresh.
01:49Let move my little head in here, just for a little variety, and let's enter in a
01:55new caption. I'm going to call this one Head Space.
01:58Let's scroll down to where our color pickers are, and we'll click on the red
02:04color picker here and then choose a yellow, and that's showing up really well.
02:09Let's go make it a little bit darker.
02:11So the color functionality is working quite well, both on the hue and value side,
02:16and the color picker gives us immediate feedback. It's actually kind of fun to
02:21play around with, as we try to find just the right color.
Collapse this transcript
Dragging the text
00:00Given how difficult it is to enable drag-and-drop in HTML5, not to mention
00:06how cool it is to be able to move page elements at will, it's going to be kind
00:10of anticlimactic when you see how easy it is to implement drag and drop with canvas.
00:15Would you believe one line of code--and a small one at that? Let's get to it.
00:19So basically, as step four says, add a caption to your photo card and drag it into place.
00:27So we want to make the text object, a.k.a. the caption, draggable.
00:31And for that we're going to need to revisit our definition of the text
00:35object, which starts on line 134 of canvas_custom.js, found in the Chapter
00:433/03_06 scripts folder.
00:45So I'm going to add a comma after text:
00:48theMessage, on line 137 so I can add another property and enter our one line of code: draggable:
00:57true, and that's it.
00:59So let's save the page and take our text for a drive.
01:03Back to the browser, hit refresh, enter some code, and move it around.
01:10The great thing about the drag-and- drop is that you can move it literally
01:14anywhere on your canvas.
01:15You can even move it partly off-screen, if there's some reason you would need to do that.
01:20In the next chapter, I'll show you how to further enhance your caption with drop
01:24shadows and gradients.
Collapse this transcript
4. Adding Text Effects
Creating shadows
00:00One of the problems that stems from laying a caption over an image is contrast.
00:05Unless the photo background is in black and white, no matter what color you
00:09choose for your text, you are not always assured there will be a location on the
00:14image that doesn't blend in with your caption.
00:17This issue is made worse when we add the option of gradient-colored text, which
00:22we'll do in the next two lessons.
00:24So, how do you solve such a contrast issue?
00:27Well, one elegant solution is to give your text its own background via the
00:32Canvas text shadow options.
00:34In this lesson, we'll add everything you need, from HTML to JavaScript, so that
00:39you can incorporate a shadow for your text with user control over offset, blur, and color.
00:45Let's begin with the HTML.
00:47I have the modify.php file from the Chapter 4/04_01 folder open in my code editor.
00:54I am going to scroll down to the end of the form and add another field set and legend.
01:01So we'll go right after the last field set. And then after we have that, we'll add
01:08in our legend, and here the legend will read Add a shadow.
01:14Let's add the first of three form elements so we can control the shadow's
01:18offset from the text.
01:19To simplify the concept I'll insert a single range input type to handle both
01:24the X and Y offsets.
01:26This will position the shadow anywhere from the upper-left to the lower-right of the text.
01:31Of course, you could add another slider control for separate X and Y values,
01:36so I'll put my cursor at the end of legend here and hit Return and start
01:40entering my paragraph tag.
01:42Within that, I'll put in a label tag with a for attribute and set that equal to
01:49shadowOffset, which is the id of my slider control for the shadow offset.
01:56Now, let's give it a label text Shadow Offset:,
02:00and after label we'll insert the input tag. I am going to set the type equal to range.
02:09The id we'll make equal to shadowOffset.
02:12I am going to set a minimum equal to -10.
02:18This will allow the offset to move from the center position to the upper-
02:24left. And we'll set a max to 10. This will allow the offset to go down to the lower-right.
02:31I am going to start in the lower-right position with a value of 4.
02:38Okay, we'll close that input tag and move our cursor down below the paragraph.
02:43Next, let's add a control for the shadowsBlur.
02:48I don't want this to get too fuzzy, so I'm going to limit the range from 1 to 10
02:53and start out with a value of 1. Since this is very similar to what we coded for
02:59Shadow Offset, I'll just copy-paste.
03:03Copy my selection, paste it in, and now let's make some alterations.
03:07Instead of shadowOffset, it's going to be shadowBlur. We'll change Offset to Blur here too.
03:15Okay, our input type is still range, but we want to make the input type shadowBlur,
03:21and we'll set the minimum to 1 and not -10.
03:25The max we'll keep at 10, and I am going to set the initial value to 1.
03:31For our final shadow-related control, we'll insert another color picker and give
03:36it a grayish value as the default.
03:38I am just going to go up to line 114 and copy one of the existing color pickers.
03:44Now let's go down and paste it in. We'll get rid of that extraneous line. And now
03:49let's change the for attribute to shadowColor.
03:54We'll change the label itself to be, similarly, Shadow Color, two words, and let me
04:02just copy this for attribute and then paste it in as the id.
04:06All right, let's change the value to a lightish gray as I said. 707070.
04:10Okay, the HTML is done, so let me save the page, and then we'll switch over to
04:17the canvas_custom.js file that is found in the Chapter 4/04_01 scripts folder.
04:24The code we'll insert here will follow the same pattern as for the other
04:29functionality in our app.
04:30First, let's set up the event listeners,
04:33so I'll scroll down to that section. And I am just going to copy the last one
04:38here, paste that in, and the element we're targeting here is the shadowOffset, so
04:43shadowOffset. Just copy that little text there so I can change this function
04:51called shadowOffsetChanged, and that one is done.
04:56Now let's copy that, paste it in.
04:59Next, let's add an event listener to shadowBlur.
05:03So I'll change Offset to Blur, both in the getElementById argument and as part
05:11of the function name that we're calling, so it's shadowBlurChanged.
05:13And one last time, we'll copy both of those statements, paste it in. Let's change
05:21shadowBlur to shadowColor and change the function name as well.
05:29Okay, now we'll define the three functions we referenced.
05:34So there is my text event function, so I am going to move down towards the end
05:38of them, copy this one, paste it in once, and now we want to change the function
05:44name to shadowOffsetChanged. And the variable, instead of fontSize, we'll change to shadowOffset.
05:54Okay, let's do a copy of that and paste it in.
05:58Now I only need to change one word. Instead of shadowOffsetChanged, it's going to
06:04be shadowBlurChanged, and of course, the variable then would be shadowBlur.
06:09One last one to paste it. We still have that shadowOffsetChanged function in our clipboard.
06:15Now I can change Offset to Color.
06:18shadowColorChanged and shadowOffset becomes shadowColor.
06:23Now, you'll recall with the color pickers we need to do a little bit more work
06:28because we're adding in the hash sign,
06:31so I'll put that in in quotes and then add a plus sign to concatenate the two strings.
06:38Finally, let's add the executable statements to the draw text function.
06:43We'll scroll down. And I'm going to isolate these a little bit, since they
06:47are their own group.
06:49And first, we'll set the shadowColor so theText.setShadowColor, and we'll set it
06:56to the variable shadowColor.
06:58I bet you know what's coming next.
07:01Yes, it's true, a little copy paste.
07:04This time instead of shadowColor, it will be Blur, so change both the property
07:10and the value to shadowBlur, paste that again. Let's do shadowOffset,
07:15setShadowOffset, and we set it to shadowOffset.
07:22Okay, now just to throw you a complete curve, I am going to add in one of the
07:26property we haven't worked with, and that's opacity.
07:31Now we could have set up another slider to control opacity; that's true.
07:35But I didn't want to overwhelm the user with a series of options,
07:38so we're going to set this to a specific value.
07:42ShadowOpacity would be set between 0 and 1. I am going to make this 0.75. Okay,
07:50time to save and test.
07:52In the browser you see I have the page from Chapter/04_01 open, and I don't have
07:57the shadow HTML showing yet, so let me click refresh. There is my ADD A SHADOW.
08:02Now before we go into there, let's add some text, and there is my text there.
08:10Now, if I pull it over the white, you can see there already is a drop shadow
08:15defined. That's looking pretty good. That isolates it some.
08:18Let me scroll down now to my shadow controls. So that we can see it a little bit better,
08:24I am going to change the Shadow Color to black.
08:26Yes, it's showing up a lot better now.
08:29Now watch what happens when I too move the Offset, you can see that my Shadow
08:33Offset has disappeared.
08:35So that indicates that there is a problem.
08:37Let's go back to the code, and I want to go to where my shadowOffsetChanged
08:43function is defined.
08:44And you can see it's just like all the others, where it picks the target value
08:50and sets it to the variable.
08:52Now I know from experimentation that sometimes this Offset control seems to
08:57be a little finicky.
08:58So, even though you shouldn't have to do this, because you don't have to do it
09:02for any of the other slider controls, I'm going to expressly make sure that
09:06target value is a number, and I'll do that by adding in a parse int function.
09:12This will change any string to a number. It's a standard JavaScript function.
09:20Okay we'll save that change. Now let's try it out back in the browser.
09:24again I'll Refresh. I am going to have to bring in my text again, so I've
09:29already got that text there. I'll just hit Return, and I should see it up here.
09:33There it is. Let's bring it down.
09:37Again, let's change the color to black to see it.
09:41Now let's try out that Offset again.
09:43So I am going to go to the left. It's working beautifully now. Excellent!
09:47Let's go a little bit up to the left here just to show it off.
09:51You can see that if I move in my blur, it gets a little bit softer;
09:55if I move it all the way, it really fades away and becomes quite natural.
10:00So our shadow enhancement seems to be working just fine now.
10:03There are a good number of further additions you could add.
10:07You could, for example, create separate offset controls for the X and Y axes or as
10:12I said, add another slider for opacity.
10:15I'll leave that as an exercise for you to do at your discretion.
Collapse this transcript
Crafting linear gradients
00:00Maybe I just have a low threshold for excitement, but the first time I saw
00:04gradient-styled text--I think it was in a Photoshop tutorial--I
00:09distinctly remember going "Ooh!"
00:12In this lesson we're going to put that functionality into your site visitors'
00:16hands, with a lot more interactivity and a lot fewer steps than that old PS2, I
00:22can guarantee you that.
00:24Any HTML we need is already in place, so let's go directly to the
00:28canvas_custom.js file found in the Chapter 4/04_02 folder and bring in the
00:34necessary event listeners.
00:36I'll scroll down to that section, which is getting pretty lengthy at this point, and
00:40I am going to copy the textFillColor one here and I am actually going to put it
00:46right below it, because all I need to change here is I am activating the second
00:50color picker, which is called textFillColor2, so, similarly, we have to change the
00:56function name to textFillColor2Changed, and then we need to add one more, so we
01:03can listen for the fill type.
01:07So I'll change the ID from my pasted in code to fillType and change the function
01:14name as well to fillType changed.
01:18Two new event listeners mean two new functions, so we're going to code those next.
01:25Again, I'll take the shortcut of copying the textFillColorChanged routine and
01:31then paste that in, and let's change the name of that to textFillColor2Changed and the variable.
01:41Okay, that's all we need for that one.
01:42I am going to paste what I have on my clipboard again, and this time we'll
01:47change the function name to fillTypeChanged, and the variable will become
01:54fillType as well, and I can lose the additional string and concatenation.
02:00So it's fillType = target.value.
02:04Now comes the fun part.
02:05Here we need to specify what happens if someone chooses linear gradient from
02:10the fill type select list.
02:13So let me scroll down to our drawText code.
02:16Right now the fill is controlled by the code on line 283,
02:20theText.setFill (textFillColor).
02:23So what we need to do now is to set up an if-else clause to take care of
02:28the condition that happens when somebody chooses a linear gradient from the
02:33fill type select list.
02:34I am going to temporarily remove our setFill line here, as that will become
02:41integrated into our new code.
02:43Let's put this right after the shadow block. And we'll need to set up a couple of
02:47variables first, because we need to position the gradient fill.
02:51So, the first is var textMiddle, and I am going to set that equal to theText.getY--
03:03that's Y the coordinate--plus theText.getHeight function divided by 2.
03:08So what this line will do is it will get the top Y coordinate and then add half
03:15the line height to it, so that the Y value, the text middle, will be right in
03:20the middle of the text. All right, a semicolon
03:22to close that linem and let's do something similar for text center. This is a
03:27lot simpler actually.
03:28So var textCenter, this is going to be the horizontal center.
03:33This we can do with just theText.getWidth divided by 2.
03:39Okay, now that we have that, let's set up our if-else clause.
03:43If, open parenthesis, the fillType == linearGradient--make sure you put the
03:54capital G in the middle there in camel case and we'll put and we'll put an
03:58opening curly brace afterwards and put our conditions within that--
04:01if that's true, we want to set up a variable called gradient that will contain
04:07the linear gradient object.
04:09Well, this particular function is not one that's included in KineticJS.
04:13So for this, we're going to have to do it old school, go back to the actual
04:18Canvas built-in functionality and apply the create linear gradient method.
04:24That requires the context of the text, not the text object itself.
04:30So you may recall that much earlier in the course, we defined the text context
04:36for exactly this reason.
04:37I am going to enter in textContext. createLinearGradient, open and close parentheses.
04:47This takes two pairs of two arguments, basically the coordinates. So the first XY
04:54coordinate is where you want to start the gradient, and first we'll start it at 0,
04:59and then the Y coordinate is going to be textMiddle.
05:03Now, for the next set of coordinates, we want to position it at the end of the text.
05:07So how do we get the end of the text?
05:10Well, that's the width.
05:11So I'll enter theText.getWidth, open and close parentheses, comma, and again the Y
05:17coordinate will be textMiddle.
05:18Okay, let's close that line with a semicolon.
05:21Now we need to add the color stops.
05:24We'll add one for each of the colors we're using, and we'll pull the values for
05:29those colors from the color pickers
05:31textFillColor and textFillColor2.
05:34So I'll use the gradient object I just created;
05:37gradient.addColorStop, open parenthesis. addColorStop takes two arguments.
05:44The first is the position expressed in a value from 0 to 1.
05:48So I want this to start at the beginning, enter a 0, and then we'll specify as
05:53the color whatever value is in textFillColor.
05:57Okay, now we do the second one, and I am going to just do a quick copy-paste there.
06:02Now we want this color stop to be at the other end, so we'll enter a 1 here and
06:08make textFillColor textFillColor2.
06:11After we've set that all up, we're ready to set the fill.
06:14So again, we can go back to theText. setFill, and we'll fill it with gradient.
06:23So that takes care of the if condition.
06:25If it's not a linear gradient, I am going to put in an else statement, followed
06:30by open and close curly brace, and this is where we'll bring back in our
06:36setFill to textFillColor, our solid color fill.
06:41Okay, that's all we need for that one.
06:43Let's save it and then try it out.
06:45Let me hit Refresh. I'll enter some text here. Let's try smooth move.
06:52Press Tab. We scroll up a bit to see if it's there, there is it, maybe bring
06:59the size up somewhat.
07:00Now I found that a big thickish font works best for gradient color text.
07:05So I am going to choose Impact from my Text Font options.
07:08Now let me select Linear Gradient from the Fill Type list, and there it is, a
07:14smooth transition from our first color to the second.
07:17Of course, any color changes you make are instantly reflected in the text.
07:21Let's change this black to something more, let's go with green here.
07:26Make it bright green.
07:28That's pretty obnoxious, but there you are.
07:30It's working well, and now you're ready to add the next enhancements:
07:35radial gradients.
Collapse this transcript
Incorporating radial gradients
00:00A linear gradient, one that follows along a line from one color to another,
00:04doesn't require much explanation.
00:07But what's a radial gradient?
00:09As the name implies--not too clearly, but it's there--
00:12with a radial gradient we transition from one color to another along a radius.
00:18In this lesson, we'll insert our code so that the color in the first color
00:23picker, text color, is in the center ring of the text and the gradient color fills
00:28the outer ring. And of course there's a smooth transition between the two.
00:33Okay let's get to it.
00:34Now all of our infrastructure is already in place, thanks to adding the code for
00:39the linear gradient in the previous lesson.
00:42We only need to add the code to handle the condition if radial gradient is
00:47chosen from the fill type list.
00:48We can put all that code we need in an else-if block.
00:52I'm going to scroll down to where we have our linear gradient code, which starts
00:59on line 288 with a couple of variables, and I'm just going to inject this else-
01:04if clause right in front of the else clause.
01:12Now I have a matching set of curly braces so I'm good, and I'm ready to put in my if condition.
01:19So after the if statement, open and close parentheses, and if the fillType ==
01:28radialGradient, then we're going to need to change the definition of textMiddle.
01:36So I'm going to enter var textMiddle and set this to the center of the text by
01:44getting the height of the text and then dividing it by 2.
01:47So theText.getHeight divided by 2.
01:53Next, we want to set up our gradient object in a similar fashion to what we did
01:57in the linear gradient lesson previously,
02:00so var gradient = textContext.
02:06Again, this is not a function that KineticJS currently supports,
02:10so we're having to use the standard canvas operation which requires the
02:15textContext and not a text object.
02:18textContext.createRadialGradient, open and close parentheses. createRadialGradient
02:27takes two pairs of three arguments each:
02:30the x and y coordinates, followed by the x and y coordinates and a value for the radius.
02:37One set of argument will be applied to one color and the second set to the second color.
02:44So our XY coordinates for the first color are textCenter textMiddle--that will be
02:52dead-center in the text--and then I'll give it a radial value of 3. That's
02:56fairly small but noticeable.
02:58I don't want to make it too obvious.
03:01Next we'll keep the same x and y coordinates, so it's all coming from the same
03:06center area, textCenter, textMiddle, and the value we're going to enter in here
03:13will be the textCenter value -3.
03:16So it will be outside the radius of the other color. Okay, a semicolon
03:24to close this line. And now the next three lines are exactly the same as we use before,
03:30so I'm just going to go up to the linearGradient section and copy those.
03:36Well I spoke too quickly; they're not exactly the same.
03:39I could use one here, but it results in kind of a harsh transition.
03:44So I'm going to just put in .6 here to make it a little bit smoother. And with
03:49that one little bit of code, we're done.
03:51I'll save the file and head on over to the browser for a quick test.
03:56So here's my browser page. Let me hit refresh, enter in some new text.
04:05Again, I'll switch it to impact, and then let's size it up. And let's change Solid
04:11Color to Radial Gradient.
04:13Well, now it looks like all three of my color fill options are working just fine,
04:18and the user has a nice set of alternative for his or her caption coloring.
04:22We can of course change the colors to make them look a little bit nicer.
04:26This time let's change the first color, which is the center, and I'm going to go
04:31with something a little more yellowish.
04:35And now let's change the second color as well, and this time I'm going to go for more red to get
04:42more of a sunset effect.
04:44Now the project is basically done, but I have one final bit of code cleanup to take care of.
04:50In order to facilitate testing, I had commented out the bit of CSS that hid the
04:55caption controls until the user-clicked caption.
04:58So now I want to remove that.
05:00I'm going to go back to my code editor and open up styles main.css, scroll down
05:09to around line 165--there it is--and remove this comment line here and its
05:17companion, and now the text form will be hidden initially.
05:21I'll save the CSS file, and let's go verify that it's working correctly.
05:25One more refresh and as expected, I'm on the modify page and I only see my two
05:34figures here, so let's adjust those, properly. And now I'm ready to add a
05:39caption, and there are all my caption controls. Let's change the text to
05:51something meaningful. I'll change the font to fantasy and let's size it up a little bit here.
05:58I think I'll go with a linear gradient and a red-to-yellow effect, and let's
06:08change the shadow color, and maybe drag it down just as tad here so I can take a
06:15look. That looks pretty good. Just tighten it up a little bit, and that's the
06:22completed HTML5 Project Photo Card Titles.
06:25Congratulations!
Collapse this transcript
Goodbye
Next steps
00:00I hope you've got a lot out of HTML5 Projects:
00:03Photo Card Titles and had as much fun as I did in putting it together.
00:08If you haven't checked out the companion HTML5 project, Customized Photo Cards,
00:12give it a look-see right here on lynda.com.
00:16You have a great day.
Collapse this transcript


Suggested courses to watch next:

HTML5 Projects: Customized Photo Cards (44m 20s)
Joseph Lowery


CSS: Core Concepts (8h 49m)
James Williamson

HTML5 Projects: Engaging Ecommerce (37m 29s)
Joseph Lowery


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 104,069 instructional videos.

get started learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 2,024 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.

preview image of new course page

Try our new course pages

Explore our redesigned course pages, and tell us about your experience.

If you want to switch back to the old view, change your site preferences from the my account menu.

Try the new pages No, thanks

site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked