paint.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. AV.init({
  2. appId: "BmologYYnRqCv0SLHDeDdA17-gzGzoHsz",
  3. appKey: "w9mVebFMdCmY6Nh9vfcBGaGt",
  4. serverURL: "https://bmologyy.lc-cn-n1-shared.com/",
  5. });
  6. $(window).on({
  7. contextmenu: function () {
  8. return false;
  9. },
  10. dragstart: function () {
  11. return false;
  12. },
  13. })
  14. var tryTime = 0
  15. function tryClear() {
  16. if (tryTime == 0) alert('你干嘛')
  17. else if (tryTime == 1) alert('哎呦')
  18. else if (tryTime == 2) alert('不会让你清空的')
  19. else if (tryTime == 3) {
  20. alert('按钮都给你禁用咯')
  21. document.getElementById('clear').classList.add('disabled');
  22. }
  23. tryTime++;
  24. }
  25. function reset() {
  26. const query = new AV.Query('paint');
  27. query.descending('updatedAt')
  28. query.limit(1)
  29. query.find().then((result) => {
  30. if (result.length) {
  31. var map = result[0].get('data')
  32. for (let i = 0; i < box.length; i++) {
  33. box[i].style.backgroundColor = map[i]
  34. }
  35. } else {
  36. console.log('Reset found no data!')
  37. }
  38. })
  39. }
  40. let box = []
  41. $(document).ready(function () {
  42. let color = '#000'
  43. let lcolor = '#000'
  44. function updateCol() {
  45. lcolor = 'rgb('
  46. + document.getElementById('box-r-text').innerHTML + ', '
  47. + document.getElementById('box-g-text').innerHTML + ', '
  48. + document.getElementById('box-b-text').innerHTML + ')'
  49. nowColor.style.backgroundColor = lcolor
  50. }
  51. function slider(obj, num) {
  52. var box = document.getElementById(obj);
  53. var hd = box.children[0];
  54. var hdt = box.children[1];
  55. //鼠标按下事件 只要鼠标按下时是在hd上触发就可以
  56. hd.onmousedown = function (event) {
  57. var event = event || window.event;
  58. var leftval = event.clientX - this.offsetLeft;
  59. //滑动事件 作用在document上,因为如果是作用在hd.onmousemove,滑动时可能会出现鼠标脱离hd范围(范围太小)而停止滑动,但是此时鼠标按下还没有弹起还处于滑动状态,所以就产生了bug
  60. document.onmousemove = function (event) {
  61. var event = event || window.event;
  62. hd.style.left = event.clientX - leftval + "px";
  63. var far = parseInt(hd.style.left);
  64. if (far < 0) {
  65. hd.style.left = 0;
  66. }
  67. else if (far > 180) {
  68. hd.style.left = 180 + "px";
  69. }
  70. hdt.style.width = hd.style.left;
  71. var result = parseInt(parseInt(hdt.style.width) / 180 * num);
  72. document.getElementById(obj + '-text').innerHTML = result;
  73. updateCol()
  74. }
  75. document.onmouseup = function () {
  76. document.onmousemove = null;
  77. }
  78. }
  79. box.onclick = function (event) {
  80. var event = event || window.event;
  81. var boxWidth = event.clientX - this.offsetLeft;
  82. if (boxWidth > 180) {
  83. boxWidth = 180;
  84. }
  85. hdt.style.width = hd.style.left = boxWidth + 'px';
  86. var result = parseInt(parseInt(hdt.style.width) / 180 * num);
  87. document.getElementById(obj + '-text').innerHTML = result;
  88. updateCol()
  89. }
  90. }
  91. slider('box-r', 255)
  92. slider('box-g', 255)
  93. slider('box-b', 255);
  94. var id = Math.ceil(Math.random() * 1000000);
  95. var draw = document.getElementById('draw')
  96. draw.draggable = false
  97. var lineButton = document.getElementById('show-hide-line')
  98. var colorList = document.getElementById('color-list')
  99. var resetButton = document.getElementById('reset')
  100. var nowColor = document.getElementById('now-color')
  101. var isdown = 0;
  102. draw.onmousedown = function () {
  103. isdown = 1;
  104. }
  105. draw.onmouseup = function () {
  106. isdown = 0;
  107. }
  108. let showGrid = false
  109. draw.draggable = false
  110. for (let i = 0; i < 64; i++) { //创建像素点16*16
  111. for (let j = 0; j < 64; j++) {
  112. box.push(document.createElement("div"))
  113. }
  114. }
  115. for (let i = 0; i < box.length; i++) {
  116. box[i].style.backgroundColor = "#fff"
  117. box[i].className = 'pixel'
  118. box[i].onmousedown = function (e) {
  119. if (e.button == 0) color = lcolor
  120. else color = '#fff'
  121. box[i].style.backgroundColor = color
  122. }
  123. box[i].onmouseover = function () {
  124. if (isdown) box[i].style.backgroundColor = color
  125. else if (box[i].style.backgroundColor == 'rgb(255, 255, 255)') {
  126. box[i].style.backgroundColor = 'rgb(235, 235, 235)'
  127. }
  128. }
  129. box[i].onmouseleave = function () {
  130. if (!isdown && box[i].style.backgroundColor == 'rgb(235, 235, 235)') box[i].style.backgroundColor = ('#ffffff')
  131. }
  132. draw.appendChild(box[i])
  133. }
  134. reset();
  135. lineButton.onclick = function () {
  136. if (showGrid) {
  137. changeClass(box, 'pixel')
  138. showGrid = false
  139. } else {
  140. changeClass(box, 'pixel-line')
  141. showGrid = true
  142. }
  143. }
  144. resetButton.onclick = function () {
  145. reset()
  146. }
  147. let colors = ['#FFF', '#000', '#7f7f7f', '#c3c3c3', '#880015', '#ed1c24', '#ff7f27', "#fff200", "#22b14c", "#00a2e8", "#3f48cc", "#a349a4"]
  148. for (let i = 0; i < colors.length; i++) {
  149. let colorItem = document.createElement("li")
  150. colorItem.className = 'color-item'
  151. colorItem.style.backgroundColor = colors[i]
  152. colorItem.onclick = function () {
  153. lcolor = colors[i]
  154. nowColor.style.backgroundColor = colors[i]
  155. }
  156. if (i === 0) {
  157. colorItem.style.border = '1px solid #000000'
  158. }
  159. colorList.appendChild(colorItem)
  160. }
  161. function changeClass(arr, className) {
  162. for (let i = 0; i < arr.length; i++) {
  163. arr[i].className = className
  164. }
  165. }
  166. upload.onclick = function () {
  167. if (confirm('请确认,上传后对所有人可见。')) {
  168. const query = new AV.Query('paint');
  169. console.log(id)
  170. query.equalTo('id', id)
  171. query.find().then((result) => {
  172. var map = []
  173. for (let i = 0; i < box.length; i++) {
  174. map[i] = box[i].style.backgroundColor
  175. }
  176. if (result.length) {
  177. result[0].set('data', map)
  178. result[0].save()
  179. console.log('Updated')
  180. }
  181. else {
  182. const up = new AV.Object('paint');
  183. up.set('data', map)
  184. up.set('id', id)
  185. up.save()
  186. console.log('Created new instance')
  187. }
  188. })
  189. }
  190. }
  191. })