关键词搜索

源码搜索 ×
×

canvas笔记-图形变换(位移translate、缩放scale、变换矩阵transform)

发布2020-06-01浏览3898次

详情内容

这里首先演示下位移,这里使用的函数是translate(x, y)这个函数是叠加的,也就是说,当translate(100, 100),后再次调用translate(200, 200),那么最后圆点的基准值就为translate(300, 300)。

也就是说如果要实现某个图形的位移后,还要变回来。

代码如下:

  1. translate(100, 100);
  2. //TODO ... ...
  3. translate(-100, -100);

或者使用如下的方式:

  1. save(); //保存当前图形状态
  2. Restore(); //返回save前的状态

程序运行截图如下:

代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <canvas id="canvas" style="border: 1px solid #aaa; display: block; margin: 50px auto;">
  9. 当前浏览器不支持canvas
  10. </canvas>
  11. <script>
  12. window.onload = function(){
  13. let canvas = document.getElementById("canvas");
  14. canvas.width = 800;
  15. canvas.height = 800;
  16. let context = canvas.getContext("https://cdn.jxasp.com:9143/image/2d");
  17. context.fillStyle = "red";
  18. context.fillRect(0, 0, 400, 400);
  19. context.fillStyle = "blue";
  20. context.translate(100, 100);
  21. context.fillRect(0, 0, 400, 400);
  22. context.fillStyle = "green";
  23. context.translate(300, 300);
  24. context.fillRect(0, 0, 400, 400);
  25. }
  26. </script>
  27. </body>
  28. </html>

 

使用save()及restore()

程序运行截图如下:

源码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <canvas id="canvas" style="border: 1px solid #aaa; display: block; margin: 50px auto;">
  9. 当前浏览器不支持canvas
  10. </canvas>
  11. <script>
  12. window.onload = function(){
  13. let canvas = document.getElementById("canvas");
  14. canvas.width = 800;
  15. canvas.height = 800;
  16. let context = canvas.getContext("https://cdn.jxasp.com:9143/image/2d");
  17. context.fillStyle = "red";
  18. context.fillRect(0, 0, 400, 400);
  19. context.save();
  20. context.fillStyle = "blue";
  21. context.translate(100, 100);
  22. context.fillRect(0, 0, 400, 400);
  23. context.restore();
  24. context.save();
  25. context.fillStyle = "green";
  26. context.translate(300, 300);
  27. context.fillRect(0, 0, 400, 400);
  28. context.restore();
  29. }
  30. </script>
  31. </body>
  32. </html>

 

下面是关于scale,这里scale会把坐标,线条边框都扩大了,如下图:

源码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <canvas id="canvas" style="border: 1px solid #aaa; display: block; margin: 50px auto;">
  9. 当前浏览器不支持canvas
  10. </canvas>
  11. <script>
  12. window.onload = function(){
  13. let canvas = document.getElementById("canvas");
  14. canvas.width = 800;
  15. canvas.height = 800;
  16. let context = canvas.getContext("https://cdn.jxasp.com:9143/image/2d");
  17. context.save();
  18. context.scale(1, 1);
  19. context.strokeRect(50, 50, 200, 200);
  20. context.restore();
  21. context.save();
  22. context.scale(2, 2);
  23. context.strokeRect(50, 50, 200, 200);
  24. context.restore();
  25. context.save();
  26. context.scale(3, 3);
  27. context.strokeRect(50, 50, 200, 200);
  28. context.restore();
  29. }
  30. </script>
  31. </body>
  32. </html>

下面是关于变换矩阵transform。

这里有个基本概念:二维图像变换矩阵为3*3,三维图像变换矩阵为 4*4

关于二维图像变换矩阵为:

[

a c e

b d f

0 0 1

]

 

a表示水平缩放,默认值为1;

b表示水平倾斜,默认值为0;

c表示垂直倾斜,默认值为0;

d表示垂直缩放,默认值为1;

e表示水平位移,默认值为0;

f表示垂直位移,默认值为0。

 

从中可知,默认情况下变换矩阵为单位矩阵

[

1 0 0

0 1 0

0 0 1

]

如下使用单位矩阵变换的例子:

源码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <canvas id="canvas" style="border: 1px solid #aaa; display: block; margin: 50px auto;">
  9. 当前浏览器不支持canvas
  10. </canvas>
  11. <script>
  12. window.onload = function(){
  13. let canvas = document.getElementById("canvas");
  14. canvas.width = 800;
  15. canvas.height = 800;
  16. let context = canvas.getContext("https://cdn.jxasp.com:9143/image/2d");
  17. context.fillStyle = "red";
  18. context.strokeStyle = "#058";
  19. context.lineWidth = 5;
  20. context.save();
  21. context.transform(1, 0, 0, 1, 0, 0);
  22. context.fillRect(50, 50, 300, 300);
  23. context.strokeRect(50, 50, 300, 300);
  24. context.restore();
  25. }
  26. </script>
  27. </body>
  28. </html>

 

下面开始进行变换,程序运行截图如下:

源码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <canvas id="canvas" style="border: 1px solid #aaa; display: block; margin: 50px auto;">
  9. 当前浏览器不支持canvas
  10. </canvas>
  11. <script>
  12. window.onload = function(){
  13. let canvas = document.getElementById("canvas");
  14. canvas.width = 800;
  15. canvas.height = 800;
  16. let context = canvas.getContext("https://cdn.jxasp.com:9143/image/2d");
  17. context.fillStyle = "red";
  18. context.strokeStyle = "#058";
  19. context.lineWidth = 5;
  20. context.save();
  21. context.transform(1, 1, 0, 1, 0, 0);
  22. context.fillRect(50, 50, 300, 300);
  23. context.strokeRect(50, 50, 300, 300);
  24. context.restore();
  25. }
  26. </script>
  27. </body>
  28. </html>

同样这给transform也是叠加的,使用setTransform是把之前的变换都设置为单位矩阵,然后再进行缩放。

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载