关键词搜索

源码搜索 ×
×

Web前端笔记-圆环随时间逐渐缩小(使用two.js)

发布2020-07-14浏览979次

详情内容

这里给出效果图:

这里就给出截图,不搞gif了:

运行时:

这里主要是使用了setInterval去操作的,并且设置的timeout为0,这样的效果是比较好的。

关键代码如下:

画图相关文件:

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

鼠标滚轮及放缩相关:

  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咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

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