New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

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

HTML5 Projects: Customized Photo Cards
Illustration by

Preparing the stage


From:

HTML5 Projects: Customized Photo Cards

with Joseph Lowery

Video: Preparing the stage

It's just about show time, and we have a user selected background, as well as a user contributed image. Before we can get into the actual combined image manipulation, however, we have to take care of a little bit of prep. The first of which is linking to a terrific library for all things canvas. I'll also show you how to use another code library to get the dimensions of the uploaded image. First, let's call in the cavalry and link to a few excellent resources. But first is a library of functions for working with canvas called KineticJS.

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
HTML5 Projects: Customized Photo Cards
44m 20s Intermediate Oct 31, 2012

Viewers: in countries Watching now:

The HTML5 Projects series puts HTML5, CSS3, and recent JavaScript API technologies to work—enhancing your web projects with interactivity and multimedia. This first installment shows you how to build an online application for creating personalized photo cards with user-uploaded imagery. Author Joseph Lowery shows how to create the form interface and introduces HTML5 features such as drag-and-drop file upload and interactive image manipulation.

Topics include:
  • Creating a form
  • Uploading images
  • Preparing the stage
  • Manipulating photos interactively
  • Saving photo cards
Subjects:
Design Web User Experience Web Design Projects
Software:
HTML JavaScript CSS
Author:
Joseph Lowery

Preparing the stage

It's just about show time, and we have a user selected background, as well as a user contributed image. Before we can get into the actual combined image manipulation, however, we have to take care of a little bit of prep. The first of which is linking to a terrific library for all things canvas. I'll also show you how to use another code library to get the dimensions of the uploaded image. First, let's call in the cavalry and link to a few excellent resources. But first is a library of functions for working with canvas called KineticJS.

KineticJS was created by Eric Rowell, who has done a most excellent job with the code, as well as documenting its features. We will be using his library to handle the core of our image manipulation. Let's open the code editor and link in the file. And I'm going to put it right after the link to main.css here, and it's a script tag of course, where the source is http:// and we're going to his site, which is html5canvastutorial.com/libraries/kinetic-v4.0.0.js. and now we close out the script tag, and that one is good to go.

Now one of the requirements of our image customization code is to get the dimensions of the uploaded image. For that we are going to turn to Denmark and bring in the work of Jacob Seidelin, which he calls the ImageInfo library. The ImageInfo script has a dependency on another JavaScript file, binaryajax.js. So we will link to that first. I will enter in the script tag and set the source = "scripts/binaryajax.js".

I've already downloaded this script and inserted it into my scripts folder. Likewise, I've also brought in the ImageInfo script. So let's link that in. So let's put these bad boys to work. I will create a new line and enter in another script tag, but this time we will be writing the code. So I will put in a type="text/javascript", make some room for my functions. So we will start this off with a window.onload function, and set that equal to a function, and within the function we will declare a series of variables.

The first is theBg, short for the background, and we will set theBg equal to the localStorage value of theChoice, which was the user's selected background. Next, we will declare another variable called theFile and make that equal to another localStorage value, this time for the uploadedFile. Now I am going to do a little file concatenation in order to make sure that the path to the file is included.

So let's start with theBg first and make that equal to the images folder followed by a leading slash, and we'll use our plus sign to concatenate it to the variable theBg. And we will do a similar thing for the file. This time, however, we will set its path to uploads and concatenate the file. Next, we are going to set up yet another variable called sources and this information will be passed into our custom Canvas JavaScript file.

So I will enter var sources and set that equal to an array where the first value is myBg, and instead of an equal sign here we use a colon, and that is of course assigned to theBg variable we set up. Follow that with a comma, and now the second source is myImage and that will be the foreground image that we'll be using and allowing the user to manipulate, and we set that to theFile. I will put a little closing semicolon there.

Now we are ready to move on to the second part of the information that we need to pass in to our Canvas JavaScript file, and those are the image dimensions. So for that I'll start a new line and enter in ImageInfo with an uppercase I in both cases, .loadInfo, again uppercase I, and we want to pass it two arguments, the file that we are going to examine and get the dimensions, so that's contained in the variable of theFile, and then the callback to the function that we are going to use in order to get those dimensions, and that is getDimensions.

Now we will set up getDimensions in just one second, but here I want to go ahead and take the information that we have and use loadImages, which again will pass information into our Canvas JavaScript file. So loadImages, and the arguments are sources, declared above, and initStage, and that's where that needs to go. Alright, we are ready for the final bit of preparation here, and that's to create the function getDimensions.

So again I am going to declare a couple of variables, one for the width and one for the height. First, we will start with theWidth, and we'll set that to an ImageInfo property called getField. And the field we want to get is located in theFile. That's the file that was uploaded. And the field we want to get is width. Now, I've found that in the current implementation of ImageInfo, it seems to be doubling the size of the actual images. So I am going to reduce that by half by putting in a slash, or divide by, 2, and then close out the function.

Let's do the same thing for theHeight, and we'll set that equal to ImageInfo.getField(theFile, "height"). And again, slash that in half. We have our variables, how are we going to pass them in? Well, there is that localStorage thing again. Let's use it. Here we will set the item, and we will call this imageWidth, and what we're going to put in there to store is of course the variable we just declared, theWidth. One last line.

Let's store the height in the imageHeight local storage unit, and we'll use, of course, the variable theHeight. I'm going to save the page, and now prepping is all done. We're ready to bring in the final script and tie it altogether.

There are currently no FAQs about HTML5 Projects: Customized Photo Cards.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed HTML5 Projects: Customized Photo Cards.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Notes cannot be added for locked videos.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

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.

Sign up and receive emails about lynda.com and our online training library:

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

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.