paint.js 9.5 KB

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