paint.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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')
  79. else document.getElementById('upload').classList.remove('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. hd.onmousedown = function (event) {
  173. var event = event || window.event;
  174. var leftval = event.clientX - this.offsetLeft;
  175. document.onmousemove = function (event) {
  176. var event = event || window.event;
  177. hd.style.left = event.clientX - leftval + "px";
  178. var far = parseInt(hd.style.left);
  179. if (far < 0) {
  180. hd.style.left = 0;
  181. }
  182. else if (far > 180) {
  183. hd.style.left = 180 + "px";
  184. }
  185. hdt.style.width = hd.style.left;
  186. var result = parseInt(parseInt(hdt.style.width) / 180 * num);
  187. document.getElementById(obj + '-text').innerHTML = result;
  188. updateCol()
  189. }
  190. document.onmouseup = function () {
  191. document.onmousemove = null;
  192. }
  193. }
  194. box.onclick = function (event) {
  195. var event = event || window.event;
  196. var boxWidth = event.clientX - this.offsetLeft - document.getElementById('tools').offsetLeft;
  197. if (boxWidth > 180) boxWidth = 180;
  198. if (boxWidth < 0) boxWidth = 0;
  199. hdt.style.width = hd.style.left = boxWidth + 'px';
  200. var result = parseInt(parseInt(hdt.style.width) / 180 * num);
  201. document.getElementById(obj + '-text').innerHTML = result;
  202. updateCol()
  203. }
  204. }
  205. slider('box-r', 255);
  206. slider('box-g', 255);
  207. slider('box-b', 255);
  208. var id = getId()
  209. console.log('id: ' + id)
  210. var draw = document.getElementById('draw')
  211. draw.draggable = false
  212. var lineButton = document.getElementById('show-hide-line')
  213. var colorList = document.getElementById('color-list')
  214. var resetButton = document.getElementById('reset')
  215. var zoomButton = document.getElementById('zoom')
  216. var toggleButton = document.getElementById('toggle')
  217. var nowColor = document.getElementById('now-color')
  218. var isdown = 0;
  219. draw.onmousedown = function () {
  220. isdown = 1;
  221. }
  222. draw.onmouseup = function () {
  223. isdown = 0;
  224. }
  225. let showGrid = false
  226. let zoom = false
  227. let showTools = false
  228. draw.draggable = false
  229. for (let i = 0; i < 128; i++) {
  230. for (let j = 0; j < 128; j++) {
  231. box.push(document.createElement("div"))
  232. }
  233. }
  234. for (let i = 0; i < box.length; i++) {
  235. box[i].style.backgroundColor = "#fff"
  236. box[i].className = 'pixel'
  237. box[i].onmousedown = function (e) {
  238. if (e.button == 0) color = lcolor
  239. else color = '#fff'
  240. box[i].style.backgroundColor = color
  241. }
  242. box[i].onmouseover = function () {
  243. if (isdown) box[i].style.backgroundColor = color
  244. else if (box[i].style.backgroundColor == 'rgb(255, 255, 255)') {
  245. box[i].style.backgroundColor = 'rgb(235, 235, 235)'
  246. }
  247. }
  248. box[i].onmouseleave = function () {
  249. if (!isdown && box[i].style.backgroundColor == 'rgb(235, 235, 235)') box[i].style.backgroundColor = ('#ffffff')
  250. }
  251. draw.appendChild(box[i])
  252. }
  253. reset();
  254. lineButton.onclick = function () {
  255. if (showGrid) {
  256. changeClass(box, 'pixel')
  257. showGrid = false
  258. } else {
  259. changeClass(box, 'pixel-line')
  260. showGrid = true
  261. }
  262. }
  263. resetButton.onclick = function () {
  264. reset()
  265. }
  266. toggleButton.onclick = function () {
  267. var result = $('#tools')
  268. if (showTools) {
  269. for (let i = 0; i < result.length; i++)result[i].style.setProperty('transform', '');
  270. showTools = 0;
  271. } else {
  272. for (let i = 0; i < result.length; i++)result[i].style.setProperty('transform', 'scale(80%) translate(170px,520px)');
  273. showTools = 1;
  274. }
  275. }
  276. zoomButton.onclick = function () {
  277. if (zoom == 0) {
  278. draw.style.setProperty('transform', 'scale(200%) translate(-' + draw.offsetLeft / 4 + 'px,16px)')
  279. zoom = 1
  280. } else if (zoom == 1) {
  281. draw.style.setProperty('transform', 'scale(400%) translate(-' + draw.offsetLeft / 4 + 'px,8px)')
  282. zoom = 2
  283. this.children[0].classList.remove('glyphicon-zoom-in')
  284. this.children[0].classList.add('glyphicon-zoom-out')
  285. } else {
  286. draw.style.setProperty('transform', '')
  287. zoom = 0
  288. this.children[0].classList.remove('glyphicon-zoom-out')
  289. this.children[0].classList.add('glyphicon-zoom-in')
  290. }
  291. }
  292. let colors = ['#FFF', '#000', '#7f7f7f', '#c3c3c3', '#880015', '#ed1c24', '#ff7f27', "#fff200", "#22b14c", "#00a2e8", "#3f48cc", "#a349a4"]
  293. for (let i = 0; i < colors.length; i++) {
  294. let colorItem = document.createElement("li")
  295. colorItem.className = 'color-item'
  296. colorItem.style.backgroundColor = colors[i]
  297. colorItem.onclick = function () {
  298. lcolor = colors[i]
  299. nowColor.style.backgroundColor = colors[i]
  300. }
  301. if (i === 0) {
  302. colorItem.style.border = '1px solid #000000'
  303. }
  304. colorList.appendChild(colorItem)
  305. }
  306. function changeClass(arr, className) {
  307. for (let i = 0; i < arr.length; i++) {
  308. arr[i].className = className
  309. }
  310. }
  311. upload.onclick = function () {
  312. if (curVer != 0) {
  313. alert('对历史版本的编辑无法保存!')
  314. return false;
  315. }
  316. var nick = checkCookie()
  317. console.log('nickName: ' + nick)
  318. if (nick) {
  319. const query = new AV.Query('paint');
  320. console.log('id: ' + id)
  321. query.equalTo('id', id)
  322. query.find().then((result) => {
  323. var map = []
  324. for (let i = 0; i < box.length; i++) {
  325. map[i] = box[i].style.backgroundColor
  326. }
  327. if (result.length) {
  328. result[0].set('data', map)
  329. result[0].set('nickName', nick)
  330. result[0].save()
  331. console.log('Updated')
  332. }
  333. else {
  334. const up = new AV.Object('paint');
  335. up.set('data', map)
  336. up.set('id', id)
  337. up.set('nickName', nick)
  338. up.save()
  339. console.log('Created new instance')
  340. }
  341. })
  342. document.getElementById('version').innerHTML = "当前版本:" + (curVer == 0 ? '' : '-') + curVer + ' by ' + nick;
  343. }
  344. }
  345. })