关键词搜索

源码搜索 ×
×

Web&QML笔记-qml获取canvas中元素是否被按下

发布2020-07-08浏览1075次

详情内容

以前出了几个用QWebView,获取html前端数据的博文,

使用QWebElement可以直接获取html中元素的填充的值。

在此不在多提。这个是纯QML获取canvas中元素是否被按下的思路。

 

这里先演示下

程序运行截图如下:

点击按钮生成两个元素:

点一下第一个红色矩形元素:

点一下第二个红色矩形元素:

其原理就是qml按钮调用web前端函数,构造出2canvas矩形。

然后web前端还提供了一个函数,这个函数用于查询这个按钮是否被按下,下面这个例子是使用map存储id以及bool,判断是否被按下。Qml中又给计时器去不停的调用这个函数,从而获取按钮是否被按下。

 

其qml源码如下:

  1. import QtQuick 2.6
  2. import QtQuick.Window 2.2
  3. import QtWebEngine 1.3
  4. import QtQuick.Controls 1.4
  5. import QtQuick.Dialogs 1.2
  6. Window {
  7. visible: true
  8. width: 640
  9. height: 480
  10. title: qsTr("Hello World")
  11. WebEngineView{
  12. id: web;
  13. anchors.fill: parent;
  14. url: "http://127.0.0.1:8081/";
  15. }
  16. Button{
  17. onClicked: {
  18. var functionStr = "window.mainPage.createNewBtn(10001, 100, 100)";
  19. web.runJavaScript(functionStr, function(result){
  20. console.log(">" + "window.mainPage.createNewBtn(10001, 100, 100) over");
  21. });
  22. functionStr = "window.mainPage.createNewBtn(10003, 400, 400)";
  23. web.runJavaScript(functionStr, function(result){
  24. console.log(">" + "window.mainPage.createNewBtn(10003, 400, 400) over");
  25. })
  26. }
  27. }
  28. MessageDialog {
  29. id: messageDialog
  30. }
  31. //使用定时器来判断界面按钮是否按下了
  32. Timer{
  33. interval: 500;
  34. repeat: true;
  35. running: true;
  36. onTriggered: {
  37. var functionStr1 = "window.mainPage.getClickedStatus(10001)";
  38. var functionStr2 = "window.mainPage.getClickedStatus(10003)";
  39. web.runJavaScript(functionStr1, function(result){
  40. //console.log(">" + String(result));
  41. if(String(result).toString().match("true")){
  42. messageDialog.title = "tip";
  43. messageDialog.text = "window.mainPage.getClickedStatus(10001)";
  44. messageDialog.visible = true;
  45. }
  46. });
  47. web.runJavaScript(functionStr2, function(result){
  48. //console.log(">" + String(result));
  49. if(String(result).toString().match("true")){
  50. messageDialog.title = "tip";
  51. messageDialog.text = "window.mainPage.getClickedStatus(10003)";
  52. messageDialog.visible = true;
  53. }
  54. })
  55. }
  56. }
  57. }

前端源码如下:

  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, createBtn, getStatus} from "JS/draw";
  10. export default {
  11. name: 'MainPage',
  12. mounted(){
  13. window.mainPage = this;
  14. drawGraphic();
  15. },
  16. methods:{
  17. createNewBtn(id, x, y){
  18. createBtn(id, x, y);
  19. return "over";
  20. },
  21. getClickedStatus(id){
  22. return getStatus(id);
  23. }
  24. },
  25. data () {
  26. return {
  27. msg: 'Welcome to Your Vue.js App'
  28. }
  29. }
  30. }
  31. </script>
  32. <style scoped>
  33. </style>

关键代码:

脚本源码:

  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. //移动端触碰结束
  66. let text = two.makeText("Hello", 0, 0);
  67. text.size = 20;
  68. text.fill = "red";
  69. text.rotation = 90 * Math.PI / 180;
  70. //查询
  71. let corona = makeTriangle(two, 100);
  72. corona.noStroke();
  73. corona.fill = "red";
  74. corona.translation.set(200, 200);
  75. let tip = makeTip(two, "错误", 'rgb(255, 255, 255)', 90);
  76. tip.translation.set(500, 500);
  77. let ellipse = two.makeEllipse(800, 800, 200, 300);
  78. ellipse.linewidth = 5;
  79. ellipse.stroke = "rgb(0, 128, 255)";
  80. */
  81. }
  82. function makeTriangle(two, size) {
  83. let tri = two.makePath(-size / 2, 0, size / 2, 0, 0, size);
  84. return tri;
  85. }
  86. function makeTip(two, text, textColor, rotation){
  87. let group = two.makeGroup();
  88. let len = 100;
  89. let tip = two.makePath(-len / 2, -len / 3, -len / 2, -len, len / 2, -len, len / 2, -len / 3 , 0, 0 );
  90. tip.fill = 'red';
  91. tip.rotation = rotation * Math.PI / 180;
  92. group.add(tip);
  93. let txt = two.makeText(text, 0, -len / 1.8);
  94. txt.size = 20;
  95. txt.fill = textColor;
  96. group.add(txt);
  97. group.noStroke();
  98. //连文字一起旋转
  99. //group.rotation = rotation * Math.PI / 180;
  100. return group;
  101. }
  102. let map = new Map();
  103. export function createBtn(id, x, y) {
  104. let rect = two.makeRectangle(x, y, 200, 200);
  105. rect.noStroke();
  106. rect.fill = "red";
  107. rect.myId = id;
  108. //此步骤不能少,否则
  109. two.update();
  110. map.set(id.toString(), false);
  111. $(rect._renderer.elem)
  112. .click(function (e){
  113. map.set(rect.myId.toString(), true);
  114. });
  115. }
  116. export function getStatus(id){
  117. let result = map.get(id.toString());
  118. if(result != null && result != false){
  119. map.delete(id.toString());
  120. }
  121. return result;
  122. }

关键代码如下:

 

 

 

相关技术文章

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

提示信息

×

选择支付方式

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