paint.js 13 KB

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