paint.js 13 KB

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