paint.js 14 KB

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