Guest User

Untitled

a guest
Jul 30th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Javascript</title>
  6. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  7. </head>
  8. <body onload="init();">
  9. <div style="width:700px;">
  10. <div id="status" style="width:65px;position:absolute;padding:10px;background:#111;color:#FFF;"></div>
  11. <canvas id="Canvas" height="300" width="300" style="border:1px solid black;position:relative;float:left;"></canvas>
  12. <div id="brushbase" style="width:80px; height:600px;border:1px solid #eee;position:relative;float:left;display:block;">
  13. <div id="slidearea" style="position:absolute;margin:30px;width:20px;height:540px;background:#eee;">
  14. </div>
  15. <div id="slider" style="position:absolute;margin:25px;width:30px;height:20px;background:#bbb;cursor:pointer;">
  16. <p id="check"></p>
  17. </div>
  18. </div>
  19. <div id="colorsbase" style="width:600px;border:1px solid #eee; height:80px;position:relative;float:left;">
  20. <ul id="colors">
  21. <li class="col" id="col0" onclick="pen.color='red'"></li>
  22. <li class="col" id="col1" onclick="pen.color='green'"></li>
  23. <li class="col" id="col2" onclick="pen.color='blue'"></li>
  24. <li class="col" id="col3" onclick="pen.color='black'"></li>
  25. <li class="col" id="col4" onclick="pen.color='orange'"></li>
  26. <li class="col" id="col5" onclick="pen.color='white'"></li>
  27. </ul>
  28. <div id="current" style="position:absolute;right:0px;height:70px;width:70px;background:#111;margin:5px;color:white;text-align:center;font-family:arial;">
  29. <text style="padding-top:5px;">Current</text>
  30. </div>
  31. </div>
  32. </div>
  33. <style>
  34. ul#colors
  35. {
  36. padding:0px;
  37. margin:0px;
  38. //display:inline;
  39. position:absolute;
  40. }
  41. li.col
  42. {
  43. display:inline-block;
  44. border:1px solid black;
  45. list-style-type:none;
  46. width:70px;
  47. height:70px;
  48. margin-left:5px;
  49. margin-top:5px;
  50. cursor:pointer;
  51. }
  52. .col
  53. {
  54. }
  55.  
  56. #col0{background:red;}
  57. #col1{background:green;}
  58. #col2{background:blue;}
  59. #col3{background:black;}
  60. #col4{background:orange;}
  61. #col5{background:white;}
  62.  
  63. </style>
  64. <script>
  65. function controls()
  66. {
  67. moveslider();
  68. document.getElementById('brushbase').addEventListener('mousemove',SlideMove,true);
  69. document.getElementById('brushbase').addEventListener('mousedown',SlideDown,true);
  70. document.addEventListener('mouseup',SlideUp,true);
  71. }
  72.  
  73. var mouse_down = "false";
  74.  
  75. function moveslider()
  76. {
  77. if (mouse_down=="true" && $('#slider').offset().top>=30 && $('#slider').offset().top<=560)
  78. {
  79. $('#slider').css("top",control.y-34);
  80. if ($('#slider').offset().top<30){$('#slider').css("top",-4);}
  81. if ($('#slider').offset().top>560){$('#slider').css("top",526);}
  82.  
  83. }
  84. }
  85.  
  86. var control =
  87. {
  88. y:0,
  89. }
  90.  
  91. function SlideMove(evt)
  92. {
  93. var root = document.documentElement;
  94. control.y = evt.clientY - 10 - root.scrollTop;
  95. }
  96.  
  97.  
  98. function SlideDown()
  99. {
  100. mouse_down = "true";
  101. }
  102.  
  103. function SlideUp()
  104. {
  105. mouse_down = "false";
  106. }
  107.  
  108. </script>
  109. <script>
  110. // ~~~~~~~~ VARIABLES ~~~~~~~~~~
  111. var HEIGHT=600;
  112. var WIDTH=600;
  113. var c = document.getElementById('Canvas');
  114. var ctx = c.getContext('2d');
  115. // ~~~~~~~~~ OBJECTS ~~~~~~~~~~~
  116. var mouse =
  117. {
  118. x:0,
  119. y:0,
  120. clicked:'no',
  121. }
  122.  
  123. var pen =
  124. {
  125. array:[],
  126. size : 2, //radius
  127. color: "black",
  128. }
  129.  
  130. // ~~~~~~~~~~~ DRAW ~~~~~~~~~~~~~~
  131. function render()
  132. {
  133. ctx.fillStyle = 'black';
  134.  
  135. document.getElementById('status').innerHTML = mouse.x + ' ' + mouse.y + '<br/>' + mouse.clicked + ' ' + pen.array.length;
  136.  
  137. drawPen();
  138. Gaps();
  139. }
  140.  
  141. function drawPen()
  142. {
  143. if (pen.array.length>0)
  144. {
  145. for (i=0;i<pen.array.length;i++)
  146. {
  147. ctx.beginPath();
  148. ctx.arc(pen.array[i][0], pen.array[i][1],pen.size,0,Math.PI*2,false);
  149. ctx.fillStyle = pen.color;
  150. ctx.fill();
  151. }
  152. }
  153. }
  154.  
  155. function Gaps()
  156. {
  157. for (i=1;i<pen.array.length;i++)
  158. {
  159. if (pen.array[i-1][0]%pen.array[i][0]>2 || pen.array[i-1][1]%pen.array[i][1]>2)
  160. {
  161. ctx.beginPath();
  162. ctx.moveTo(pen.array[i-1][0],pen.array[i-1][1]);
  163. ctx.quadraticCurveTo((pen.array[i-1][0]+pen.array[i][0])/2,(pen.array[i-1][1]+pen.array[i][1])/2,pen.array[i][0],pen.array[i][1]);
  164. ctx.lineWidth=pen.size*2;
  165. ctx.strokeStyle=pen.color;
  166. ctx.stroke();
  167. }
  168. }
  169. }
  170. // ~~~~~~~~~~~~ LOGIC ~~~~~~~~~~~~~~~
  171. function logic()
  172. {
  173. Draw();
  174. Cleardups();
  175. Current();
  176. Size();
  177. }
  178.  
  179. function Size()
  180. {
  181. pen.size = 10/100*($('#slider').offset().top-34)+1;
  182. }
  183.  
  184. function Current()
  185. {
  186. document.getElementById('current').style.background = pen.color;
  187. if (pen.color == "white" || pen.color == "orange")
  188. {
  189. document.getElementById('current').style.color = "black";
  190. }
  191. else if (pen.color == "red" || pen.color == "green" || pen.color == "black")
  192. {
  193. document.getElementById('current').style.color = "white";
  194. }
  195. }
  196.  
  197. function MouseMove(evt)
  198. {
  199. var rect = c.getBoundingClientRect(), root = document.documentElement;
  200.  
  201. // return relative mouse position
  202. mouse.x = evt.clientX - rect.top - root.scrollTop;
  203. mouse.y = evt.clientY - rect.left - root.scrollLeft;
  204.  
  205. }
  206.  
  207. function MouseDown()
  208. {
  209. mouse.clicked="yes";
  210. }
  211.  
  212. function MouseUp()
  213. {
  214. pen.array.length = 0;
  215. mouse.clicked="no";
  216. }
  217.  
  218. function Draw()
  219. {
  220. if (mouse.clicked=="yes")
  221. {
  222. pen.array.push([mouse.x,mouse.y,pen.color]);
  223. }
  224. }
  225.  
  226. function Cleardups()
  227. {
  228. for (i=0;i<pen.array.length;i++)
  229. {
  230. if (pen.array[i+1] && pen.array[i][0]==pen.array[i+1][0] && pen.array[i+1] && pen.array[i][1]==pen.array[i+1][1])
  231. {
  232. pen.array.splice(i,1);
  233. }
  234. }
  235. }
  236. // ~~~~~~~~~~~~~~~~ MECHANICS ~~~~~~~~~~~~~~~~
  237.  
  238. function init()
  239. {
  240. BuildCanvas();
  241. c.addEventListener('mousemove',MouseMove,true);
  242. c.addEventListener('mousedown',MouseDown,true);
  243. window.addEventListener('mouseup',MouseUp,true);
  244.  
  245. intervalID = setInterval(play,1);
  246. }
  247.  
  248.  
  249. function play()
  250. {
  251. logic();
  252. render();
  253. controls();
  254. }
  255.  
  256. function BuildCanvas()
  257. {
  258. c.height=HEIGHT;
  259. c.width=WIDTH;
  260. }
  261.  
  262.  
  263. </script>
  264. </body>
  265. </html>
Advertisement
Add Comment
Please, Sign In to add comment