Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. <!doctype html>
  2. <html>
  3.  
  4. <head>
  5. <title>rectExample</title>
  6. <meta charset='utf-8' />
  7. </head>
  8.  
  9. <body>
  10. <canvas id='example'>Обновите браузер</canvas>
  11. <script type="text/javascript">
  12. function collides(rects, x, y) {
  13. var isCollision = false;
  14. for (var i = 0, len = rects.length; i < len; i++) {
  15. var left = rects[i].x,
  16. right = rects[i].x + rects[i].w;
  17. var top = rects[i].y,
  18. bottom = rects[i].y + rects[i].h;
  19. if (right >= x &&
  20. left <= x &&
  21. bottom >= y &&
  22. top <= y) {
  23. isCollision = rects[i];
  24. }
  25. }
  26. return isCollision;
  27. }
  28.  
  29. function makeMosaic(ElemsCols, ElemsRows, CanvasWidth, CanvasHeigth) {
  30. function isFloat(n) {
  31. return n === +n && n !== (n | 0);
  32. }
  33. var example = document.getElementById("example"),
  34. ctx = example.getContext('2d');
  35. ctx.canvas.width = CanvasWidth; //window.innerWidth;
  36. ctx.canvas.height = CanvasHeigth; //window.innerHeight;
  37. var width = screen.width;
  38. var height = screen.height;
  39. //alert(width);
  40. //alert(height);
  41. var marginLeft = (width / 2);
  42. var marginTop = (height / 2);
  43. //alert(marginLeft);
  44. var BgHeight = marginTop - 50;
  45. var BgWidth = marginLeft - 50;
  46. var PosX = ctx.canvas.width / 2 - BgWidth / 2;
  47. var PosY = ctx.canvas.height / 2 - BgHeight / 2;
  48.  
  49. //cxt.drawImage(image,
  50. // canvas.width / 2 - image.width / 2,
  51. // canvas.height / 2 - image.height / 2
  52. //ctx.fillRect(posX,posY, 20, 20);
  53. //ctx.strokeRect(posY - 50, 5, 50, 50);
  54. ctx.lineWidth = 5;
  55. ctx.strokeRect(PosX, PosY, BgWidth, BgHeight);
  56. var ElemsWidth = ElemsCols; //кол-во элементов в ширь
  57. var ElemsHeight = ElemsRows; // в высь
  58.  
  59.  
  60. var elmHeight = BgHeight / ElemsHeight;
  61. var elmWidth = BgWidth / ElemsWidth;
  62.  
  63. if (ElemsHeight == 1) {
  64. elmHeight = (BgWidth / 4) * 3;
  65. elmHeight = (elmHeight / ElemsWidth);
  66. }
  67. if (ElemsWidth == 1) {
  68. elmWidth = (BgHeight / 3) * 4;
  69. elmWidth = (elmWidth / ElemsHeight);
  70. }
  71.  
  72. var topStep = PosY;
  73. var leftStep = PosX;
  74.  
  75. if (isFloat(elmHeight)) {
  76. elmHeight = Math.round(elmHeight);
  77. }
  78.  
  79. if (isFloat(elmWidth)) {
  80. elmWidth = Math.round(elmWidth);
  81. }
  82.  
  83.  
  84. var rects = [];
  85.  
  86. var i, k = 0;
  87. ctx.lineWidth = 1;
  88. //document.write('<MAP NAME="mosaic_editor">');
  89. for (i = 0; i != ElemsHeight; i++) {
  90. //for ($i = 0; $i != $height_elm; $i++) {
  91. for (k = 0; k != ElemsWidth; k++) {
  92. ctx.strokeRect(leftStep, topStep, elmWidth, elmHeight);
  93. var rect = {
  94. x: leftStep,
  95. y: topStep,
  96. w: elmWidth,
  97. h: elmHeight
  98. }
  99. rects.push(rect);
  100. //<AREA SHAPE="RECT" COORDS="0, 0, 16, 16" HREF="S1.html">
  101. //document.write('<AREA style="display:block;" SHAPE="RECT" COORDS="'+leftStep+','+topStep+','+elmWidth+','+elmHeight+'" HREF="?'+leftStep+'" />');
  102. console.log("left = " + leftStep + "topStep =" + topStep + "Width = " + elmWidth + "Height =" + elmHeight);
  103. leftStep += elmWidth;
  104. //for ($k = 0; $k != $width_elm; $k++) {
  105. }
  106. topStep += elmHeight;
  107. leftStep = PosX;
  108. }
  109. //document.write('</MAP>');
  110. example.addEventListener('click', function(e) {
  111. console.log('click: ' + e.offsetX + '/' + e.offsetY);
  112. var rect = collides(rects, e.offsetX, e.offsetY);
  113. if (rect) {
  114. console.log('collision: ' + rect.x + '/' + rect.y);
  115. } else {
  116. console.log('no collision');
  117. }
  118. }, false);
  119. }
  120. makeMosaic(3, 3, window.innerWidth, window.innerHeight);
  121. </script>
  122. </body>
  123.  
  124. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement