p5.js语法手册变形篇-applyMatrix()

说明

用于将作为参数给出的矩阵与当前矩阵相乘。这可用于同时执行平移、缩放、剪切和旋转操作。这是一个强大的操作,可用于轻松操纵场景中的对象。

范例

 

  1. function setup() {
  2. createCanvas(500, 100, WEBGL);
  3. frameRate(10);
  4. rectMode(CENTER);
  5. }
  6. function draw() {
  7. let step = frameCount % 20;
  8. background(200);
  9. // 案例1
  10. push()
  11. translate(-250, -50);
  12. applyMatrix(1, 0, 0, 1, 40 + step, 50);
  13. rect(0, 0, 50, 50);
  14. pop()
  15. // 案例2
  16. push()
  17. translate(-100, 0);
  18. applyMatrix(1 / step, 0, 0, 1 / step, 0, 0);
  19. rect(0, 0, 50, 50);
  20. pop();
  21. // 案例3
  22. push();
  23. translate(0, 0);
  24. noFill();
  25. rotateY(PI / 6);
  26. stroke(153);
  27. box(35);
  28. let rad = millis() / 1000;
  29. let ct = cos(rad);
  30. let st = sin(rad);
  31. applyMatrix(
  32. ct, 0.0, st, 0.0,
  33. 0.0, 1.0, 0.0, 0.0,
  34. -st, 0.0, ct, 0.0,
  35. 0.0, 0.0, 0.0, 1.0
  36. );
  37. noFill();
  38. stroke(255);
  39. box(50);
  40. pop();
  41. // 案例4
  42. push();
  43. translate(100, 0);
  44. let angle2 = map(step, 0, 20, -PI / 4, PI / 4);
  45. let shear_factor = 1 / tan(PI / 2 - angle2);
  46. applyMatrix(1, 0, shear_factor, 1, 0, 0);
  47. rect(0, 0, 50, 50);
  48. pop();
  49. // 案例5
  50. push()
  51. translate(200, 0);
  52. let angle = map(step, 0, 20, 0, TWO_PI);
  53. let cos_a = cos(angle);
  54. let sin_a = sin(angle);
  55. applyMatrix(cos_a, sin_a, -sin_a, cos_a, 0, 0);
  56. rect(0, 0, 50, 50);
  57. pop();
  58. }
  59. new p5();

 

语法

 
 
  1. applyMatrix(arr)
  2. applyMatrix(a, b, c, d, e, f)
  3. applyMatrix(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)

参数

arr

一个2*3 / 4*4 的矩阵

a

数字:定义要相乘的 2×3 矩阵的数字。

b

数字:定义要相乘的 2×3 矩阵的数字。

c

数字:定义要相乘的 2×3 矩阵的数字。

d

数字:定义要相乘的 2×3 矩阵的数字。

e

数字:定义要相乘的 2×3 矩阵的数字。

f

数字:定义要相乘的 2×3 矩阵的数字。

g

数字:定义要相乘的 4×4 矩阵的数字。

h

数字:定义要相乘的 4×4 矩阵的数字。

i

数字:定义要相乘的 4×4 矩阵的数字。

j

数字:定义要相乘的 4×4 矩阵的数字。

k

数字:定义要相乘的 4×4 矩阵的数字。

l

数字:定义要相乘的 4×4 矩阵的数字。

m

数字:定义要相乘的 4×4 矩阵的数字。

n

数字:定义要相乘的 4×4 矩阵的数字。

o

数字:定义要相乘的 4×4 矩阵的数字。

p

数字:定义要相乘的 4×4 矩阵的数字。

【社群】P5JS语法手册 - 乐述云享 (aleshu.com)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

one行feng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值