paint.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. console.log(ca)
  39. for (var i = 0; i < ca.length; i++) {
  40. var c = ca[i].trim();
  41. if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
  42. }
  43. return "";
  44. }
  45. function checkCookie() {
  46. var username = getCookie("username");
  47. if (username != "") {
  48. alert("Welcome again " + username);
  49. return username
  50. }
  51. else {
  52. username = prompt("Please enter your name:", "");
  53. if (username != "" && username != null) {
  54. setCookie("username", username, 365);
  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,365)
  67. return newId
  68. }
  69. }
  70. var curVer = 0
  71. function getMap() {
  72. if (curVer != 0) document.getElementById('upload').classList.add('disabled')
  73. else document.getElementById('upload').classList.remove('disabled')
  74. const query = new AV.Query('paint');
  75. document.getElementById('version').innerHTML = "当前版本:" + (curVer == 0 ? '' : '-') + curVer + ' by ';
  76. query.descending('updatedAt')
  77. query.limit(curVer + 1)
  78. console.log(curVer)
  79. query.find().then((result) => {
  80. if (result.length > curVer) {
  81. var map = result[curVer].get('data')
  82. document.getElementById('version').title = result[curVer].get('updatedAt');
  83. document.getElementById('version').innerHTML += result[curVer].get('nickName');
  84. for (let i = 0; i < box.length; i++) {
  85. box[i].style.backgroundColor = map[i]
  86. }
  87. } else {
  88. console.log('Found no data!')
  89. curVer = result.length - 1;
  90. getMap()
  91. }
  92. })
  93. }
  94. function reset() {
  95. curVer = 0;
  96. getMap()
  97. }
  98. let box = []
  99. $(document).ready(function () {
  100. let color = '#000'
  101. let lcolor = '#000'
  102. function updateCol() {
  103. lcolor = 'rgb('
  104. + document.getElementById('box-r-text').innerHTML + ', '
  105. + document.getElementById('box-g-text').innerHTML + ', '
  106. + document.getElementById('box-b-text').innerHTML + ')'
  107. nowColor.style.backgroundColor = lcolor
  108. }
  109. function slider(obj, num) {
  110. var box = document.getElementById(obj);
  111. var hd = box.children[0];
  112. var hdt = box.children[1];
  113. hd.onmousedown = function (event) {
  114. var event = event || window.event;
  115. var leftval = event.clientX - this.offsetLeft;
  116. document.onmousemove = function (event) {
  117. var event = event || window.event;
  118. hd.style.left = event.clientX - leftval + "px";
  119. var far = parseInt(hd.style.left);
  120. if (far < 0) {
  121. hd.style.left = 0;
  122. }
  123. else if (far > 180) {
  124. hd.style.left = 180 + "px";
  125. }
  126. hdt.style.width = hd.style.left;
  127. var result = parseInt(parseInt(hdt.style.width) / 180 * num);
  128. document.getElementById(obj + '-text').innerHTML = result;
  129. updateCol()
  130. }
  131. document.onmouseup = function () {
  132. document.onmousemove = null;
  133. }
  134. }
  135. box.onclick = function (event) {
  136. var event = event || window.event;
  137. var boxWidth = event.clientX - this.offsetLeft - document.getElementById('tools').offsetLeft;
  138. if (boxWidth > 180) boxWidth = 180;
  139. if (boxWidth < 0) boxWidth = 0;
  140. hdt.style.width = hd.style.left = boxWidth + 'px';
  141. var result = parseInt(parseInt(hdt.style.width) / 180 * num);
  142. document.getElementById(obj + '-text').innerHTML = result;
  143. updateCol()
  144. }
  145. }
  146. slider('box-r', 255)
  147. slider('box-g', 255)
  148. slider('box-b', 255);
  149. var id=getId()
  150. console.log('id:'+id)
  151. var draw = document.getElementById('draw')
  152. draw.draggable = false
  153. var lineButton = document.getElementById('show-hide-line')
  154. var colorList = document.getElementById('color-list')
  155. var resetButton = document.getElementById('reset')
  156. var zoomButton = document.getElementById('zoom')
  157. var nowColor = document.getElementById('now-color')
  158. var isdown = 0;
  159. draw.onmousedown = function () {
  160. isdown = 1;
  161. }
  162. draw.onmouseup = function () {
  163. isdown = 0;
  164. }
  165. let showGrid = false
  166. let zoom = false
  167. draw.draggable = false
  168. for (let i = 0; i < 128; i++) {
  169. for (let j = 0; j < 128; j++) {
  170. box.push(document.createElement("div"))
  171. }
  172. }
  173. for (let i = 0; i < box.length; i++) {
  174. box[i].style.backgroundColor = "#fff"
  175. box[i].className = 'pixel'
  176. box[i].onmousedown = function (e) {
  177. if (e.button == 0) color = lcolor
  178. else color = '#fff'
  179. box[i].style.backgroundColor = color
  180. }
  181. box[i].onmouseover = function () {
  182. if (isdown) box[i].style.backgroundColor = color
  183. else if (box[i].style.backgroundColor == 'rgb(255, 255, 255)') {
  184. box[i].style.backgroundColor = 'rgb(235, 235, 235)'
  185. }
  186. }
  187. box[i].onmouseleave = function () {
  188. if (!isdown && box[i].style.backgroundColor == 'rgb(235, 235, 235)') box[i].style.backgroundColor = ('#ffffff')
  189. }
  190. draw.appendChild(box[i])
  191. }
  192. reset();
  193. lineButton.onclick = function () {
  194. if (showGrid) {
  195. changeClass(box, 'pixel')
  196. showGrid = false
  197. } else {
  198. changeClass(box, 'pixel-line')
  199. showGrid = true
  200. }
  201. }
  202. resetButton.onclick = function () {
  203. reset()
  204. }
  205. zoomButton.onclick = function () {
  206. if (zoom == 0) {
  207. draw.style.setProperty('transform', 'scale(200%) translate(-' + draw.offsetLeft / 4 + 'px,16px)')
  208. zoom = 1
  209. } else if (zoom == 1) {
  210. draw.style.setProperty('transform', 'scale(400%) translate(-' + draw.offsetLeft / 4 + 'px,8px)')
  211. zoom = 2
  212. this.children[0].classList.remove('glyphicon-zoom-in')
  213. this.children[0].classList.add('glyphicon-zoom-out')
  214. } else {
  215. draw.style.setProperty('transform', '')
  216. zoom = 0
  217. this.children[0].classList.remove('glyphicon-zoom-out')
  218. this.children[0].classList.add('glyphicon-zoom-in')
  219. }
  220. }
  221. let colors = ['#FFF', '#000', '#7f7f7f', '#c3c3c3', '#880015', '#ed1c24', '#ff7f27', "#fff200", "#22b14c", "#00a2e8", "#3f48cc", "#a349a4"]
  222. for (let i = 0; i < colors.length; i++) {
  223. let colorItem = document.createElement("li")
  224. colorItem.className = 'color-item'
  225. colorItem.style.backgroundColor = colors[i]
  226. colorItem.onclick = function () {
  227. lcolor = colors[i]
  228. nowColor.style.backgroundColor = colors[i]
  229. }
  230. if (i === 0) {
  231. colorItem.style.border = '1px solid #000000'
  232. }
  233. colorList.appendChild(colorItem)
  234. }
  235. function changeClass(arr, className) {
  236. for (let i = 0; i < arr.length; i++) {
  237. arr[i].className = className
  238. }
  239. }
  240. upload.onclick = function () {
  241. if (curVer != 0) {
  242. alert('对历史版本的编辑无法保存!')
  243. return false;
  244. }
  245. var nick = prompt('请确认,可使用昵称,上传后对所有人可见。', '-')
  246. console.log(nick)
  247. if (nick) {
  248. const query = new AV.Query('paint');
  249. console.log(id)
  250. query.equalTo('id', id)
  251. query.find().then((result) => {
  252. var map = []
  253. for (let i = 0; i < box.length; i++) {
  254. map[i] = box[i].style.backgroundColor
  255. }
  256. if (result.length) {
  257. result[0].set('data', map)
  258. result[0].set('nickName', nick)
  259. result[0].save()
  260. console.log('Updated')
  261. }
  262. else {
  263. const up = new AV.Object('paint');
  264. up.set('data', map)
  265. up.set('id', id)
  266. up.set('nickName', nick)
  267. up.save()
  268. console.log('Created new instance')
  269. }
  270. })
  271. document.getElementById('version').innerHTML = "当前版本:" + (curVer == 0 ? '' : '-') + curVer + ' by ' + nick;
  272. }
  273. }
  274. })