Main content
Computer programming - JavaScript and the web
Course: Computer programming - JavaScript and the web > Unit 4
Lesson 6: TransformationsScaling
The final coordinate system transformation is scaling, which changes the size of the grid. Take a look at this program, which draws a square, then scales the grid to twice its normal size, and draws it again.
First, you can see that the square appears to have moved. It hasn’t, of course. Its upper left corner is still at (20, 20) on the scaled-up grid, but that point is now twice as far away from the origin as it was in the original coordinate system.
If you wanted the large square to start at the same corner as the small square, you could translate first, and then scale:
You might also notice from both of those programs that the lines are thicker on the large squares. That’s no optical illusion—the lines really are twice as thick, because the coordinate system has been scaled to double its size. You could explicitly change the
strokeWeight()
to account for that, or avoid using scale()
all together.The
scale()
command optionally accepts two parameters instead of just one, so you can scale the x and y dimensions separately. Try using scale(3.0, 0.5)
in the above program to make the x dimension three times its normal size and the y dimension only half its normal size.This article is an adaptation of 2D Transformations by J David Eisenberg, used under a Creative Commons Attribution-NonCommercial-ShareAlike License.
Want to join the conversation?
- I'm having trouble on step 2 out of 2 on optical illusion can someone help me?(6 votes)
- I had the exact same issue, but here is my code and it works now.
while (scaleF > 0) {
pushMatrix();
translate(200,200);
scale(scaleF);
drawBlackCircle(diameter);
scaleF -= decreaseAmt;
popMatrix();
pushMatrix();
translate(200,200);
scale(scaleF);
drawWhiteCircle(diameter);
scaleF -= decreaseAmt;
popMatrix();(5 votes)
- The translation, the rotation, and the reflection are the three isometric transformations. (Isometric simply means that an object's size and shape is preserved.) Translations and rotations have both been covered. But, is there a reflection function in Java? If so, what command utilizes it?(7 votes)
- Reflection can be implemented with a combination of
translate
andscale
. See mymirrorImage
function in https://www.khanacademy.org/cs/images-50/1405420533
It is also a very simple algebraic computation (notranslate
orscale
) as explained in https://www.khanacademy.org/cs/mirrored-shapes/4772646007865344(6 votes)
- What is the keyboard shortcut for making a block of code into comment?
What is the keyboard shortcut for making a block of code moving left?
Thanks!(3 votes)- To comment or uncomment the current line, press
Ctrl+/
.
To decrease the indentation level of the currently highlighted lines, pressShift+Tab
.(10 votes)
- i m not getting the step 2 of optical illusion challenge.....please help...i have tried everything,but its not showing any type of error.(7 votes)
- I am stuck on
challenge : optical illusion.It keeps on saying the while loop is taking to long to run.What should I do to fix it?(3 votes)
- how do I make the screen move with scaling? (Ex: I want to see X: 500!)
Thanks a ton!(4 votes)- Do you want to adjust the view or resize the canvas? You can't resize the canvas, but you can move the view around: https://www.khanacademy.org/cs/infinite-scrolling-example/6261362609094656(4 votes)
- Is it true that if I wanted to make a program usable in any canvas size I could put this at the top of my program:
scale(width / 400, height / 400);
(4 votes)- That is correct. Be sure to consistently use
pushMatrix()
andpopMatrix()
instead ofresetMatrix()
when making other transformations so that the initial scale is not reset.(5 votes)
- how do you scale things down?(4 votes)
- Scale factors between -1 and 1 will scale things down. https://www.khanacademy.org/cs/simple-zooming/5374698393698304(2 votes)
- I just saw your fill() function had 4 numbers, how that you get that?(1 vote)
- You type it.
The fourth number is the color's alpha component, which means opacity here.
0 is fully transparent, 255 is fully opaque.(6 votes)
- This technique reminds me of vector graphics. Do built-in functions of ProcessingJS library (such as
rect()
,line()
...) draw vectors or just some calculated pixels?(5 votes)- In the end you will have pixels on your screen, I doubt that you are still working on a real [CRT vector display](http://en.wikipedia.org/wiki/Vector_monitor).
However ProcessingJS built-in functions draw these as vectors, the underlying canvas converts them to pixels to be displayed on the canvas. Doing that in JS would probably be unacceptably slow.(0 votes)
- So I just muddled my way through the Optical Illusion challenge... I did it with big help from the hint at the top of the page, but I didn't truly understand it all. If I was given a blank page, for example, I would not be able to repeat this exercise on my own.
Is this normal? I started learning to code in January, am I not absorbing this at the rate I should be??(2 votes)- It depends also on how much time you can spend per day.
Also after every new concept learned it would be good to make your own project of it. And everytime you got an idea to use something, try it out.
But don’t hurry. Make sure you understand everything before you move on.(3 votes)