4oln4V7wRxyJZ-Fe8Kccaw_433a908e0e4748399733fd99fe66e856_Programming-Exercise---An-Interactive-Paint-Canvas
4oln4V7wRxyJZ-Fe8Kccaw_433a908e0e4748399733fd99fe66e856_Programming-Exercise---An-Interactive-Paint-Canvas
Before doing this assignment, you should try the short examples in the Reading: Try It! Inputs
and Events.
In this programming exercise you will use CodePen to write an interactive Paint canvas for
which you will be able to draw pictures, resize the canvas and clear the canvas.
Getting Started
You can go to this codepen site that has some JavaScript starter code and make a copy of it by
using the Fork button in the bottom right corner of your browser window.
https://codepen.io/shrodger/pen/jOqOKoL
Or create a new CodePen pen and copy the JavaScript code from below into the JavaScript
part to get started. After putting in the starter code below, the webpage will still be blank. You
will be writing most of the code for this assignment.
This starter code is NOT complete and may show an error saying TypeError: the paintcanvas
is null. You will need to add code to make that error message go away.
function clearCanvas () {
context.clearRect(0, 0, paintcanvas.width, paintcanvas.height);
}
The JavaScript code you are given starts with several globally defined variables that will be
used by many of the functions and some of the functions may change their values. Those
variables are:
You are also given three complete functions and one empty function
1) The function setWidth is empty. You will fill it in later to change the width of the
canvas.
2) The function clearCanvas() clears the canvas by erasing a rectangle the entire size
of the canvas.
3) The function paintcircle(x,y) paints a circle at location (x,y) on the canvas that is
of color the global variable color, and has radius the global variable radius.
4) The function isNumeric(value) returns true if the parameter value is a numeric
value and returns false if it is not. For example isNumeric(“100”) would return true
and isNumeric(“300px”) would return false. You will use this function to determine if
the user typed in a valid number.
1. Let's start by adding a title to the page and a canvas element. In the HTML part:
a. Add an <h1> header with words such as Paint Your Own Picture.
b. Add a canvas element with id=”canvas1”. You should not be able to see the
canvas yet. There should be a separate end canvas tag </canvas>.
2. Let’s put a border around the canvas so we can see it. In the CSS part:
Define a canvas style rule that defines the canvas border as 1px and a grayish
color:
canvas {
MISSING LINE HERE
}
Now you should be able to see your header and the outline of the canvas as:
5. Now let’s start painting. You will need to add some functions to the JavaScript window.
a. Add the function named startPaint() that has no parameters. This function
just needs to set the variable named isPainting to true to indicate it is time to
start painting.
b. Add the function named endPaint that has no parameters. This function ends
the capability to paint by setting isPainting to false.
c. Add the function named doPaint that has two parameters named x and y . This
function checks to see if it is ok to paint (it is ok to paint if the variable
isPainting has the value true) and if so it draws a circle at position x and y,
with radius set to radius and color set to color. Hint: Call the paintCircle
function.
6. Now add mouse control to the canvas so you can paint. In the HTML window inside the
<canvas > tag add the following:
a. Add onmousedown attribute to start painting when you click the mouse by giving
it the value to call the startPaint() function. That is, you should add this
attribute inside the <canvas> tag already there, right after the id attribute so it
looks like:
<canvas id="canvas1" onmousedown="startPaint()" >
b. Add onmousemove attribute so painting continues as the mouse moves. It
should have the value of calling the doPaint() function and pass the two
arguments event.offsetX and event.offsetY which are the current
position of the mouse within the canvas no matter where it is positioned on the
web page. This should also be inside the same <canvas> tag.
c. Now try clicking and moving the mouse inside the canvas. You can draw a big
black circle over and over. Note you can’t stop it. You will fix that next.
d. Add onmouseup attribute to end the painting when you let go of the mouse. You
should give it the value of calling the endPaint() function. This should also be
inside the same <canvas> tag.
e. Now try clicking and dragging the mouse and letting go of it and it should stop.
f. Here is a picture of painting the big black circle several times.
7. It would be nice to paint with different colors. To do this add the following.
a. In the JavaScript window, add the function named changeColor that has one
parameter named newColor. This function should change the variable named
color to the value newColor.
b. In the HTML window, below the canvas, add a paragraph <p> </p> and inside
the paragraph add the following:
i. Put the words: Brush color
ii. Right after that, add another <input /> tag with the values inside of the tag
1. type should be set to “color”
2. value should be set to “#000”
3. onchange should call the function setColor with argument
this.value, and the call should all be in quotes.
c. Now select a different color and paint. Then another color and paint on top. Here
is an example of painting with pink and green.
8. Now let’s change the size of the circle we are painting by adding a slider.
a. In the JavaScript window, add the function named resizeBrush that has one
parameter named newSize. This function should
i. Set the variable named radius t o the value newSize
ii. Set document.getElementById("sizeOutput").value to
newSize
b. In the HTML window, in the <p> where we added the color button, right after the
color button and still inside the <p> </p> add:
i. Put the words: Brush size:
ii. Then add another <input /> tag with the values inside of the tag
1. type should be set to “range”, that makes it a range slider.
2. id should be set to “sizeInput”
3. min should be set to “1”
4. max should be set to “50”
5. value should be set to “50”
6. oninput should call the function resizeBrush with argument
this.value, and the call should all be in quotes.
c. Try painting with different colors now. You should see something like this:
9. Let's add an output to show the value of the slider.
a. Right after the end of the tag <input /> from the last step and still in <p> </p>,
add an <output> start and end tag around the number 50, and inside the
<output > tag set the id to “sizeOutput”. It should look like the following:
<output id="sizeOutput">50</output>
b. Now move the slider and you should see the value change, getting smaller as
you move the slider to the left and larger as you move the slider to the right.
c. It may look like this:
10. Let’s add a button to clear the canvas so you can start painting again.
a. In the HTML window pane at the bottom, add the following:
i. Add in a new paragraph with tags <p> and </p>
ii. Between the starting and ending <p> tags, add an < input /> button with
the following values inside this tag:
1. The type is set to “button”
2. The id is set to “clear”
3. The value is set to “Clear Canvas”
4. The onclick is set to call the function “clearCanvas()”, a
function we have provided for you that erases a rectangle the
entire size of the canvas
b. You should now see the Clear Canvas button and after drawing you should be
able to clear the canvas. Your webpage might look like this:
Now you have an awesome interactive Paint program!