关键词搜索

源码搜索 ×
×

Web前端笔记-2D图形平面内平移定位(two.js)

发布2020-07-14浏览1364次

详情内容

此处是在控制台中输入了window.mainPage.flyToPosition(-1000, 500),他是经过平移过去的。

整个坐标盘是这样的:

这里使用two.bind(‘update’, function(frameCount){})用于平移时的绑定跟新,当平移结束后,使用two.unbind(‘update’);

平移相关的代码:

  1. export function flyTo({x, y}){
  2. waterWave(x, y);
  3. //计算出目前中心点与x,y坐标的差值
  4. //当前屏幕中点 对应的 场景坐标
  5. let dot = getScreenOriginal();
  6. console.log("dot:" + dot);
  7. //屏幕中心点坐标
  8. let original = {
  9. x: 0,
  10. y: 0
  11. };
  12. original.x = two.width / 2;
  13. original.y = two.height / 2;
  14. let differenceValueX = (dot.x - x) * two.scene._matrix.elements[0];
  15. let differenceValueY = (dot.y - y) * two.scene._matrix.elements[4];
  16. console.log(two.scene._matrix.elements);
  17. console.log("差值:"+ differenceValueX + " " + differenceValueY);
  18. two.bind('update', function(frameCount){
  19. //判断方向
  20. if(differenceValueX >= 0){ //向左移动,x+
  21. if((differenceValueX <= 20 &&differenceValueX >= -20) && (differenceValueY) < 0){
  22. console.log("向正下方移动...");
  23. mouse.graphicMove(0, -10);
  24. differenceValueY += 10;
  25. }
  26. else if((differenceValueY >= -20 && differenceValueY <= 20)){
  27. console.log("向正左方向移动...");
  28. mouse.graphicMove(10, 0);
  29. differenceValueX -= 10;
  30. }
  31. else if(differenceValueY >=0){ //向上移动,y+
  32. console.log("向左上移动...")
  33. mouse.graphicMove(10, 10);
  34. differenceValueX -= 10;
  35. differenceValueY -= 10;
  36. }
  37. }
  38. else{ //向右移动 x-
  39. if(differenceValueY >=0 && (differenceValueX > -20 && differenceValueX < 20)) {
  40. console.log("向正上方移动...");
  41. mouse.graphicMove(0, 10);
  42. differenceValueY -= 10;
  43. }
  44. else if(differenceValueY < 0 && (differenceValueX > -20 && differenceValueX < 20)){ //垂直向下移动
  45. console.log("向正下方移动...");
  46. mouse.graphicMove(0, -10);
  47. differenceValueY += 10;
  48. }
  49. else if(differenceValueY > 0){ //向右上方移动
  50. mouse.graphicMove(-10, +10);
  51. differenceValueX += 10;
  52. differenceValueY -= 10;
  53. }
  54. else if(differenceValueX < 0 && (differenceValueY <= 20 && differenceValueY >= -20)){
  55. console.log("向正右方移动");
  56. mouse.graphicMove(-10, 0);
  57. differenceValueX += 10;
  58. }
  59. else if(differenceValueY < 0){
  60. console.log("向右下方移动...");
  61. mouse.graphicMove(-10, -10);
  62. differenceValueX += 10;
  63. differenceValueY += 10;
  64. }
  65. }
  66. // console.log("differenceValueX:" + differenceValueX + " differenceValueY:" + differenceValueY);
  67. if(differenceValueX < 20 && differenceValueX > -20 &&
  68. differenceValueY < 20 && differenceValueY > -20 ){
  69. // console.log("differenceValueX:" + differenceValueX + " differenceValueY:" + differenceValueY);
  70. console.log("update over");
  71. two.unbind('update');
  72. }
  73. }).play();
  74. }

画圆波纹相关代码:

  1. export function waterWave(x, y) {
  2. let repeat = 1000;
  3. let radius = 1000;
  4. let cir = two.makeCircle(x, y, radius);
  5. cir.noFill();
  6. cir.linewidth = 3;
  7. cir.stroke = "rgba(255, 255, 0, 0.5)";
  8. let radiusTime = setInterval(function(){
  9. radius -= 10;
  10. if(radius <= 0){
  11. radius = 1000;
  12. cir.radius = radius;
  13. }
  14. cir.radius = radius;
  15. }, 1);
  16. let time = setInterval(function(){
  17. if(repeat < 0){
  18. cir.remove();
  19. clearInterval(radiusTime);
  20. clearInterval(time);
  21. }
  22. repeat--;
  23. }, 0);
  24. }

把浏览器中心点转换为场景坐标点:

  1. //计算当前屏幕圆心 对应的 图形坐标
  2. function getScreenOriginal(){
  3. let original = {
  4. x: 0,
  5. y: 0
  6. };
  7. original.x = two.width / 2;
  8. original.y = two.height / 2;
  9. // console.log(two.scene._matrix.elements)
  10. //获取水平位移及垂直位移
  11. //将浏览器上界面坐标转换为two.js的场景坐标,也就是 cirX和cirY为当前界面中点的场景坐标
  12. let cirX = (original.x - two.scene._matrix.elements[2]) / two.scene._matrix.elements[0];
  13. let cirY = (original.y - two.scene._matrix.elements[5]) / two.scene._matrix.elements[4];
  14. console.log("当前圆心 cirX:" + cirX + " cirY:" + cirY);
  15. original.x = cirX;
  16. original.y = cirY;
  17. return original;
  18. }

 

相关技术文章

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

提示信息

×

选择支付方式

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