paint.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. } else if (tryTime == 10) {
  23. alert('...')
  24. } else if (tryTime > 10) {
  25. alert('闹呢')
  26. }
  27. tryTime++;
  28. }
  29. var curVer = 0
  30. function getMap() {
  31. if (curVer != 0) document.getElementById('upload').classList.add('disabled')
  32. else document.getElementById('upload').classList.remove('disabled')
  33. const query = new AV.Query('paint');
  34. document.getElementById('version').innerHTML = "当前版本:" + (curVer == 0 ? '' : '-') + curVer + ' by ';
  35. query.descending('updatedAt')
  36. query.limit(curVer + 1)
  37. console.log(curVer)
  38. query.find().then((result) => {
  39. if (result.length > curVer) {
  40. var map = result[curVer].get('data')
  41. document.getElementById('version').title = result[curVer].get('updatedAt');
  42. document.getElementById('version').innerHTML += result[curVer].get('nickName');
  43. for (let i = 0; i < box.length; i++) {
  44. box[i].style.backgroundColor = map[i]
  45. }
  46. } else {
  47. console.log('Found no data!')
  48. curVer = result.length - 1;
  49. getMap()
  50. }
  51. })
  52. }
  53. function reset() {
  54. curVer = 0;
  55. getMap()
  56. }
  57. let box = []
  58. $(document).ready(function () {
  59. let color = '#000'
  60. let lcolor = '#000'
  61. function updateCol() {
  62. lcolor = 'rgb('
  63. + document.getElementById('box-r-text').innerHTML + ', '
  64. + document.getElementById('box-g-text').innerHTML + ', '
  65. + document.getElementById('box-b-text').innerHTML + ')'
  66. nowColor.style.backgroundColor = lcolor
  67. }
  68. function slider(obj, num) {
  69. var box = document.getElementById(obj);
  70. var hd = box.children[0];
  71. var hdt = box.children[1];
  72. hd.onmousedown = function (event) {
  73. var event = event || window.event;
  74. var leftval = event.clientX - this.offsetLeft;
  75. document.onmousemove = function (event) {
  76. var event = event || window.event;
  77. hd.style.left = event.clientX - leftval + "px";
  78. var far = parseInt(hd.style.left);
  79. if (far < 0) {
  80. hd.style.left = 0;
  81. }
  82. else if (far > 180) {
  83. hd.style.left = 180 + "px";
  84. }
  85. hdt.style.width = hd.style.left;
  86. var result = parseInt(parseInt(hdt.style.width) / 180 * num);
  87. document.getElementById(obj + '-text').innerHTML = result;
  88. updateCol()
  89. }
  90. document.onmouseup = function () {
  91. document.onmousemove = null;
  92. }
  93. }
  94. box.onclick = function (event) {
  95. var event = event || window.event;
  96. var boxWidth = event.clientX - this.offsetLeft - document.getElementById('tools').offsetLeft;
  97. if (boxWidth > 180) boxWidth = 180;
  98. if (boxWidth < 0) boxWidth = 0;
  99. hdt.style.width = hd.style.left = boxWidth + 'px';
  100. var result = parseInt(parseInt(hdt.style.width) / 180 * num);
  101. document.getElementById(obj + '-text').innerHTML = result;
  102. updateCol()
  103. }
  104. }
  105. slider('box-r', 255)
  106. slider('box-g', 255)
  107. slider('box-b', 255);
  108. var id = Math.ceil(Math.random() * 1000000);
  109. var draw = document.getElementById('draw')
  110. draw.draggable = false
  111. var lineButton = document.getElementById('show-hide-line')
  112. var colorList = document.getElementById('color-list')
  113. var resetButton = document.getElementById('reset')
  114. var zoomButton = document.getElementById('zoom')
  115. var nowColor = document.getElementById('now-color')
  116. var isdown = 0;
  117. draw.onmousedown = function () {
  118. isdown = 1;
  119. }
  120. draw.onmouseup = function () {
  121. isdown = 0;
  122. }
  123. let showGrid = false
  124. let zoom = false
  125. draw.draggable = false
  126. for (let i = 0; i < 128; i++) {
  127. for (let j = 0; j < 128; j++) {
  128. box.push(document.createElement("div"))
  129. }
  130. }
  131. for (let i = 0; i < box.length; i++) {
  132. box[i].style.backgroundColor = "#fff"
  133. box[i].className = 'pixel'
  134. box[i].onmousedown = function (e) {
  135. if (e.button == 0) color = lcolor
  136. else color = '#fff'
  137. box[i].style.backgroundColor = color
  138. }
  139. box[i].onmouseover = function () {
  140. if (isdown) box[i].style.backgroundColor = color
  141. else if (box[i].style.backgroundColor == 'rgb(255, 255, 255)') {
  142. box[i].style.backgroundColor = 'rgb(235, 235, 235)'
  143. }
  144. }
  145. box[i].onmouseleave = function () {
  146. if (!isdown && box[i].style.backgroundColor == 'rgb(235, 235, 235)') box[i].style.backgroundColor = ('#ffffff')
  147. }
  148. draw.appendChild(box[i])
  149. }
  150. reset();
  151. lineButton.onclick = function () {
  152. if (showGrid) {
  153. changeClass(box, 'pixel')
  154. showGrid = false
  155. } else {
  156. changeClass(box, 'pixel-line')
  157. showGrid = true
  158. }
  159. }
  160. resetButton.onclick = function () {
  161. reset()
  162. }
  163. zoomButton.onclick = function () {
  164. if (zoom == 0) {
  165. draw.style.setProperty('transform', 'scale(200%) translate(-' + draw.offsetLeft / 4 + 'px,16px)')
  166. zoom = 1
  167. } else if (zoom == 1) {
  168. draw.style.setProperty('transform', 'scale(400%) translate(-' + draw.offsetLeft / 4 + 'px,8px)')
  169. zoom = 2
  170. this.children[0].classList.remove('glyphicon-zoom-in')
  171. this.children[0].classList.add('glyphicon-zoom-out')
  172. } else {
  173. draw.style.setProperty('transform', '')
  174. zoom = 0
  175. this.children[0].classList.remove('glyphicon-zoom-out')
  176. this.children[0].classList.add('glyphicon-zoom-in')
  177. }
  178. }
  179. let colors = ['#FFF', '#000', '#7f7f7f', '#c3c3c3', '#880015', '#ed1c24', '#ff7f27', "#fff200", "#22b14c", "#00a2e8", "#3f48cc", "#a349a4"]
  180. for (let i = 0; i < colors.length; i++) {
  181. let colorItem = document.createElement("li")
  182. colorItem.className = 'color-item'
  183. colorItem.style.backgroundColor = colors[i]
  184. colorItem.onclick = function () {
  185. lcolor = colors[i]
  186. nowColor.style.backgroundColor = colors[i]
  187. }
  188. if (i === 0) {
  189. colorItem.style.border = '1px solid #000000'
  190. }
  191. colorList.appendChild(colorItem)
  192. }
  193. function changeClass(arr, className) {
  194. for (let i = 0; i < arr.length; i++) {
  195. arr[i].className = className
  196. }
  197. }
  198. upload.onclick = function () {
  199. if (curVer != 0) {
  200. alert('对历史版本的编辑无法保存!')
  201. return false;
  202. }
  203. var nick = prompt('请确认,可使用昵称,上传后对所有人可见。', '-')
  204. console.log(nick)
  205. if (nick) {
  206. const query = new AV.Query('paint');
  207. console.log(id)
  208. query.equalTo('id', id)
  209. query.find().then((result) => {
  210. var map = []
  211. for (let i = 0; i < box.length; i++) {
  212. map[i] = box[i].style.backgroundColor
  213. }
  214. if (result.length) {
  215. result[0].set('data', map)
  216. result[0].set('nickName', nick)
  217. result[0].save()
  218. console.log('Updated')
  219. }
  220. else {
  221. const up = new AV.Object('paint');
  222. up.set('data', map)
  223. up.set('id', id)
  224. up.set('nickName', nick)
  225. up.save()
  226. console.log('Created new instance')
  227. }
  228. })
  229. document.getElementById('version').innerHTML = "当前版本:" + (curVer == 0 ? '' : '-') + curVer + ' by ' + nick;
  230. }
  231. }
  232. })