关键词搜索

源码搜索 ×
×

Web前端笔记-two.js图形旋转动画的2种实现方式

发布2020-07-14浏览1355次

详情内容

这里有两种方式!
第一种是使用setInterval:

代码如下:

  1. let time = setInterval(function(){
  2. if(sun.sun.rotation >= TWO_PI - 0.0625){
  3. sun.sun.rotation = 0;
  4. }
  5. sun.sun.rotation += (TWO_PI - sun.sun.rotation) * 0.0625;
  6. }, 300);

运行截图如下:

 

 

 

第二种是使用two.bind('update', function(frameCount){})

代码如下:

  1. two.bind('update', function (frameCount){
  2. if(sun.sun.rotation >= TWO_PI - 0.0625){
  3. sun.sun.rotation = 0;
  4. console.log("Hello");
  5. }
  6. sun.sun.rotation += (TWO_PI - sun.sun.rotation) * 0.0625;
  7. }).play();

运行截图如下:

第2种方式效果比第1种方式要好。

项目代码draw.js

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

图中那个太阳相关代码:

  1. ;
  2. export class SunElement{
  3. constructor({two, x, y, len}){
  4. this.x = x;
  5. this.y = y;
  6. this.two = two;
  7. this.len = len;
  8. this.render();
  9. }
  10. render() {
  11. let color = "rgba(255, 128, 0, 0.66)";
  12. let sun = this.two.makeGroup();
  13. let radius = this.len;
  14. let gutter = this.len / 5;
  15. let core = this.two.makeCircle(this.x, this.y, radius);
  16. core.noStroke();
  17. core.fill = color;
  18. sun.core = core;
  19. let coronas = [];
  20. let corona_amount = 16;
  21. for(let i = 0; i < corona_amount; i++){
  22. let pct = (i + 1) / corona_amount;
  23. let theta = pct * Math.PI * 2;
  24. let x = (radius + gutter) * Math.cos(theta);
  25. let y = (radius + gutter) * Math.sin(theta);
  26. console.log("x:" + x + " Math.cos(theta):" + Math.cos(theta) + " theta:" + theta);
  27. let corona = this.makeTriangle(gutter);
  28. corona.noStroke();
  29. corona.fill = color;
  30. corona.translation.set(x, y);
  31. corona.rotation = Math.atan2(-y , -x) + Math.PI / 2;
  32. coronas.push(corona);
  33. }
  34. sun.add(coronas);
  35. this.sun = sun;
  36. sun.translation.set(this.x, this.y)
  37. return sun;
  38. }
  39. makeTriangle(size){
  40. let tri = this.two.makePath(-size / 2, 0, size / 2, 0, 0, size);
  41. return tri;
  42. }
  43. }

 

 

相关技术文章

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

提示信息

×

选择支付方式

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