BLUSHIF

Untitled

Nov 5th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <style>
  5. body{ background-color: ivory; }
  6. canvas{border:1px solid red;}
  7. </style>
  8. <script>
  9. document.addEventListener("DOMContentLoaded", function(event) {
  10. var b=document.getElementById("canvas");
  11. var canvas=document.getElementById("canvas");
  12. var ctx=canvas.getContext("2d");
  13. ctx.strokeStyle="lightgray";
  14.  
  15. var offsetX=b.offsetLeft;
  16. var offsetY=b.offsetTop;
  17.  
  18. var mouseIsDown=false;
  19. var lastX=0;
  20. var lastY=0;
  21. ctx.canvas.width = window.innerWidth-40;
  22. ctx.canvas.height = window.innerHeight-40;
  23. var Boxes=[];
  24. makebox(20,30,50,25,"skyblue",2,2);
  25. makebox(20,100,30,25,"skyblue",2,2);
  26. makebox(20,170,50,25,"salmon",2,2);
  27. makebox(20,240,30,25,"salmon",2,2);
  28. function sidebar(){
  29. ctx.fillRect(0+canvas.width*.75, 0, canvas.width/4, canvas.height);
  30. }
  31. function makebox(x,y,width,height,fill,iv,ov){
  32. var Box={
  33. x:x,
  34. y:y,
  35. width:width,
  36. height:height,
  37. right:x+width,
  38. bottom:y+height,
  39. fill:fill,
  40. inputvals:iv,
  41. outputvals:ov
  42. }
  43. Boxes.push(Box);
  44. return(Box);
  45. }
  46.  
  47. drawAllboxes();
  48.  
  49. function drawAllboxes(){
  50. ctx.clearRect(0,0,canvas.width,canvas.height);
  51. for(var i=0;i<Boxes.length;i++){
  52. var Box=Boxes[i]
  53. drawbox(Box);
  54. ctx.fillStyle=Box.fill;
  55. ctx.fill();
  56. ctx.stroke();
  57. }
  58. }
  59.  
  60. function drawbox(Box){
  61. var lch;
  62. if(Box.inputvals>Box.outputvals){
  63. lch=Box.inputvals;
  64. }else{
  65. if(Box.inputvals==Box.outputvals){
  66. lch=Box.inputvals;
  67. }else{
  68. lch=Box.outputvals;
  69. }
  70. }
  71. ctx.beginPath();
  72. ctx.fillStyle = Box.fill;
  73. ctx.fill();
  74. ctx.fillRect(Box.x,Box.y, Box.width, Box.height+lch*20);
  75. ctx.closePath();
  76. for(var i=0;i<Box.inputvals;i++){
  77. ctx.beginPath();
  78. ctx.arc(Box.x, Box.y+10+i*20, 1, 0, 2 * Math.PI, false);
  79. ctx.lineWidth = 5;
  80. ctx.strokeStyle = 'green';
  81. ctx.stroke();
  82. ctx.closePath();
  83. }
  84. for(var i=0;i<Box.outputvals;i++){
  85. ctx.beginPath();
  86. ctx.arc(Box.right, Box.y+10+i*20, 1, 0, 2 * Math.PI, false);
  87. ctx.lineWidth = 5;
  88. ctx.strokeStyle = 'blue';
  89. ctx.stroke();
  90. ctx.closePath();
  91. }
  92. }
  93.  
  94. function handleMouseDown(e){
  95. mouseX=parseInt(e.clientX-offsetX);
  96. mouseY=parseInt(e.clientY-offsetY);
  97. lastX=mouseX;
  98. lastY=mouseY;
  99. mouseIsDown=true;
  100. }
  101.  
  102. function handleMouseUp(e){
  103. mouseX=parseInt(e.clientX-offsetX);
  104. mouseY=parseInt(e.clientY-offsetY);
  105. mouseIsDown=false;
  106. }
  107.  
  108. function handleMouseMove(e){
  109. if(!mouseIsDown){ return; }
  110. mouseX=parseInt(e.clientX-offsetX);
  111. mouseY=parseInt(e.clientY-offsetY);
  112. for(var i=0;i<Boxes.length;i++){
  113. var Box=Boxes[i];
  114. drawbox(Box);
  115. if(ctx.isPointInPath(lastX,lastY)){
  116. Box.x+=(mouseX-lastX);
  117. Box.y+=(mouseY-lastY);
  118. Box.right=Box.x+Box.width;
  119. Box.bottom=Box.y+Box.height;
  120. }
  121. }
  122. lastX=mouseX;
  123. lastY=mouseY;
  124. drawAllboxes();
  125. }
  126. b.addEventListener('mousedown',function(e){handleMouseDown(e);});
  127. b.addEventListener('mousemove',function(e){handleMouseMove(e);});
  128. b.addEventListener('mouseup',function(e){handleMouseUp(e);});
  129.  
  130. });
  131. </script>
  132. </head>
  133. <body>
  134. <canvas id="canvas" width="640" height="480"></canvas>
  135. </body>
  136. </html>
Advertisement
Add Comment
Please, Sign In to add comment