关键词搜索

源码搜索 ×
×

计算机图形学&Web前端笔记-图形平移放缩原理及实现(two.js鼠标事件适用所有渲染)

发布2020-07-10浏览1379次

详情内容

在two.js中,只提供了svg渲染时的鼠标事件,而canvas和webgl并没有提供,这样就对本人造成了很大的困扰,因此学习了下计算机图形学相关的知识,实现了利用two.js绘图在canvas、svg、webgl渲染方式下,放缩或移动场景,还能使用鼠标对其进行点击交互。

 

下面先演示下截图:

点击绿色的矩形:

从上面的图可以看出变色了。

下面滚动滚轮及拖动下场景:

同样点击刚刚那个矩形:

还是可以被找到,并且改变颜色。

 

下面先来说下2d图形中平移和放缩的原理(非常重要,下面的代码实现都需要用到):

平移:

放缩:

放缩+平移:

可见是先放缩,然后再进行平移。

总结下:

  1. a b c
  2. d e f
  3. g h i

其中a为:水平放缩;

其中c为:水平位移;

其中e为:垂直放缩;

其中f为:垂直位移。

 

下面来看下代码是如何实现的:

在two.js,two对象:

scene中存在_matrix里面存储了Float32Array(9),这里就对应了上面的3*3矩阵,上面的就是单位矩阵,

  1. 1 0 0
  2. 0 1 0
  3. 0 0 1

 

当放缩或移动后:

创建这几个矩形的代码:

代码如下:

  1. function createBtn(id, x, y, weight, color) {
  2. rect = two.makeRectangle(x, y, 200, 200);
  3. rect.noStroke();
  4. rect.fill = color;
  5. rect.myId = id;
  6. map.set(rect, weight);
  7. }
  8. 这些rect都存储在map中,如何在:
  9. $stage.bind('mousedown', function(event){
  10. isPressed = true;
  11. originalPositionX = event.clientX;
  12. originalPositionY = event.clientY;
  13. let x = event.clientX;
  14. let y = event.clientY;
  15. let letX = (rect._translation._x / 2 * (two.scene._matrix.elements[0]) + two.scene._matrix.elements[2]);
  16. let letY = (rect._translation._y / 2 * (two.scene._matrix.elements[4]) + two.scene._matrix.elements[5]);
  17. let letWidth = rect._width * two.scene._matrix.elements[0];
  18. let letHeight = rect._height *two.scene._matrix.elements[4];
  19. // if(x > letX &&
  20. // y > letY &&
  21. // x < letX + letWidth &&
  22. // y < letY + letHeight
  23. // ){
  24. //
  25. // console.log("find it");
  26. // }
  27. // console.log("坐标 x=" + x + " y=" + y);
  28. console.log(two);
  29. for(let value of map){
  30. let xOffset = value[0]._width / 2;
  31. let yOffset = value[0]._height / 2;
  32. // console.log("xOffset:" + xOffset);
  33. // console.log("yOffset:" + yOffset);
  34. // console.log(value[0])
  35. let letX = ((value[0]._translation._x - xOffset) * (two.scene._matrix.elements[0]) + two.scene._matrix.elements[2]);
  36. let letY = ((value[0]._translation._y - yOffset) * (two.scene._matrix.elements[4]) + two.scene._matrix.elements[5]);
  37. let letWidth = value[0]._width * two.scene._matrix.elements[0];
  38. let letHeight = value[0]._height * two.scene._matrix.elements[4];
  39. // console.log("id:" + value[0].myId);
  40. // console.log("letX:" + letX + " letY:" + letY);
  41. // console.log("letWidth: " + letWidth + " letHeight:" + letHeight);
  42. // console.log("");
  43. if(x > letX &&
  44. y > letY &&
  45. x < letX + letWidth &&
  46. y < letY + letHeight
  47. ){
  48. let r = Math.round(Math.random() * 255);
  49. let g = Math.round(Math.random() * 255);
  50. let b = Math.round(Math.random() * 255);
  51. let rgbStr = "rgb(" + r + "," + g + "," + b + ")";
  52. value[0].fill = rgbStr;
  53. console.log("find it " + value[0].myId);
  54. break;
  55. }
  56. }
  57. // console.log("---------------------------------");
  58. });

代码按下时对map进行遍历,如何将其进行转换后的坐标以及宽度进行矩阵变化即可:

这里有一点要说明的two.js中画矩形如果是从200, 200开始,那么这个200,200将会是矩形的

中心点,并不是左上角的点,所以这里有功offset.

 

