- restore ()
- translate ()
- rotate ()
- scale ()
More information: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Transformations
To study how to deal with transformations, I thought of the following example: A red and a green square should rotate independently in different directions.
We start with a red square which is at position (x = 50, y = 50) and has a height and width of 50px. I use the canvas function “rect”: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/rect
The square should now be inclined 5 degrees to the right. The canvas function “rotate” can be used for this, with which the canvas can be rotated around its origin.
Test different angles in the code box above by trying the values 10, 15, 20 etc. instead of 5! You will notice that the square rotates around the origin, i.e. around the top left corner.
In order for the square to correctly rotate “around itself”, the origin must be before rotation can be placed in the middle of the square, i.e. at the position (x = 75, y = 75). We use the canvas function “translate” for this. After calling “translate”, the point (x = 0, y = 0) is in the middle of the square and the top left corner of the square is at (x = -25, y = -25). The call “rect” for drawing the square must therefore be changed accordingly.
Now we have the problem that the “clearRect” function does not work properly if the canvas is rotated and its origin has been moved. This means that clearRect now also refers to the transformed canvas and not to our original canvas. We need the function so that the rectangle can be redrawn as often as desired with different settings, e.g. if you change the angle1 and then click "Execute" again. In an animation, too, the entire canvas area must be erased and redrawn in a certain millisecond interval.
To fix this, we use the “save” function to temporarily save the current state of the canvas (rotation and origin). After we have drawn our square, we restore the state with “restore”. The changes made with translate and rotate are then reset (which does not affect the square just drawn).
Now test the code for different angles:
I am now introducing a second, green square, which is also rotated by a certain angle (angle2).
It's not far to animation now. Two steps are still necessary:
- The function “rotateObjects” is no longer called just once, but repeatedly in a time interval of e.g. 10ms. I use “setInterval” to define the interval.
- With each interval call, the angles angle1 and angle2 must be slightly increased or decreased so that the effect of a rotation occurs.
Note: If you now click “Run” several times, problems will arise.
The whole file is available for download here: Dreh.html
- What has frustrated you the most recently
- How difficult is LASIK recovery
- Bearded dragons like to be handled
- Hermione goes back to Hogwarts
- If I could phishing
- What are some trending makeup styles
- What is a heat emergency
- Can stop iptables ddos and dos
- Is Israel's border wall effective?
- What do you remember about Charles Chips
- How do you make youtube videos
- How to make a paper tree
- How do you cure auditory nerve damage
- Class 9 lessons are necessary
- Why are love handles called love handles
- Why did the Church execute Galileo Galilei
- In which areas is C ++ still used?
- Was Duke Snider better than Jackie Robinson
- Why are narwhals endangered
- Canaries sing all year round
- What's wrong with our industrial society
- Ever question your atheism
- What does an HVAC service include
- When did the Romans take over Greece?
- Who uses more data Google or Facebook
- How do I achieve inner peace
- Vietnamese wear turbans
- Is the Magna Carta still legal today
- Why can't ETFs trade 24 7
- What should I study for web development?
- Which stores sell high quality clothing
- What is Jazakallahu Khairan
- Paracetamol can be used as a blood thinner
- LTE uses my data