code.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Propriétés
  2. var canvas = document.querySelector('#canvas');
  3. var context = canvas.getContext('2d');
  4. var wSize = 800;
  5. var hSize = 600;
  6. context.font = "bold 22pt Calibri,Geneva,Arial";
  7. // Variables
  8. var dimFocus = 15;
  9. var mouseX = 0, mouseY = 0, mouseClic = false; // Mouse state
  10. var baliseXArray = new Array(), baliseYArray = new Array(), baliseArrayLenght = 0;
  11. // Fonctions
  12. function clear() {
  13. context.fillStyle = "black";
  14. context.fillRect(0, 0, wSize, hSize);
  15. }
  16. function drawPic(x, y) {
  17. context.strokeStyle = "rgb(23, 145, 167)";
  18. context.beginPath();
  19. context.moveTo(x, y);// 1er point
  20. context.lineTo(x+25, y-25);// 2e point
  21. context.lineTo(x+25, y-50);// 3e
  22. context.lineTo(x-25, y-50);// 4e
  23. context.lineTo(x-25, y-25);// 5e
  24. context.closePath();
  25. context.stroke();
  26. }
  27. function drawFocus(x, y) {
  28. context.strokeStyle = "rgb(123, 45, 167)";
  29. context.lineWidth = "5";
  30. context.beginPath();
  31. context.moveTo(0, 0);
  32. context.lineTo(x-dimFocus, y-dimFocus);
  33. context.moveTo(wSize, 0);
  34. context.lineTo(x+dimFocus, y-dimFocus);
  35. context.moveTo(wSize, hSize);
  36. context.lineTo(x+dimFocus, y+dimFocus);
  37. context.moveTo(0, hSize);
  38. context.lineTo(x-dimFocus, y+dimFocus);
  39. context.stroke();
  40. }
  41. function drawText(x, y, text) {
  42. context.fillStyle = "#fff";
  43. context.fillText(text, x, y);
  44. }
  45. // Gestion des évènements
  46. // Souris
  47. document.addEventListener('mousemove', function(e) {
  48. mouseX = e.clientX;
  49. mouseY = e.clientY;
  50. }, false);
  51. document.addEventListener('mousedown', function(e) {
  52. mouseClic = true;
  53. }, false);
  54. document.addEventListener('mouseup', function(e) {
  55. mouseClic = false;
  56. baliseXArray.push(mouseX);
  57. baliseYArray.push(mouseY);
  58. }, false);
  59. // Smartphone
  60. document.addEventListener('touchstart', function(e) {
  61. drawPic(200, 100);
  62. }, false);
  63. // Main
  64. function mainLoop() {
  65. //Nettoyage
  66. clear();
  67. //Curseurs
  68. drawFocus(mouseX, mouseY);
  69. //Placement balise en cours
  70. if (mouseClic) {
  71. drawPic(mouseX, mouseY);
  72. }
  73. //Affichage de toutes les balises
  74. baliseArrayLenght = baliseXArray.length;
  75. for (var i = 0; i < baliseArrayLenght; i++) {
  76. drawPic(baliseXArray[i], baliseYArray[i]);
  77. }
  78. //Affichage effectif
  79. drawText(2, 24, baliseArrayLenght + " balises");
  80. }
  81. //Loop
  82. setInterval(mainLoop, 20);