鼠标拖动,滚轮放缩相关的代码:

  1. (function(Two){
  2. let _ = Two.Utils;
  3. let Surface = function(object) {
  4. this.object = object;
  5. };
  6. _.extend(Surface.prototype, {
  7. limits: function(min, max) {
  8. let min_exists = !_.isUndefined(min);
  9. let max_exists = !_.isUndefined(max);
  10. if (!max_exists && !min_exists) {
  11. return { min: this.min, max: this.max };
  12. }
  13. this.min = min_exists ? min : this.min;
  14. this.max = max_exists ? max : this.max;
  15. return this;
  16. },
  17. apply: function(px, py, s) {
  18. this.object.translation.set(px, py);
  19. this.object.scale = s;
  20. return this;
  21. }
  22. });
  23. let ZUI = Two.ZUI = function(group, domElement) {
  24. this.limits = {
  25. scale: ZUI.Limit.clone(),
  26. x: ZUI.Limit.clone(),
  27. y: ZUI.Limit.clone()
  28. };
  29. this.viewport = domElement || document.body;
  30. this.viewportOffset = {
  31. matrix: new Two.Matrix()
  32. };
  33. this.surfaceMatrix = new Two.Matrix();
  34. this.surfaces = [];
  35. this.reset();
  36. this.updateSurface();
  37. this.add(new Surface(group));
  38. };
  39. _.extend(ZUI, {
  40. Surface: Surface,
  41. Clamp: function(v, min, max) {
  42. return Math.min(Math.max(v, min), max);
  43. },
  44. Limit: {
  45. min: -Infinity,
  46. max: Infinity,
  47. clone: function() {
  48. let result = {};
  49. for (let k in this) {
  50. result[k] = this[k];
  51. }
  52. return result;
  53. }
  54. },
  55. TranslateMatrix: function(m, x, y) {
  56. m.elements[2] += x;
  57. m.elements[5] += y;
  58. return m;
  59. },
  60. PositionToScale: function(pos){
  61. return Math.exp(pos);
  62. },
  63. ScaleToPosition: function(scale){
  64. return Math.log(scale);
  65. }
  66. });
  67. _.extend(ZUI.prototype, {
  68. constructor: ZUI,
  69. add: function(surface){
  70. this.surfaces.push(surface);
  71. let limits = surface.limits();
  72. this.addLimits(limits.min, limits.max);
  73. return this;
  74. },
  75. addLimits: function(min, max, type) {
  76. type = type || 'scale';
  77. if (!_.isUndefined(min)){
  78. if(this.limits[type].min){
  79. this.limits[type].min = Math.max(min, this.limits[type].min);
  80. }
  81. else{
  82. this.limits[type].min = min;
  83. }
  84. }
  85. if(_.isUndefined(max)){
  86. return this;
  87. }
  88. if(this.limits[type].max){
  89. this.limits[type].max = Math.min(max, this.limits[type].max);
  90. }
  91. else{
  92. this.limits[type].max = max;
  93. }
  94. return this;
  95. },
  96. clientToSurface: function(x, y) {
  97. this.updateOffset();
  98. let m = this.surfaceMatrix.inverse();
  99. let n = this.viewportOffset.matrix.inverse().multiply(x, y, 1);
  100. return m.multiply.apply(m, _.toArray(n));
  101. },
  102. surfaceToClient: function(v) {
  103. this.updateOffset();
  104. let vo = this.viewportOffset.matrix.clone();
  105. let sm = this.surfaceMatrix.multiply.apply(this.surfaceMatrix, _.toArray(v));
  106. return vo.multiply.apply(vo, _.toArray(sm));
  107. },
  108. graphicMove: function(clientX, clientY){
  109. let dx = clientX;
  110. let dy = clientY;
  111. this.translateSurface(dx, dy);
  112. return this;
  113. },
  114. zoomBy: function(byF, clientX, clientY){
  115. let s = ZUI.PositionToScale(this.zoom + byF);
  116. this.zoomSet(s, clientX, clientY);
  117. return this;
  118. },
  119. zoomSet: function(zoom, clientX, clientY) {
  120. let newScale = this.fitToLimits(zoom);
  121. this.zoom = ZUI.ScaleToPosition(newScale);
  122. if (newScale === this.scale) {
  123. return this;
  124. }
  125. let sf = this.clientToSurface(clientX, clientY);
  126. let scaleBy = newScale / this.scale;
  127. this.surfaceMatrix.scale(scaleBy);
  128. this.scale = newScale;
  129. let c = this.surfaceToClient(sf);
  130. let dx = clientX - c.x;
  131. let dy = clientY - c.y;
  132. this.translateSurface(dx, dy);
  133. return this;
  134. },
  135. translateSurface: function(x, y) {
  136. ZUI.TranslateMatrix(this.surfaceMatrix, x, y);
  137. this.updateSurface();
  138. return this;
  139. },
  140. updateOffset: function() {
  141. let rect = this.viewport.getBoundingClientRect();
  142. _.extend(this.viewportOffset, rect);
  143. this.viewportOffset.left -= document.body.scrollLeft;
  144. this.viewportOffset.top -= document.body.scrollTop;
  145. this.viewportOffset.matrix
  146. .identity()
  147. .translate(this.viewportOffset.left, this.viewportOffset.top);
  148. return this;
  149. },
  150. updateSurface: function() {
  151. let e = this.surfaceMatrix.elements;
  152. for(let i = 0; i < this.surfaces.length; i++){
  153. this.surfaces[i].apply(e[2], e[5], e[0]);
  154. }
  155. return this;
  156. },
  157. reset: function() {
  158. this.zoom = 0;
  159. this.scale = 1.0;
  160. this.surfaceMatrix.identity();
  161. return this;
  162. },
  163. fitToLimits: function(s) {
  164. return ZUI.Clamp(s, this.limits.scale.min, this.limits.scale.max);
  165. }
  166. });
  167. })
  168. ((typeof global !== 'undefined' ? global : (this || window)).Two);

整个项目打包下载地址:

https://github.com/fengfanchen/frontUI/tree/master/mouseDemo

相关技术文章

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

提示信息

×

选择支付方式

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