Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Preparing the stage

From: HTML5 Projects: Customized Photo Cards

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.

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.

Show transcript

This video is part of

Image for HTML5 Projects: Customized Photo Cards
HTML5 Projects: Customized Photo Cards

12 video lessons · 5055 viewers

Joseph Lowery
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
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.


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.

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 preferences from 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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

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.