Guest User

Untitled

a guest
Oct 21st, 2019
68
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <HTML>
  2. <BODY>
  3. <canvas id="brezench" width="500" height="400">
  4. </canvas>
  5. <script>
  6. function Line(idata, x0, y0, x1, y1) {
  7. var flag = 1;
  8. if (x1 < x0) {
  9. var s = x0;
  10. x0 = x1;
  11. x1 = s;
  12. s = y0;
  13. y0 = y1;
  14. y1 = s;
  15. }
  16. if (y0 > y1) {
  17. s = y0;
  18. y0 = y1;
  19. y1 = s;
  20. flag = -1;
  21. }
  22. if (flag == 1) {
  23. var y = y0;
  24. }
  25. else {
  26. var y = y1;
  27. }
  28. var eps = 0;
  29. var k=2*(y1-y0);
  30. var pw=1;
  31. for (var x=x0; x<=x1; x++) {
  32. idata.data[(x+y*idata.width)*4+3]=255*
  33. Math.pow(Math.abs(0.5- (eps/(2*(x1-x0))) ),pw);
  34. idata.data[(x+(y+1)*idata.width)*4+3]=255*
  35. Math.pow(Math.abs(0.5+ (eps/(2*(x1-x0))) ), pw);
  36. eps = eps+k;
  37. if (eps>(x1-x0)) {
  38. y += flag;
  39. eps = eps - 2*(x1-x0);
  40. }
  41. }
  42.  
  43. }
  44. </script>
  45. <script>
  46. var canvas = document.getElementById("brezench");
  47. var ctx = canvas.getContext("2d");
  48.  
  49. var flag = 0;
  50. var idata = ctx.getImageData(0, 0, canvas.width, canvas.height);
  51. console.log(idata);
  52. canvas.addEventListener("click", function(event){
  53. if (!flag) {
  54. x0 = event.offsetX;
  55. y0 = event.offsetY;
  56. flag = 1;
  57. } else {
  58. x1 = event.offsetX;
  59. y1 = event.offsetY;
  60. Line(idata, x0, y0, x1, y1);
  61. ctx.putImageData(idata, 0, 0);
  62. flag = 0;
  63. }
  64. });
  65.  
  66. </script>
  67. </BODY>
  68. </HTML>
RAW Paste Data