paint.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. var result = confirm(username + ",确认上传?");
  48. if (result) return username
  49. else return false
  50. }
  51. else {
  52. username = prompt("同一设备一天内的编辑将会保存在一个版本中,并对所有人可见。\n可设置昵称:", "-");
  53. if (username != "" && username != null) {
  54. setCookie("name", username, 1);
  55. return username
  56. } else {
  57. return false
  58. }
  59. }
  60. }
  61. function getId() {
  62. var cookieId = getCookie('id')
  63. if (cookieId != '') return parseInt(cookieId)
  64. else {
  65. var newId = Math.ceil(Math.random() * 1000000)
  66. setCookie('id', newId, 1)
  67. return newId
  68. }
  69. }
  70. var curVer = 0, maxVer = -1, stopVer = 1000, stopped = false, working = false;
  71. var maxResult = {};
  72. function getMap() {
  73. if (working) return;
  74. working = true;
  75. console.log('g' + working)
  76. if (curVer != 0) document.getElementById('upload').classList.add('disabled')
  77. else document.getElementById('upload').classList.remove('disabled')
  78. if (curVer >= stopVer) {
  79. if (!stopped) document.getElementById('lbtn').classList.add('disabled'), stopped = true;
  80. else {
  81. curVer = stopVer;
  82. working = false;
  83. return;
  84. }
  85. }
  86. else document.getElementById('lbtn').classList.remove('disabled'), stopped = false;
  87. document.getElementById('version').innerHTML = "当前版本:" + (curVer == 0 ? '' : '-') + curVer + ' by ';
  88. console.log('GetMap: currentVersion: ' + curVer)
  89. if (maxVer >= curVer) {
  90. console.log('Data loaded from cache')
  91. var map = maxResult[curVer].get('data')
  92. document.getElementById('version').title = maxResult[curVer].get('updatedAt');
  93. document.getElementById('version').innerHTML += maxResult[curVer].get('nickName');
  94. for (let i = 0; i < box.length; i++) {
  95. box[i].style.backgroundColor = map[i]
  96. }
  97. working = false;
  98. } else {
  99. const query = new AV.Query('paint');
  100. query.descending('updatedAt')
  101. query.limit(curVer + 1)
  102. query.find().then((result) => {
  103. if (result.length > curVer) {
  104. console.log('Data loaded from LeanCloud')
  105. var map = result[curVer].get('data')
  106. document.getElementById('version').title = result[curVer].get('updatedAt');
  107. document.getElementById('version').innerHTML += result[curVer].get('nickName');
  108. for (let i = 0; i < box.length; i++) {
  109. box[i].style.backgroundColor = map[i]
  110. }
  111. maxVer = curVer
  112. maxResult = result
  113. } else {
  114. console.log('Found no data!')
  115. curVer = result.length - 1;
  116. stopVer = result.length - 1;
  117. getMap()
  118. }
  119. working = false;
  120. })
  121. }
  122. }
  123. function reset() {
  124. curVer = 0;
  125. maxVer = -1;
  126. stopVer = 1000;
  127. getMap()
  128. }
  129. function prevVer() {
  130. console.log(working)
  131. if (working) return;
  132. curVer++, getMap();
  133. }
  134. function nextVer() {
  135. if (working) return;
  136. curVer == 0 ? true : (curVer--, getMap());
  137. }
  138. document.onkeydown = function (event) {
  139. var e = event || window.event || arguments.callee.caller.arguments[0];
  140. if (e) {
  141. if (e.key == "ArrowLeft") {
  142. prevVer()
  143. }
  144. else if (e.key == "ArrowRight") {
  145. nextVer()
  146. }
  147. }
  148. };
  149. let box = []
  150. $(document).ready(function () {
  151. let color = '#000'
  152. let lcolor = '#000'
  153. function updateCol() {
  154. lcolor = 'rgb('
  155. + document.getElementById('box-r-text').innerHTML + ', '
  156. + document.getElementById('box-g-text').innerHTML + ', '
  157. + document.getElementById('box-b-text').innerHTML + ')'
  158. nowColor.style.backgroundColor = lcolor
  159. }
  160. function slider(obj, num) {
  161. var box = document.getElementById(obj);
  162. var hd = box.children[0];
  163. var hdt = box.children[1];
  164. hd.onmousedown = function (event) {
  165. var event = event || window.event;
  166. var leftval = event.clientX - this.offsetLeft;
  167. document.onmousemove = function (event) {
  168. var event = event || window.event;
  169. hd.style.left = event.clientX - leftval + "px";
  170. var far = parseInt(hd.style.left);
  171. if (far < 0) {
  172. hd.style.left = 0;
  173. }
  174. else if (far > 180) {
  175. hd.style.left = 180 + "px";
  176. }
  177. hdt.style.width = hd.style.left;
  178. var result = parseInt(parseInt(hdt.style.width) / 180 * num);
  179. document.getElementById(obj + '-text').innerHTML = result;
  180. updateCol()
  181. }
  182. document.onmouseup = function () {
  183. document.onmousemove = null;
  184. }
  185. }
  186. box.onclick = function (event) {
  187. var event = event || window.event;
  188. var boxWidth = event.clientX - this.offsetLeft - document.getElementById('tools').offsetLeft;
  189. if (boxWidth > 180) boxWidth = 180;
  190. if (boxWidth < 0) boxWidth = 0;
  191. hdt.style.width = hd.style.left = boxWidth + 'px';
  192. var result = parseInt(parseInt(hdt.style.width) / 180 * num);
  193. document.getElementById(obj + '-text').innerHTML = result;
  194. updateCol()
  195. }
  196. }
  197. slider('box-r', 255);
  198. slider('box-g', 255);
  199. slider('box-b', 255);
  200. var id = getId()
  201. console.log('id: ' + id)
  202. var draw = document.getElementById('draw')
  203. draw.draggable = false
  204. var lineButton = document.getElementById('show-hide-line')
  205. var colorList = document.getElementById('color-list')
  206. var resetButton = document.getElementById('reset')
  207. var zoomButton = document.getElementById('zoom')
  208. var toggleButton = document.getElementById('toggle')
  209. var nowColor = document.getElementById('now-color')
  210. var isdown = 0;
  211. draw.onmousedown = function () {
  212. isdown = 1;
  213. }
  214. draw.onmouseup = function () {
  215. isdown = 0;
  216. }
  217. let showGrid = false
  218. let zoom = false
  219. let showTools = false
  220. draw.draggable = false
  221. for (let i = 0; i < 128; i++) {
  222. for (let j = 0; j < 128; j++) {
  223. box.push(document.createElement("div"))
  224. }
  225. }
  226. for (let i = 0; i < box.length; i++) {
  227. box[i].style.backgroundColor = "#fff"
  228. box[i].className = 'pixel'
  229. box[i].onmousedown = function (e) {
  230. if (e.button == 0) color = lcolor
  231. else color = '#fff'
  232. box[i].style.backgroundColor = color
  233. }
  234. box[i].onmouseover = function () {
  235. if (isdown) box[i].style.backgroundColor = color
  236. else if (box[i].style.backgroundColor == 'rgb(255, 255, 255)') {
  237. box[i].style.backgroundColor = 'rgb(235, 235, 235)'
  238. }
  239. }
  240. box[i].onmouseleave = function () {
  241. if (!isdown && box[i].style.backgroundColor == 'rgb(235, 235, 235)') box[i].style.backgroundColor = ('#ffffff')
  242. }
  243. draw.appendChild(box[i])
  244. }
  245. reset();
  246. lineButton.onclick = function () {
  247. if (showGrid) {
  248. changeClass(box, 'pixel')
  249. showGrid = false
  250. } else {
  251. changeClass(box, 'pixel-line')
  252. showGrid = true
  253. }
  254. }
  255. resetButton.onclick = function () {
  256. reset()
  257. }
  258. toggleButton.onclick = function () {
  259. var result = $('#tools')
  260. if (showTools) {
  261. for (let i = 0; i < result.length; i++)result[i].style.setProperty('transform', '');
  262. showTools = 0;
  263. } else {
  264. for (let i = 0; i < result.length; i++)result[i].style.setProperty('transform', 'scale(80%) translate(170px,520px)');
  265. showTools = 1;
  266. }
  267. }
  268. zoomButton.onclick = function () {
  269. if (zoom == 0) {
  270. draw.style.setProperty('transform', 'scale(200%) translate(-' + draw.offsetLeft / 4 + 'px,16px)')
  271. zoom = 1
  272. } else if (zoom == 1) {
  273. draw.style.setProperty('transform', 'scale(400%) translate(-' + draw.offsetLeft / 4 + 'px,8px)')
  274. zoom = 2
  275. this.children[0].classList.remove('glyphicon-zoom-in')
  276. this.children[0].classList.add('glyphicon-zoom-out')
  277. } else {
  278. draw.style.setProperty('transform', '')
  279. zoom = 0
  280. this.children[0].classList.remove('glyphicon-zoom-out')
  281. this.children[0].classList.add('glyphicon-zoom-in')
  282. }
  283. }
  284. let colors = ['#FFF', '#000', '#7f7f7f', '#c3c3c3', '#880015', '#ed1c24', '#ff7f27', "#fff200", "#22b14c", "#00a2e8", "#3f48cc", "#a349a4"]
  285. for (let i = 0; i < colors.length; i++) {
  286. let colorItem = document.createElement("li")
  287. colorItem.className = 'color-item'
  288. colorItem.style.backgroundColor = colors[i]
  289. colorItem.onclick = function () {
  290. lcolor = colors[i]
  291. nowColor.style.backgroundColor = colors[i]
  292. }
  293. if (i === 0) {
  294. colorItem.style.border = '1px solid #000000'
  295. }
  296. colorList.appendChild(colorItem)
  297. }
  298. function changeClass(arr, className) {
  299. for (let i = 0; i < arr.length; i++) {
  300. arr[i].className = className
  301. }
  302. }
  303. upload.onclick = function () {
  304. if (curVer != 0) {
  305. alert('对历史版本的编辑无法保存!')
  306. return false;
  307. }
  308. var nick = checkCookie()
  309. console.log('nickName: ' + nick)
  310. if (nick) {
  311. const query = new AV.Query('paint');
  312. console.log('id: ' + id)
  313. query.equalTo('id', id)
  314. query.find().then((result) => {
  315. var map = []
  316. for (let i = 0; i < box.length; i++) {
  317. map[i] = box[i].style.backgroundColor
  318. }
  319. if (result.length) {
  320. result[0].set('data', map)
  321. result[0].set('nickName', nick)
  322. result[0].save()
  323. console.log('Updated')
  324. }
  325. else {
  326. const up = new AV.Object('paint');
  327. up.set('data', map)
  328. up.set('id', id)
  329. up.set('nickName', nick)
  330. up.save()
  331. console.log('Created new instance')
  332. }
  333. })
  334. document.getElementById('version').innerHTML = "当前版本:" + (curVer == 0 ? '' : '-') + curVer + ' by ' + nick;
  335. }
  336. }
  337. })