关键词搜索

源码搜索 ×
×

Web前端笔记-移动端触屏移动视角(two.js)

发布2020-07-07浏览944次

详情内容

这里主要是利用jquery,在移动端时也可以实现视角的移动效果。

程序运行截图如下:

这里用触屏操作与鼠标操作一样:

关键源码如下:

  1. //移动端触碰开始
  2. $stage.bind('touchstart', function (event){
  3. originalPositionX = event.changedTouches[0].pageX;
  4. originalPositionY = event.changedTouches[0].pageY;
  5. isPressed = true;
  6. });
  7. $stage.bind('touchend', function(event){
  8. isPressed = false;
  9. });
  10. $stage.bind('touchmove', function(event){
  11. let currentX = event.changedTouches[0].pageX;
  12. let currentY = event.changedTouches[0].pageY;
  13. let boolX = currentX - originalPositionX;
  14. let boolY = currentY - originalPositionY;
  15. mouse.graphicMove(boolX, boolY);
  16. originalPositionX = currentX;
  17. originalPositionY = currentY;
  18. });
  19. //移动端触碰结束

程序结构图如下:

源码调用如下:

MainPage.vue

  1. <template>
  2. <div>
  3. <div id="draw-shapes"></div>
  4. </div>
  5. </template>
  6. <script>
  7. import "JS/two"
  8. import "JS/mouse"
  9. import {drawGraphic} from "JS/draw";
  10. export default {
  11. name: 'MainPage',
  12. mounted(){
  13. drawGraphic();
  14. },
  15. data () {
  16. return {
  17. msg: 'Welcome to Your Vue.js App'
  18. }
  19. }
  20. }
  21. </script>
  22. <style scoped>
  23. </style>

关键文件:draw.js

  1. ;
  2. import * as Two from "JS/two";
  3. import * as $ from "JS/jquery";
  4. let isPressed = false;
  5. let originalPositionX = 0;
  6. let originalPositionY = 0;
  7. let two;
  8. let mouse;
  9. export function drawGraphic(){
  10. let elem = document.getElementById("draw-shapes");
  11. let params = {
  12. fullscreen: true,
  13. autostart: true
  14. }
  15. two = new Two(params).appendTo(elem);
  16. mouse = new Two.ZUI(two.scene);
  17. mouse.addLimits(0.1, 10);
  18. let $stage = $(two.renderer.domElement);
  19. $stage.bind('mousewheel wheel', function(event){
  20. let e = event.originalEvent;
  21. e.stopPropagation();
  22. e.preventDefault();
  23. let dy = (e.wheelDeltaY || -e.deltaY) / 1000;
  24. mouse.zoomBy(dy, e.clientX, e.clientY);
  25. });
  26. $stage.bind('mouseup', function(event){
  27. isPressed = false;
  28. });
  29. $stage.bind('mouseout', function(event){
  30. isPressed = false;
  31. })
  32. $stage.bind('mousemove', function(event){
  33. if(isPressed){
  34. let boolX = event.clientX - originalPositionX;
  35. let boolY = event.clientY - originalPositionY;
  36. mouse.graphicMove(boolX, boolY);
  37. originalPositionX = event.clientX;
  38. originalPositionY = event.clientY;
  39. }
  40. });
  41. $stage.bind('mousedown', function(event){
  42. isPressed = true;
  43. originalPositionX = event.clientX;
  44. originalPositionY = event.clientY;
  45. });
  46. //移动端触碰开始
  47. $stage.bind('touchstart', function (event){
  48. originalPositionX = event.changedTouches[0].pageX;
  49. originalPositionY = event.changedTouches[0].pageY;
  50. isPressed = true;
  51. });
  52. $stage.bind('touchend', function(event){
  53. isPressed = false;
  54. });
  55. $stage.bind('touchmove', function(event){
  56. let currentX = event.changedTouches[0].pageX;
  57. let currentY = event.changedTouches[0].pageY;
  58. let boolX = currentX - originalPositionX;
  59. let boolY = currentY - originalPositionY;
  60. mouse.graphicMove(boolX, boolY);
  61. originalPositionX = currentX;
  62. originalPositionY = currentY;
  63. });
  64. //移动端触碰结束
  65. let text = two.makeText("Hello", 0, 0);
  66. text.size = 20;
  67. text.fill = "red";
  68. text.rotation = 90 * Math.PI / 180;
  69. }

放缩移动相关文件:mouse.js

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

提示信息

×

选择支付方式

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