关键词搜索

源码搜索 ×
×

Web前端笔记-two.js画三角形及画tip含tip旋转

发布2020-07-07浏览1027次

详情内容

程序运行截图如下:

旋转下:

代码如下:

  1. import * as Two from "JS/two";
  2. import * as $ from "JS/jquery";
  3. let isPressed = false;
  4. let originalPositionX = 0;
  5. let originalPositionY = 0;
  6. let two;
  7. let mouse;
  8. export function drawGraphic(){
  9. let elem = document.getElementById("draw-shapes");
  10. let params = {
  11. fullscreen: true,
  12. autostart: true
  13. }
  14. two = new Two(params).appendTo(elem);
  15. mouse = new Two.ZUI(two.scene);
  16. mouse.addLimits(0.1, 10);
  17. let $stage = $(two.renderer.domElement);
  18. $stage.bind('mousewheel wheel', function(event){
  19. let e = event.originalEvent;
  20. e.stopPropagation();
  21. e.preventDefault();
  22. let dy = (e.wheelDeltaY || -e.deltaY) / 1000;
  23. mouse.zoomBy(dy, e.clientX, e.clientY);
  24. });
  25. $stage.bind('mouseup', function(event){
  26. isPressed = false;
  27. });
  28. $stage.bind('mouseout', function(event){
  29. isPressed = false;
  30. })
  31. $stage.bind('mousemove', function(event){
  32. if(isPressed){
  33. let boolX = event.clientX - originalPositionX;
  34. let boolY = event.clientY - originalPositionY;
  35. mouse.graphicMove(boolX, boolY);
  36. originalPositionX = event.clientX;
  37. originalPositionY = event.clientY;
  38. }
  39. });
  40. $stage.bind('mousedown', function(event){
  41. isPressed = true;
  42. originalPositionX = event.clientX;
  43. originalPositionY = event.clientY;
  44. });
  45. //移动端触碰开始
  46. $stage.bind('touchstart', function (event){
  47. originalPositionX = event.changedTouches[0].pageX;
  48. originalPositionY = event.changedTouches[0].pageY;
  49. isPressed = true;
  50. });
  51. $stage.bind('touchend', function(event){
  52. isPressed = false;
  53. });
  54. $stage.bind('touchmove', function(event){
  55. let currentX = event.changedTouches[0].pageX;
  56. let currentY = event.changedTouches[0].pageY;
  57. let boolX = currentX - originalPositionX;
  58. let boolY = currentY - originalPositionY;
  59. mouse.graphicMove(boolX, boolY);
  60. originalPositionX = currentX;
  61. originalPositionY = currentY;
  62. });
  63. //移动端触碰结束
  64. let text = two.makeText("Hello", 0, 0);
  65. text.size = 20;
  66. text.fill = "red";
  67. text.rotation = 90 * Math.PI / 180;
  68. //查询
  69. let corona = makeTriangle(two, 100);
  70. corona.noStroke();
  71. corona.fill = "red";
  72. corona.translation.set(200, 200);
  73. let tip = makeTip(two, "错误", 'rgb(255, 255, 255)', 90);
  74. tip.translation.set(500, 500);
  75. }
  76. function makeTriangle(two, size) {
  77. let tri = two.makePath(-size / 2, 0, size / 2, 0, 0, size);
  78. return tri;
  79. }
  80. function makeTip(two, text, textColor, rotation){
  81. let group = two.makeGroup();
  82. let len = 100;
  83. let tip = two.makePath(-len / 2, -len / 3, -len / 2, -len, len / 2, -len, len / 2, -len / 3 , 0, 0 );
  84. tip.fill = 'red'
  85. tip.rotation = rotation * Math.PI / 180;
  86. group.add(tip);
  87. let txt = two.makeText(text, 0, -len / 1.8);
  88. txt.size = 20;
  89. txt.fill = textColor;
  90. group.add(txt);
  91. group.noStroke();
  92. //连文字一起旋转
  93. //group.rotation = rotation * Math.PI / 180;
  94. return group;
  95. }

此处的三角形关键代码:

点1:(-size / 2, 0)

点2:(size / 2, 0)

点3:(0, size)

这里是一个倒三角形。

下面是tip的关键代码:

 

 

 

相关技术文章

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

提示信息

×

选择支付方式

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