How to Use the HTML5 Canvas API to control the scaling and transformation of images,

Source: Internet
Author: User
Tags image line

How to Use the HTML5 Canvas API to control the scaling and transformation of images,

The scale (sx, sy) parameter is used to specify two parameters: the horizontal direction and the vertical direction. For example, context. scale (2, 2) is to double the image size. In fact, it looks simple and there are still some problems in actual use. Let's look at a piece of code:

Copy the content to the clipboard using JavaScript Code
  1. <! DOCTYPE html>
  2. <Html lang = "zh">
  3. <Head>
  4. <Meta charset = "UTF-8">
  5. <Title> scaling </title>
  6. <Style>
  7. Body {background: url ("./images/bg3.jpg") repeat ;}
  8. # Canvas {border: 1px solid # aaaaaa; display: block; margin: 50px auto ;}
  9. </Style>
  10. </Head>
  11. <Body>
  12. <Div id = "canvas-warp">
  13. <Canvas id = "canvas">
  14. Does your browser support Canvas ?! Just change one !!
  15. </Canvas>
  16. </Div>
  17. <Script>
  18. Window. onload = function (){
  19. Var canvas = document. getElementById ("canvas ");
  20. Canvas. width = 800;
  21. Canvas. height = 600;
  22. Var context = canvas. getContext ("2d ");
  23. Context. fillStyle = "# FFF ";
  24. Context. fillRect (0, 0, 800,600 );
  25. Context. strokeStyle = "red ";
  26. Context. lineWidth = 5;
  27. For (var I = 1; I <4; I ++ ){
  28. Context. save ();
  29. Context. scale (I, I );
  30. Context. strokeRect (50, 50, 150,100 );
  31. Context. restore ();
  32. }
  33. };
  34. </Script>
  35. </Body>
  36. </Html>

Running result:

In fact, scaling is very simple. What is a little complicated is how to make the mouse a center to zoom in or out. If the mathematical ry is not good, the calculation formula may not be clear.

Copy the content to the clipboard using JavaScript Code
  1. Canvas. onmousewheel = canvas. onwheel = function (event) {// chrome firefox browser compatibility
  2. Var pos = windowToCanvas (canvas, event. clientX, event. clientY );
  3. Event. wheelDelta = event. wheelDelta? Event. wheelDelta :( event. deltaY * (-40 ));
  4. If (event. wheelDelta> 0 ){
  5. ImgScale * = 2;
  6. ImgX = imgX * 2-pos.x;
  7. ImgY = imgY * 2-pos.y;
  8. } Else {
  9. ImgScale/= 2;
  10. ImgX = imgX * 0.5 + pos. x * 0.5;
  11. ImgY = imgY * 0.5 + pos. y * 0.5;
  12. }
  13. DrawImage ();
  14. }

At this time, the basic function is implemented. loading an image is similar to loading multiple images. Maintain the position and size of each image. Let's sort out the code below.

Copy the content to the clipboard using JavaScript Code
  1. Var canvas, context;
  2. Var img, // image object
  3. ImgIsLoaded, // whether the image is loaded;
  4. ImgX = 0,
  5. ImgY = 0,
  6. ImgScale = 1;
  7. (Function int (){
  8. Canvas = document. getElementById ('canvas ');
  9. Context = canvas. getContext ('2d ');
  10. LoadImg ();
  11. })();
  12. Function loadImg (){
  13. Img = new Image ();
  14. Img. onload = function (){
  15. ImgIsLoaded = true;
  16. DrawImage ();
  17. }
  18. Img. src = "map.jpg ";
  19. }
  20. Function drawImage (){
  21. Context. clearRect (0, 0, canvas. width, canvas. height );
  22. Context. drawImage (img, 0, 0, img. width, img. height, imgX, imgY, img. width * imgScale, img. height * imgScale );
  23. }
  24. Canvas. onmousedown = function (event ){
  25. Var pos = windowToCanvas (canvas, event. clientX, event. clientY );
  26. Canvas. onmousemove = function (event ){
  27. Canvas. style. cursor = "move ";
  28. Var pos1 = windowToCanvas (canvas, event. clientX, event. clientY );
  29. Var x = pos1.x-pos. x;
  30. Var y = pos1.y-pos. y;
  31. Pos = pos1;
  32. ImgX + = x;
  33. ImgY + = y;
  34. DrawImage ();
  35. }
  36. Canvas. onmouseup = function (){
  37. Canvas. onmousemove = null;
  38. Canvas. onmouseup = null;
  39. Canvas. style. cursor = "default ";
  40. }
  41. }
  42. Canvas. onmousewheel = canvas. onwheel = function (event ){
  43. Var pos = windowToCanvas (canvas, event. clientX, event. clientY );
  44. Event. wheelDelta = event. wheelDelta? Event. wheelDelta :( event. deltaY * (-40 ));
  45. If (event. wheelDelta> 0 ){
  46. ImgScale * = 2;
  47. ImgX = imgX * 2-pos.x;
  48. ImgY = imgY * 2-pos.y;
  49. } Else {
  50. ImgScale/= 2;
  51. ImgX = imgX * 0.5 + pos. x * 0.5;
  52. ImgY = imgY * 0.5 + pos. y * 0.5;
  53. }
  54. DrawImage ();
  55. }
  56. Function windowToCanvas (canvas, x, y ){
  57. Var bbox = canvas. getBoundingClientRect ();
  58. Return {
  59. X: x-bbox. left-(bbox. width-canvas. width)/2,
  60. Y: y-bbox. top-(bbox. height-canvas. height)/2
  61. };
  62. }

Notes for scaling and Transformation
After reading the above example, you must be surprised at the result. First, the coordinates of the vertices in the upper left corner have changed, but the line width has also changed. Therefore, there are two problems with scaling and Transformation:

The coordinates in the upper left corner of the image are also scaled.
When scaling, the image line width also scales.
For example, for the smallest original rectangle, the coordinates in the upper left corner are (50, 50) and the line width is 5 PX. But after the enlargement is 2 times, the coordinates in the upper left corner are (100,100 ), the line width is changed to 10px. This is the side effect of scaling.

The children's shoes are definitely looking forward to my suggestions on how to solve the side effects. Unfortunately, there is no good way to solve these side effects. If you want to fix the zoom in and out of the upper left corner, you can change the coordinates in the upper left corner to (0, 0). In this way, no matter what the number is, 0 is multiplied by it or 0, so it remains unchanged. If you do not want to change the line width, do not use lines. Or encapsulate a function by yourself. Do not use scale ().

We have mentioned that translation transformation, Rotation Transformation, and scaling transformation all belong to coordinate transformation, or canvas transformation. Therefore, scaling is not about the image, but the entire coordinate system and the entire canvas! It is like scaling the unit distance of the coordinate system, so the coordinates and lines are scaled. Think about it, it seems amazing.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.