(try adding using css to add some space between the bottom of the art div and the reset button, this can be done using Margins)

Similar documents
Let s create a grid of pixels that you can use for creating pixel art. CSS provides table styles for grid and table layouts.

ORB Education Quality Teaching Resources

Programming Lab 1 (JS Hwk 3) Due Thursday, April 28

Web Programming and Design. MPT Junior Cycle Tutor: Tamara Demonstrators: Aaron, Marion, Hugh

Tutorial 2 Editor Brackets

ORB Education Quality Teaching Resources

In this project, you ll learn how to create your own webpage to tell a story, joke or poem. Think about the story you want to tell.

Project 2: After Image

HTML/CSS Lesson Plans

Want to add cool effects like rollovers and pop-up windows?

Controlled Assessment Task. Question 1 - Describe how this HTML code produces the form displayed in the browser.

JS Lab 1: (Due Thurs, April 27)

DOCMAIL: DATA INTELLIGENCE. Adding Data-driver styles and images

Mouse-over Effects. Text based buttons

Week 8 Google Maps. This week you ll learn how to embed a Google Map into a web page and add custom markers with custom labels.

Using Dreamweaver CC. 6 Styles in Websites. Exercise 1 Linked Styles vs Embedded Styles

HTML and CSS a further introduction

Excel Basics: Working with Spreadsheets

CSCI 201 Google Chrome DevTools

CoderDojo Activity Cards: The Cards (Animation2.html): How to use: Student comes to mentor, and together they choose a card to do next.

Before you begin, make sure you have the images for these exercises saved in the location where you intend to create the Nuklear Family Website.

Proper_Name Final Exam December 21, 2005 CS-081/Vickery Page 1 of 4

Page Layout Using Tables

HTML4 TUTORIAL PART 2

Using Dreamweaver CC. Logo. 4 Creating a Template. Page Heading. Page content in this area. About Us Gallery Ordering Contact Us Links

JavaScript: Tutorial 5

Section 1. How to use Brackets to develop JavaScript applications

Guidelines for doing the short exercises

Multiple Variable Drag and Drop Demonstration (v1) Steve Gannon, Principal Consultant GanTek Multimedia

Title and Modify Page Properties

CONVERSION TRACKING PIXEL GUIDE

Chapter 8: Using Toolbars

Beginning HTML. The Nuts and Bolts of building Web pages.

Using Microsoft Word. Tables

Using Dreamweaver CC. 5 More Page Editing. Bulleted and Numbered Lists

Tutorial 5. Website - Create a folder on the desktop called tutorial 5. Editor Brackets. Goals. Create a website showcasing the following techniques

Header. Article. Footer

Welcome Back! Without further delay, let s get started! First Things First. If you haven t done it already, download Turbo Lister from ebay.

In this project, you ll create a set of linked rooms where each room is a different web page which you can decorate with HTML.

Class 3 Page 1. Using DW tools to learn CSS. Intro to Web Design using Dreamweaver (VBUS 010) Instructor: Robert Lee

Word: Print Address Labels Using Mail Merge

Using Dreamweaver CS6

Enlargeit! Version 1.1 Operation Manual

Dreamweaver MX The Basics

How to lay out a web page with CSS

Tutorial 4. Activities. Code o Editor: Expression Web o Focus : Base Layout, navigation with folders, external stylesheets, Open up Expression Web

Developing Apps for the BlackBerry PlayBook

Building a Complex Application: Customer Tracker. Table of Contents. Description. Design Features Demonstrated in this Application

