关键词搜索

源码搜索 ×
×

Web前端笔记-two.js实现坐标定位(动画效果非瞬移定位)

发布2020-07-14浏览1369次

详情内容

先来看下程序运行截图:

画图相关代码:

  1. ;
  2. import * as Two from "JS/two";
  3. import * as $ from "JS/jquery";
  4. let two;
  5. let mouse;
  6. let isPressed = false;
  7. let originalPositionX = 0;
  8. let originalPositionY = 0;
  9. let map = new Map();
  10. let rect;
  11. let TWO_PI = Math.PI * 2;
  12. export function drawGraphic(){
  13. let elem = document.getElementById("draw-shapes");
  14. let params = {
  15. type: Two.Types['canvas'],
  16. fullscreen: true,
  17. autostart: true
  18. };
  19. two = new Two(params).appendTo(elem);
  20. mouse = new Two.ZUI(two.scene);
  21. mouse.addLimits(0.01, 10);
  22. let $stage = $(two.renderer.domElement);
  23. $stage.bind('mousewheel wheel', function(event){
  24. let e = event.originalEvent;
  25. e.stopPropagation();
  26. e.preventDefault();
  27. let dy = (e.wheelDeltaY || -e.deltaY) / 1000;
  28. // console.log(e.clientX + " " + e.clientY)
  29. mouse.zoomBy(dy, e.clientX, e.clientY);
  30. });
  31. $stage.bind('mouseup', function(event){
  32. isPressed = false;
  33. });
  34. $stage.bind('mouseout', function(event){
  35. isPressed = false;
  36. });
  37. $stage.bind('mousedown', function(event){
  38. isPressed = true;
  39. originalPositionX = event.clientX;
  40. originalPositionY = event.clientY;
  41. let x = event.clientX;
  42. let y = event.clientY;
  43. for(let value of map){
  44. let xOffset = value[0]._width / 2;
  45. let yOffset = value[0]._height / 2;
  46. let letX = ((value[0]._translation._x - xOffset) * (two.scene._matrix.elements[0]) + two.scene._matrix.elements[2]);
  47. let letY = ((value[0]._translation._y - yOffset) * (two.scene._matrix.elements[4]) + two.scene._matrix.elements[5]);
  48. let letWidth = value[0]._width * two.scene._matrix.elements[0];
  49. let letHeight = value[0]._height * two.scene._matrix.elements[4];
  50. if(x > letX &&
  51. y > letY &&
  52. x < letX + letWidth &&
  53. y < letY + letHeight
  54. ){
  55. let rgbStr = getRandColor();
  56. value[0].fill = rgbStr;
  57. break;
  58. }
  59. }
  60. });
  61. $stage.bind('mousemove', function(event){
  62. if(isPressed){
  63. let boolX = event.clientX - originalPositionX;
  64. let boolY = event.clientY - originalPositionY;
  65. mouse.graphicMove(boolX, boolY);
  66. originalPositionX = event.clientX;
  67. originalPositionY = event.clientY;
  68. }
  69. });
  70. for(let i = -5000; i < 5000; i += 200){
  71. for(let j = -5000; j < 5000; j += 200){
  72. let rgbStr = getRandColor();
  73. createBtn(1001, i, j, 500, rgbStr);
  74. }
  75. }
  76. }
  77. function createBtn(id, x, y, weight, color) {
  78. rect = two.makeRectangle(x, y, 200, 200);
  79. rect.noStroke();
  80. rect.fill = color;
  81. rect.myId = id;
  82. map.set(rect, weight);
  83. }
  84. function getRandColor(){
  85. let r = Math.round(Math.random() * 255);
  86. let g = Math.round(Math.random() * 255);
  87. let b = Math.round(Math.random() * 255);
  88. let rgbStr = "rgb(" + r + "," + g + "," + b + ")";
  89. return rgbStr;
  90. }
  91. //计算当前屏幕圆心 对应的 图形坐标
  92. function getScreenOriginal(){
  93. let original = {
  94. x: 0,
  95. y: 0
  96. };
  97. original.x = two.width / 2;
  98. original.y = two.height / 2;
  99. // console.log(two.scene._matrix.elements)
  100. //获取水平位移及垂直位移
  101. //将浏览器上界面坐标转换为two.js的场景坐标,也就是 cirX和cirY为当前界面中点的场景坐标
  102. let cirX = (original.x - two.scene._matrix.elements[2]) / two.scene._matrix.elements[0];
  103. let cirY = (original.y - two.scene._matrix.elements[5]) / two.scene._matrix.elements[4];
  104. console.log("当前圆心 cirX:" + cirX + " cirY:" + cirY);
  105. original.x = cirX;
  106. original.y = cirY;
  107. return original;
  108. }
  109. export function flyToPosition(x, y){
  110. //当前屏幕中点 对应的 场景坐标
  111. let dot = getScreenOriginal();
  112. console.log("dot:" + dot);
  113. //屏幕中心点坐标
  114. let original = {
  115. x: 0,
  116. y: 0
  117. };
  118. original.x = two.width / 2;
  119. original.y = two.height / 2;
  120. let c = two.makeCircle(x, y, 10);
  121. c.fill = "red";
  122. let differenceValueX = (dot.x - x) * two.scene._matrix.elements[0];
  123. let differenceValueY = (dot.y - y) * two.scene._matrix.elements[4];
  124. console.log(two.scene._matrix.elements);
  125. console.log("差值:"+ differenceValueX + " " + differenceValueY);
  126. let currentStep = 0;
  127. let browX = 0;
  128. let browY = 0;
  129. let time = setInterval(function(){
  130. //先把scale设置为最小
  131. if(currentStep == 0){
  132. if(mouse.scale <= 0.02){
  133. currentStep = 1;
  134. console.log("over1");
  135. }
  136. else{
  137. mouse.zoomBy(-0.05, original.x, original.y)
  138. }
  139. }
  140. //当界面最小时,计算x,y,在屏幕上的坐标
  141. if(currentStep == 1){
  142. browX = (x * two.scene._matrix.elements[0]) + two.scene._matrix.elements[2];
  143. browY = (y * two.scene._matrix.elements[4]) + two.scene._matrix.elements[5];
  144. console.log("note: browX:" + browX + " browY:" + browY);
  145. currentStep = 2;
  146. }
  147. //慢慢放大
  148. if(currentStep == 2){
  149. if(mouse.scale >= 2){
  150. currentStep = 3;
  151. }
  152. else{
  153. mouse.zoomBy(0.05, browX, browY)
  154. }
  155. }
  156. if(currentStep == 3){
  157. console.log("over exit")
  158. clearInterval(time);
  159. }
  160. }, 0);
  161. //飞到对应x,y坐标点,这个x, y将会变成新的屏幕中心点
  162. //mouse.graphicMove(differenceValueX, differenceValueY);
  163. // originalPositionX = differenceValueX;
  164. // originalPositionY = differenceValueY;
  165. }

鼠标放缩拖动相关代码:

  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);

 

 

 

相关技术文章

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

提示信息

×

选择支付方式

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