index.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <html>
  2. <head>
  3. <!-- Tocas UI:CSS 與元件 -->
  4. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tocas-ui/2.3.3/tocas.css">
  5. <!-- Tocas JS:模塊與 JavaScript 函式 -->
  6. <script src="https://cdnjs.cloudflare.com/ajax/libs/tocas-ui/2.3.3/tocas.js"></script>
  7. <script src="../script/jquery.min.js"></script>
  8. <script src="https://cdnjs.cloudflare.com/ajax/libs/pressure/2.1.2/jquery.pressure.min.js"></script>
  9. <script src="../script/ao_module.js"></script>
  10. <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  11. </head>
  12. <body>
  13. <div class="ts menu" style="position: absolute;width: 100vw;z-index: 2;">
  14. <a class="item" onclick="save();">Save</a>
  15. <a class="item" onclick="importimg();">Import</a>
  16. <a class="item" id="mode" onclick="chgmode()">Normal</a>
  17. </div>
  18. <canvas id="paintArea" width="100" height="100"></canvas>
  19. <div style="position: fixed;bottom: 0;right: 0;">
  20. <div style="bottom: 5px;right: 5px;display: block;float: left;" id="colorselector">
  21. <div class="ts slider" style="margin-right: 10px;float: right;">
  22. <input id="width" type="range">
  23. </div>
  24. </div>
  25. </div>
  26. <div class="ts bottom right snackbar">
  27. <div class="content"></div>
  28. </div>
  29. </body>
  30. <script type="text/javascript">
  31. var canvas = document.getElementById("paintArea");
  32. var ctx = canvas.getContext('2d');
  33. var clicking = false;
  34. var width = 0;
  35. ts('.ts.slider').slider();
  36. var currHeight = window.innerWidth;
  37. var currWidth = window.innerHeight;
  38. $("#paintArea")[0].width = window.innerWidth;
  39. $("#paintArea")[0].height = window.innerHeight;
  40. var color = ['#ff0000', '#ff8c00', '#fff600', '#00ff6e', '#00ffae', '#3b00ff', '#ae00ff', '#000000'];
  41. color.forEach(function(element) {
  42. $("#colorselector").prepend('<div class="cp" style="background-color: ' + element + ';width: 30px;height: 30px;border-radius: 50%;float: right;margin-right: 4px;" data-color="' + element + '" onclick="chgcolor(this)"></div>');
  43. });
  44. $( ".cp[data-color~='#000000']" ).html('<i style="margin-left: 24%;margin-top: 24%;" class="checkmark icon"></i>');
  45. function chgcolor(element){
  46. ctx.strokeStyle = $(element).attr("data-color");
  47. $(".cp").html("");
  48. $(element).html('<i style="margin-left: 24%;margin-top: 24%;" class="checkmark icon"></i>');
  49. }
  50. var pmode = false;
  51. function chgmode(){
  52. if(pmode){
  53. pmode = false;
  54. $("#mode").html("Normal");
  55. }else{
  56. pmode = true;
  57. $("#mode").html("Pressure");
  58. }
  59. }
  60. $('#paintArea').pressure({
  61. change: function(f, event){
  62. if(pmode){
  63. width = f * 10;
  64. $("#width").attr("style","background-image: -webkit-gradient(linear, 0% 0%, 100% 0%, color-stop(" + width/10 + ", rgb(150, 150, 150)), color-stop(" + width/10 + ", rgb(233, 233, 233)));");
  65. $("#width").val(width * 10);
  66. console.log(width);
  67. }
  68. },
  69. });
  70. $(window).on('resize', function () {
  71. var currImg = ctx.getImageData(0, 0, currWidth, currHeight);
  72. currWidth = window.innerWidth;
  73. currHeight = window.innerHeight;
  74. $("#paintArea")[0].width = window.innerWidth;
  75. $("#paintArea")[0].height = window.innerHeight;
  76. ctx.putImageData(currImg, 0, 0);
  77. });
  78. var prevX = 0;
  79. var prevY = 0;
  80. $( "#paintArea" ).on('mousedown touchstart',function(e) {
  81. console.log(e.pageX, e.pageY);
  82. clicking = true;
  83. if(!pmode){
  84. ctx.beginPath();
  85. ctx.moveTo(e.pageX,e.pageY);
  86. }
  87. prevX = e.pageX;
  88. prevY = e.pageY;
  89. });
  90. $( "#paintArea" ).on('mousemove touchmove',function(e) {
  91. if(clicking){
  92. if(pmode){
  93. ctx.beginPath();
  94. ctx.moveTo(prevX,prevY);
  95. }
  96. ctx.lineWidth = width;
  97. console.log(e.pageX, e.pageY);
  98. ctx.lineTo(e.pageX,e.pageY);
  99. ctx.stroke();
  100. prevX = e.pageX;
  101. prevY = e.pageY;
  102. }
  103. });
  104. $( "#paintArea" ).on('mouseup touchend',function(e) {
  105. clicking = false;
  106. });
  107. $("#width").on('change', function () {
  108. width = $("#width").val() / 10;
  109. });
  110. function save(){
  111. if (ao_module_virtualDesktop){
  112. ao_module_openFileSelector(Math.round(Math.random()*100,0),"setPathBySelector",undefined,undefined,false,"folder");
  113. }else{
  114. ao_module_openFileSelectorTab(Math.round(Math.random()*100,0),"../",false,"folder",setPathBySelector);
  115. }
  116. }
  117. function importimg(){
  118. if (ao_module_virtualDesktop){
  119. ao_module_openFileSelector(Math.round(Math.random()*100,0),"importselector",undefined,undefined,false,"file");
  120. }else{
  121. ao_module_openFileSelectorTab(Math.round(Math.random()*100,0),"../",false,"file",importselector);
  122. }
  123. }
  124. function importselector(object){
  125. var files = JSON.parse(object);
  126. console.log(files);
  127. var img = new Image();
  128. img.onload = function() {
  129. var ctx = document.getElementById('paintArea').getContext('2d');
  130. ctx.drawImage(img, 0, 0);
  131. msgbox("Imported /AOR/" + files[0].filepath);
  132. }
  133. img.src = "../" + files[0].filepath;
  134. }
  135. function setPathBySelector(object){
  136. var files = JSON.parse(object);
  137. console.log(files);
  138. $.post("save.php", { b64: $("#paintArea")[0].toDataURL('image/png'), path: files[0].filepath},
  139. function(data){
  140. msgbox("Saved as " + data);
  141. });
  142. }
  143. function msgbox(content){
  144. ts('.snackbar').snackbar({
  145. content: content,
  146. onAction: () => {
  147. $(".snackbar").removeAttr("style");
  148. }
  149. });
  150. }
  151. function preventPullToRefresh(element) {
  152. var prevent = false;
  153. document.querySelector(element).addEventListener('touchstart', function(e){
  154. if (e.touches.length !== 1) { return; }
  155. var scrollY = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
  156. prevent = (scrollY === 0);
  157. });
  158. document.querySelector(element).addEventListener('touchmove', function(e){
  159. if (prevent) {
  160. prevent = false;
  161. e.preventDefault();
  162. }
  163. });
  164. }
  165. preventPullToRefresh('#paintArea') // pass #id or html tag into the method
  166. </script>
  167. </html>