MailChimp mailer integration with Bitrix24. About the App. Before you ll start to use the App, you should sign up to Bitrix24 (

Keep Track of Your Passwords Easily

Getting started with Altmetric badges

Things to note: Each week Xampp will need to be installed. Xampp is Windows software, similar software is available for Mac, called Mamp.

Dreamweaver CS6. Level 1. Topics Workspaces Basic HTML Basic CSS

INTRODUCTION TO JAVASCRIPT

USING DRUPAL. Hampshire College Website Editors Guide

PYTHON YEAR 10 RESOURCE. Practical 01: Printing to the Shell KS3. Integrated Development Environment

HTML version of slides:

Using Dreamweaver CS6

ATMS ACTION TRACKING MANAGEMENT SYSTEM. Quick Start Guide. The ATMS dev team

1. Please, please, please look at the style sheets job aid that I sent to you some time ago in conjunction with this document.

RC Justified Gallery User guide for version 3.2.X. Last modified: 06/09/2016

Rico AjaxEngine Tutorial

Improved Web Development using HTML-Kit

Burning CDs in Windows XP

Developing Apps for the BlackBerry PlayBook

FolderView DMXzone.com Folder View 2 Manual

Bonus Lesson: Working with Code

Wanted! Introduction. Step 1: Styling your poster. Activity Checklist. In this project, you ll learn how to make your own poster.

Gdes2000 Graphic Design and the Internet

Flux Quickstart Guide

How to Rescue a Deleted File Using the Free Undelete 360 Program

CS1046 Lab 4. Timing: This lab should take you 85 to 130 minutes. Objectives: By the end of this lab you should be able to:

Using Adobe Contribute 4 A guide for new website authors

CSS worksheet. JMC 105 Drake University

Rockablepress.com Envato.com. Rockable Press 2010

A Step-by-Step Guide to getting started with Hot Potatoes

Learn Dreamweaver CS6

You will be writing code in the Python programming language, which you may have learnt in the Python module.

HTML5 Responsive Notify 2 DMXzone

ENGL 323: Writing for New Media Repurposing Content for the Web Part Two

GoLive will first ask you if your new site will be for one individual or a work group; select for a Single User, and click Next.

FolderView DMXzone.com FolderView Manual

Advanced Web Tutorial 1 Editor Brackets / Visual Code

c122jan2714.notebook January 27, 2014

Using Dreamweaver CC. 3 Basic Page Editing. Planning. Viewing Different Design Styles

PlayerLync Forms User Guide (MachForm)

Welcome to Book Display Widgets

Making use of other Applications

Title and Modify Page Properties

Jump to: Using AAUP Photos AAUP Logos Embedding the AAUP Twitter Feed Embedding the AAUP News Feed CREATING A WEBSITE

To illustrate how to set TAG styles the <body> and <h1> tags (for the BODY and the HEADING 1 styles) will be adjusted.

CS Multimedia and Communications. Lab 06: Webpage Tables and Image Links (Website Design part 3 of 3)

Course Exercises for the Content Management System. Grazyna Whalley, Laurence Cornford June 2014 AP-CMS2.0. University of Sheffield

Techniques for Optimizing Reusable Content in LibGuides

Microsoft Expression Web Quickstart Guide

Using Dreamweaver. 1 Overview. About the Web. About Dreamweaver

E-Business Systems 1 INTE2047 Lab Exercises. Lab 5 Valid HTML, Home Page & Editor Tables

Client Side JavaScript and AJAX

CS Multimedia and Communications REMEMBER TO BRING YOUR MEMORY STICK TO EVERY LAB!

EXCEL + POWERPOINT. Analyzing, Visualizing, and Presenting Data-Rich Insights to Any Audience KNACK TRAINING

Transcription:

Pixel Art Editor Extra Challenges 1. Adding a Reset button Add a reset button to your HTML, below the #art div. <div id= art > Pixels go here </div> <button>reset</button> The result should look something like this: (try adding using css to add some space between the bottom of the art div and the reset button, this can be done using Margins) Add a Javascript function to reset all of the pixels to have a white background. In your script.js file add the following: function clearpixels(){ var pixels = document.getelementsbyclassname( pixel ); for(var i = 0; i < pixels.length; i++){ pixels[i].style.backgroundcolor = white ;

Finally we need to trigger this function from the reset button. So we add the onclick attribute to our HTML like so: <button onclick= clearpixels() >Reset</button> We can now test this by adding some colour to the grid and then clicking the reset button.

Explaining the Code Let s take a look at what this code is actually doing. First of all we have this line: var pixels = document.getelementsbyclassname( pixel ); document refers to the current html page. The getelementsbyclassname function looks through the document for any elements which have a specified class. In this case we want to get all of the pixels on our page, these all contain the pixel css class, so the document.getelementsbyclassname( pixel ) will return an Array containing all of the pixel elements. We are then storing that Array into a new variable which we have decided to call pixels. This is so we can reuse it in future without having to type document.getelementsbyclassname every time. Now the next step is to do something with our list of pixels. In order to change each pixel we need to Loop through each item in the list and change each pixel in it individually. In this case we will be using a For Loop to do this. for(var i = 0; i < pixels.length; i++){ Code goes inside the loop Loops can initially look a bit complicated, so let s see what s actually going on here. To begin with we create a variable called i (this could be called anything, but i is a common convention used when writing a loop, it stands for index). In order to loop i requires a starting point, so we set it to be equal to 0. The next step is then to set an end point for the loop, when it will finish. In this case we want to go through the entire pixels array. Using pixels.length will tell us how many items are in the array. So we can say continue to loop while i is less than the number of pixels. Finally we can change the value of i at the end of each loop. We only want i to increase by 1 at the end of each loop so that the loop moves on to the next item. Writing i++ is just means add 1 to i. So essentially i will start at 0, then increase to 1 and then 2 etc etc until the loop finishes.

Finally, we update the backgroundcolor style on each pixel to set it back to white. pixels[i].style.backgroundcolor = white; This selects an item from the pixels array which has an index equal to i. So when i = 0 it will select the first pixel in the array. On the next loop i will equal 1, so it will select the next item in the array. Again the next loop i will equal 2 and so the third pixel will be selected, and so on until all the pixels in the array have had their backgroundcolor reset. Extra info For extra reading on how Arrays and Loops work see the following pages: Arrays - https://www.w3schools.com/js/js_arrays.asp Loops - https://www.w3schools.com/js/js_loop_for.asp

Populating the grid with loops As you may have noticed typing out the HTML for each row and pixel in the grid is slow and boring, and you can end up with accidental mistakes in the code. For these reasons Code Reuse is a huge part of programming, being able to write a small amount of code which can be used to do common tasks over and over without you as the coder having to change anything. In this example we will be looking at how we can reuse the same code to make the pixel art grid any size we want, while changing as little as possible. In order to do this we will be using more loops, just like we did in the previous challenge. Adding the variables First off, we want to tell our program how many rows we want, and how many pixels we want in each row. To do this we just add two variables in to our script.js file. var numrows = 5; var numpixels = 5; These can contain any number that you want, and they don t have to be the same! But in order to keep things simple to begin with we ll start with a 5x5 grid. Adding Rows Now we need a way to build up a list of all the rows we want. Ideally good code should be split up in to blocks, with each block trying to achieve one goal. In Javascript we call these blocks Functions. Functions are used to encourage Code Reuse, rather than typing out the same code again and again we can just call a Function and it will run all of the code that it contains. In this case we can create a Function which has the goal of adding all of the rows we need to the grid. function addrows(){ var rows = ; for(var i = 0; i < numrows; i++){ rows += <div class= row ></div> ; return rows; In this function we have three main things going on. Firstly, we create a rows variable to store the HTML we re generating. Secondly, we add a loop which will add some HTML to the rows variable until it reaches the end point (the end point being when i equals numrows, this means that we can change how many times the loop will run by changing the value of numrows). Finally, we return rows. Returning a value from a function means that we can use that value in other places outside of that function (hopefully this will become clearer in the next section).

Adding the grid to the art div In order to test the code that we have so far we need to add the rows to our art div in the HTML code. We can do this by adding another short function. function addgrid(){ var art = document.getelementbyid( art ); art.innerhtml = addrows(); We create a variable called art, we use getelementbyid to find the art div in our HTML and store it in the art variable. We can then use art.innerhtml to set what HTML goes inside the art div. <div id= art > Inner HTML will go here <div> In this case we are calling the addrows() function we created earlier, running any code inside that function and then setting the innerhtml to equal the returned value from that function. Finally all we need to do is call the addgrid function from inside our code. That will cause the grid to be created when our page starts up. We can do this by just adding addgrid(); at the bottom of our code. So far the full code should look something like this: var numrows = 5; var numpixels = 5; function addrows(){ var rows = ; for(var i =0; i < numrows; i++){ rows += <div class= row ></div> ; return rows; function addgrid(){ var art = document.getelementbyid( art ); art.innerhtml = addrows(); addgrid();

As you will see when you run this, the grid will currently be empty as we haven t added any pixels yet. However, using the developer tools built in to most browsers (right click -> inspect in Chrome) we can find the art div and see that it does actually contain the rows that we ve added. Adding Pixels In order to add Pixels to the grid we can essentially copy the addrows function from earlier, replacing the pixel html rather than the row. And making sure to use numpixels for the loop end point rather than numrows. function addpixels(){ var pixels = ; for(var i =0; i < numpixels; i++){ rows += <div class= pixel onclick= setpixelcolour(this) ></div> ; return pixels; The important part about this function is how we call it. We need to update the addrows function to call addpixels inside the row HTML. function addrows(){ var rows = ; for(var i = 0; i < numrows; i++){ rows += <div class= row > + addpixels() + </div> ; return rows; Notice how we ve added a call to the addpixels function inside the row HTML. This means that every time a row is added addpixels will be called which will add 5 pixels inside the row. Essentially this is using a loop inside another loop.

Refresh the page once these changes have been added and you should be able to see the full grid has now been created. Now if we inspect the code in the browser we can see that each row now contains 5 pixels. Try adjusting the values of numrows and numpixels and seeing what effect this has on the grid, why does this change happen?

Updating the grid from the Web Page At the moment we still need to change the values of numrows and numpixels in our code in order to change the size of the grid, but ideally we would like to be able to change this from our html page, without needing to touch the code. This is one of the cool things about Javascript, it allows you to change elements on the page and instantly see those changes on your site. Adding some number inputs The first step will be to add some number inputs to our html so that we can change the number of rows and pixels in each row. Try adding the following HTML code above the art div. Number of rows: <input id= numrows type= number value= 3 > Number of pixels per row: <input id= numrows type= number value= 3 > These won t do anything yet but if you visit your page you should see something like this: Remember that you can add your own styling via css if you want to change how the page looks. Updating the Javascript Rather than hard coding the values of numrows and numpixels in our code we can now set them to use the values from the number inputs we just added. We want this to happen any time the grid is changed so we can add this code to the addgrid function, and remove the existing numrows and numpixels variables. function addgrid(){ var numrows = document.getelementbyid( numrows ).value; var numpixels = document.getelementsbyid( numpixels ).value; var art = document.getelementbyid( art ); art.innerhtml = addrows();

Now that we have these values stored we need to actually use them in our addrows and addpixels functions. Unfortunately this leads us to a problem, variables that are created inside a function can not be used in other functions. This is called the scope of the variable. So our addrows function has no idea that the numrows variable exists, and can t use the value from it at the moment. In order to get around this we can use something called a parameter, which lets us pass an existing variable from one function to another. Adding parameters to the addrows function You ve probably noticed by now that all of our functions have a pair of brackets after the name, which don t seem to do anything. This is where we tell the function what parameters it can expect. Having a pair of empty brackets () after a function means that it doesn t need any paramters. So we need to change our addrows function to take two parameters, numrows and numpixels, so that we can use those values within that function. You can add these parameters inside the brackets, like this: function addrows(numrows, numpixels){ var rows = ; for(var i = 0; i < numrows; i++){ rows += <div class= row > + addpixels() + </div> ; return rows; The numrows variable in the loop now uses whatever value is passed in to the function in the numrows parameter. However this means that addrows must be given two parameters any time it is called so we need to update the addrows() call in the addgrid function. function addgrid(){ var numrows = document.getelementbyid( numrows ).value; var numpixels = document.getelementsbyid( numpixels ).value; var art = document.getelementbyid( art ); art.innerhtml = addrows(numrows, numpixels);

Adding a parameter to the addpixels function Notice how despite giving numpixels to the addrows function we aren t actually using it anywhere, so we also need to add that to the addpixels function, like so: function addpixels(numpixels){ var pixels = ; for(var i =0; i < numpixels; i++){ rows += <div class= pixel onclick= setpixelcolour(this) ></div> ; return pixels; And we also need to update the addpixels call in the addrows function: function addrows(numrows, numpixels){ var rows = ; for(var i = 0; i < numrows; i++){ rows += <div class= row > + addpixels(numpixels) + </div> ; return rows; So now the numpixels parameter is being passed from addrows to the addpixels function. Getting the grid to change The last thing we need to do is call the addgrid function from our html, to make the grid change when the numbers are changed. Earlier we were using the onclick event to call our Javacript, but this time we want to use the onchange event so that any time the numbers are changed the grid will update. Add the following to your html: Number of rows: <input id= numrows type= number value= 3 onchange= addgrid() > Number of pixels per row: <input id= numrows type= number value= 3 onchange= addgrid() > If you now open up your html file you should find that adjusting the numbers will change the size of the grid.