Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Rysowanie rectangle
Forum PHP.pl > Forum > Po stronie przeglądarki
rad11
Panowie mam problem i nie wiem jak go rozwiazac mam nadzieje ze cos podpowiecie:

Mam kontener:
  1. <div id="myCanvas" style="position:relative;height:250px;width:100%;"></div>


i rysuje na nim rectangle:

  1. function drawRectangle(x,y,width,height){
  2.  
  3. var length = $('#myCanvas').children('div').length;
  4.  
  5. $('#myCanvas').append('<div class="rectangle'+length+'" style="border: 3px solid red; top:'+y+'; left:'+x+';position:absolute;"></div>');
  6.  
  7. }
  8.  
  9. var x;
  10. var y;
  11. var width;
  12. var height;
  13. var mouseDownX;
  14. var mouseDownY;
  15.  
  16. $('#myCanvas').on('mousedown', function(e){
  17.  
  18. var offset = $("#myCanvas").offset();
  19. mouseDownX = e.pageX - offset.left;
  20. mouseDownY = e.pageY - offset.top;
  21.  
  22.  
  23.  
  24. $(this).on('mousemove', function(e){
  25. var offset = $("#myCanvas").offset();
  26. var upX = e.pageX - offset.left;
  27. var upY = e.pageY - offset.top;
  28.  
  29. var width = upX - mouseDownX;
  30. var height = upY - mouseDownY;
  31. var length = $('#myCanvas').children('div').length;
  32.  
  33.  
  34.  
  35. $('.rectangle').css({
  36. width: width,
  37. height: height
  38. });
  39. });
  40. drawRectangle(mouseDownX, mouseDownY);
  41.  
  42. });
  43.  
  44.  
  45. $('#myCanvas').on('mouseup', function(e){
  46. $('#myCanvas').unbind('mousemove');
  47. });


Nie wiem jak rozwiazac aby dodanie css dotyczylo aktualnie rysowanego rectangla jakies pomysly?
markuz
Nadajesz mu klasę "rectangle'+length" a następnie zmieniasz css-a dla ".rectangle".. Możesz sobie dodać na samym początku var id = 0; potem przy rysowaniu id = length; i przy zmianie css ".rectangle'+id" - powinno działać.
rad11
Poradzilem sobie z tym ale pojawil sie kolejny problem, czy istnieje mozliwosc po zakonczeniu rysowania pokazania dialogu ? tzn pokazac dialog kiedy odklikujemy ?

Poradzilem sobie tak ale nie wiem czy to dobre rozwiazanie:

  1.  
  2. function drawRectangle(x,y){
  3. var length = $('.viewport').children('div.rectangle').length;
  4. var length_marker = $('.marker').length
  5. length_marker = length_marker+1;
  6. x_marker = x - 12;
  7. y_marker = y - 29;
  8. $('.viewport').append('<span class="marker black" style="top:'+y_marker+'px; left:'+x_marker+'px;" display:none;>'+length_marker+'</span><div class="rectangle rectangle'+length+'" style="border: 3px solid red; top:'+y+'px; left:'+x+'px;position:absolute;"></div>');
  9.  
  10. return true;
  11. }
  12.  
  13. var x;
  14. var y;
  15. var width;
  16. var height;
  17. var mouseDownX;
  18. var mouseDownY;
  19. var drawed;
  20. $('.viewport').on('mousedown', function(e){
  21. var offset = $('.viewport').offset();
  22. mouseDownX = e.pageX - offset.left;
  23. mouseDownY = e.pageY - offset.top;
  24.  
  25. if (e.button == 2) {
  26. return false;
  27. }
  28.  
  29. $(this).on('mousemove', function(e){
  30. var offset = $('.viewport').offset();
  31. var upX = e.pageX - offset.left;
  32. var upY = e.pageY - offset.top;
  33. width = upX - mouseDownX;
  34. height = upY - mouseDownY;
  35. var length = $('.viewport').children('div.rectangle').length;
  36. length = length-1;
  37.  
  38. $('div.rectangle'+length).css({
  39. width: width,
  40. height: height
  41. });
  42. // $('.line'+length).css({
  43. // width: width
  44. // });
  45. });
  46. drawed = drawRectangle(mouseDownX, mouseDownY);
  47. // drawLine(mouseDownX, mouseDownY);
  48.  
  49. $('.viewport').on('mouseup', function(e){
  50.  
  51. if (drawed == true) {
  52. $('<div id="NoteDialog"></div>').dialog({
  53. open: function () {
  54. $(this).html('<div>'+width+'</div>');
  55. }
  56. });
  57. }
  58. $('.viewport').unbind('mousemove');
  59. });
  60.  
  61. });
  62.  
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.