paint.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. $('#clear').addClass('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) $('#upload').addClass('disabled');
  82. else $('#upload').removeClass('disabled');
  83. if (curVer != 0) $('#rbtn').removeClass('disabled');
  84. else $('#rbtn').addClass('disabled');
  85. if (curVer >= stopVer) {
  86. if (!stopped) $('#lbtn').addClass('disabled'), stopped = true;
  87. else {
  88. curVer = stopVer;
  89. working = false;
  90. return;
  91. }
  92. }
  93. else $('#lbtn').removeClass('disabled'), stopped = false;
  94. $('#version')[0].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. $('#version')[0].title = date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate() + ' ' + date.toString().split(' ')[4];
  100. $('#version')[0].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 + 6)
  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. $('#version')[0].title = date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate() + ' ' + date.toString().split(' ')[4];
  115. $('#version')[0].innerHTML += result[curVer].get('nickName');
  116. for (let i = 0; i < box.length; i++) {
  117. box[i].style.backgroundColor = map[i]
  118. }
  119. maxVer = Math.min(result.length - 1, curVer + 6)
  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. } else if ("0" <= e.key && e.key <= "9") {
  161. }
  162. }
  163. };
  164. let box = []
  165. $(document).ready(function () {
  166. let color = '#000'
  167. let lcolor = '#000'
  168. function updateCol() {
  169. lcolor = 'rgb('
  170. + $('#box-r-text')[0].value + ', '
  171. + $('#box-g-text')[0].value + ', '
  172. + $('#box-b-text')[0].value + ')'
  173. nowColor.style.backgroundColor = lcolor
  174. }
  175. function slider(obj, num) {
  176. var box = document.getElementById(obj);
  177. var hd = box.children[0];
  178. var hdt = box.children[1];
  179. var mouseMoved = false;
  180. hd.onmousedown = function (event) {
  181. mouseMoved = false;
  182. var event = event || window.event;
  183. var leftval = event.clientX - this.offsetLeft;
  184. document.onmousemove = function (event) {
  185. mouseMoved = true;
  186. var event = event || window.event;
  187. hd.style.left = event.clientX - leftval + "px";
  188. var far = parseInt(hd.style.left);
  189. if (far < 0) hd.style.left = 0;
  190. else if (far > 180) hd.style.left = 180 + "px";
  191. hdt.style.width = hd.style.left;
  192. var result = parseInt(parseInt(hdt.style.width) / 180 * num);
  193. document.getElementById(obj + '-text').value = result;
  194. updateCol()
  195. }
  196. document.onmouseup = function () {
  197. document.onmousemove = null;
  198. }
  199. }
  200. box.onclick = function (event) {
  201. var event = event || window.event;
  202. if (mouseMoved) { mouseMoved = false; return false; }
  203. mouseMoved = false;
  204. var boxWidth = event.clientX - this.offsetLeft - $('#tools')[0].offsetLeft;
  205. if (boxWidth > 180) boxWidth = 180;
  206. if (boxWidth < -10) return;
  207. if (boxWidth < 0) boxWidth = 0;
  208. hdt.style.width = hd.style.left = boxWidth + 'px';
  209. var result = parseInt(parseInt(hdt.style.width) / 180 * num);
  210. document.getElementById(obj + '-text').value = result;
  211. updateCol()
  212. }
  213. }
  214. function changeSlider(obj, num, newVal) {
  215. if (newVal < 0) newVal = 0;
  216. if (newVal > 255) newVal = 255;
  217. var box = document.getElementById(obj);
  218. var hd = box.children[0];
  219. var hdt = box.children[1];
  220. hd.style.left = newVal / num * 180 + 'px';
  221. hdt.style.width = hd.style.left;
  222. document.getElementById(obj + '-text').value = newVal;
  223. }
  224. slider('box-r', 255);
  225. slider('box-g', 255);
  226. slider('box-b', 255);
  227. $(".box-text").bind('input porpertychange', function () {
  228. console.log("e");
  229. updateCol();
  230. changeSlider('box-r', 255, $('#box-r-text')[0].value)
  231. changeSlider('box-g', 255, $('#box-g-text')[0].value)
  232. changeSlider('box-b', 255, $('#box-b-text')[0].value)
  233. });
  234. var id = getId()
  235. console.log('id: ' + id)
  236. var draw = document.getElementById('draw')
  237. draw.draggable = false
  238. var lineButton = document.getElementById('show-hide-line')
  239. var colorList = document.getElementById('color-list')
  240. var resetButton = document.getElementById('reset')
  241. var zoomButton = document.getElementById('zoom')
  242. var toggleButton = document.getElementById('toggle')
  243. var nowColor = document.getElementById('now-color')
  244. var isdown = 0;
  245. draw.onmousedown = function () {
  246. isdown = 1;
  247. }
  248. draw.onmouseup = function () {
  249. isdown = 0;
  250. }
  251. let showGrid = false
  252. let zoom = false
  253. let showTools = false
  254. draw.draggable = false
  255. for (let i = 0; i < 128; i++) {
  256. for (let j = 0; j < 128; j++) {
  257. box.push(document.createElement("div"))
  258. }
  259. }
  260. for (let i = 0; i < box.length; i++) {
  261. box[i].style.backgroundColor = "#fff"
  262. box[i].className = 'pixel'
  263. box[i].onmousedown = function (e) {
  264. if (e.button == 0) color = lcolor
  265. else color = '#fff'
  266. box[i].style.backgroundColor = color
  267. }
  268. box[i].onmouseover = function () {
  269. if (isdown) box[i].style.backgroundColor = color
  270. else if (box[i].style.backgroundColor == 'rgb(255, 255, 255)') {
  271. box[i].style.backgroundColor = 'rgb(235, 235, 235)'
  272. }
  273. }
  274. box[i].onmouseleave = function () {
  275. if (!isdown && box[i].style.backgroundColor == 'rgb(235, 235, 235)') box[i].style.backgroundColor = ('#ffffff')
  276. }
  277. draw.appendChild(box[i])
  278. }
  279. reset();
  280. lineButton.onclick = function () {
  281. if (showGrid) {
  282. changeClass(box, 'pixel')
  283. showGrid = false
  284. } else {
  285. changeClass(box, 'pixel-line')
  286. showGrid = true
  287. }
  288. }
  289. resetButton.onclick = function () {
  290. reset()
  291. }
  292. toggleButton.onclick = function () {
  293. var tool = $('#tools')
  294. var controls = $('#controls')
  295. if (showTools) {
  296. tool[0].style.setProperty('transform', '');
  297. controls[0].style.setProperty('transform', '');
  298. showTools = 0;
  299. } else {
  300. tool[0].style.setProperty('transform', 'scale(80%) translate(170px,530px)');
  301. controls[0].style.setProperty('transform', 'rotate(-30deg) translate(-370px,50px)');
  302. showTools = 1;
  303. }
  304. }
  305. zoomButton.onclick = function () {
  306. if (zoom == 0) {
  307. draw.style.setProperty('transform', 'scale(200%) translate(-' + draw.offsetLeft / 4 + 'px,16px)')
  308. zoom = 1
  309. } else if (zoom == 1) {
  310. draw.style.setProperty('transform', 'scale(400%) translate(-' + draw.offsetLeft / 4 + 'px,8px)')
  311. zoom = 2
  312. this.children[0].classList.remove('glyphicon-zoom-in')
  313. this.children[0].classList.add('glyphicon-zoom-out')
  314. } else {
  315. draw.style.setProperty('transform', '')
  316. zoom = 0
  317. this.children[0].classList.remove('glyphicon-zoom-out')
  318. this.children[0].classList.add('glyphicon-zoom-in')
  319. }
  320. }
  321. let colors = ['#FFF', '#000', '#7f7f7f', '#c3c3c3', '#880015', '#ed1c24', '#ff7f27', "#fff200", "#22b14c", "#00a2e8", "#3f48cc", "#a349a4"]
  322. for (let i = 0; i < colors.length; i++) {
  323. let colorItem = document.createElement("li")
  324. colorItem.className = 'color-item'
  325. colorItem.style.backgroundColor = colors[i]
  326. colorItem.onclick = function () {
  327. lcolor = colors[i]
  328. nowColor.style.backgroundColor = colors[i]
  329. changeSlider('box-r', 255, parseInt(colorItem.style.backgroundColor.split(',')[0].split('(')[1]))
  330. changeSlider('box-g', 255, parseInt(colorItem.style.backgroundColor.split(',')[1]))
  331. changeSlider('box-b', 255, parseInt(colorItem.style.backgroundColor.split(',')[2]))
  332. updateCol();
  333. }
  334. if (i === 0) {
  335. colorItem.style.border = '1px solid #000000'
  336. }
  337. colorList.appendChild(colorItem)
  338. }
  339. function changeClass(arr, className) {
  340. for (let i = 0; i < arr.length; i++) {
  341. arr[i].className = className
  342. }
  343. }
  344. upload.onclick = function () {
  345. if (curVer >= 3) {
  346. alert('过于久远,无法上传!')
  347. return false;
  348. }
  349. var nick = checkCookie()
  350. console.log('nickName: ' + nick)
  351. if (nick) {
  352. const query = new AV.Query('paint');
  353. console.log('id: ' + id)
  354. query.equalTo('id', id)
  355. query.find().then((result) => {
  356. var map = []
  357. for (let i = 0; i < box.length; i++) {
  358. map[i] = box[i].style.backgroundColor
  359. }
  360. if (result.length) {
  361. result[0].set('data', map)
  362. result[0].set('nickName', nick)
  363. result[0].save()
  364. console.log('Updated')
  365. }
  366. else {
  367. const up = new AV.Object('paint');
  368. up.set('data', map)
  369. up.set('id', id)
  370. up.set('nickName', nick)
  371. up.save()
  372. console.log('Created new instance')
  373. }
  374. })
  375. $('#version')[0].innerHTML = "当前版本:" + (curVer == 0 ? '' : '-') + curVer + ' by ' + nick;
  376. }
  377. }
  378. })