关键词搜索

源码搜索 ×
×

Web前端笔记-画布拖动及放缩(two.js)

发布2020-07-02浏览1701次

详情内容

程序运行截图如下:

结构图如下:

关键代码如下:

界面调用

HelloWorld.vue

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

js关键代码如下:

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

test1.js

  1. ;
  2. import * as Two from "JS/two";
  3. import * as $ from "JS/jquery"
  4. export function demo1(){
  5. let elem = document.getElementById('draw-shapes');
  6. let params = {
  7. type: Two.Types['webgl'],
  8. fullscreen: true,
  9. autostart: true
  10. }
  11. let two = new Two(params).appendTo(elem);
  12. let circle = two.makeCircle(0, 0, 50);
  13. let rect = two.makeRectangle(2000, 2000, 100, 100);
  14. circle.fill = '#FF8000';
  15. circle.stroke = 'orangered';
  16. circle.linewidth = 5;
  17. rect.fill = 'rgb(0, 200, 255)';
  18. rect.opacity = 5;
  19. rect.noStroke();
  20. two.update();
  21. let zui = new Two.ZUI(two.scene);
  22. zui.addLimits(0.06, 8);
  23. let $stage = $(two.renderer.domElement);
  24. $stage.bind('mousewheel wheel' , function (event){
  25. let e = event.originalEvent;
  26. e.stopPropagation();
  27. e.preventDefault();
  28. let dy = (e.wheelDeltaY || -e.deltaY) / 1000;
  29. zui.zoomBy(dy, e.clientX, e.clientY);
  30. });
  31. let isPressed = false;
  32. let originalPositionX = 0;
  33. let originalPositionY = 0;
  34. $stage.bind('mouseup', function(event){
  35. isPressed = false;
  36. });
  37. $stage.bind('mousemove', function(event){
  38. if(isPressed){
  39. let boolX = event.clientX - originalPositionX;
  40. let boolY = event.clientY - originalPositionY;
  41. zui.graphicMove(boolX, boolY);
  42. originalPositionX = event.clientX;
  43. originalPositionY = event.clientY;
  44. }
  45. });
  46. $stage.bind('mousedown', function(event){
  47. isPressed = true;
  48. originalPositionX = event.clientX;
  49. originalPositionY = event.clientY;
  50. });
  51. }

 

相关技术文章

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

提示信息

×

选择支付方式

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