You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
6.2 KiB

4 years ago
  1. <!DOCTYPE html>
  2. <html ng-app="myApp">
  3. <head>
  4. <style type="text/css">
  5. body{
  6. margin: 8px;
  7. background: gray;
  8. }
  9. #menu{
  10. width: 100px;
  11. margin-top: 28px;
  12. text-align: center;
  13. }
  14. #board{
  15. cursor: crosshair;
  16. border: 1px solid black;
  17. background: white;
  18. }
  19. div.pixel{
  20. position: absolute;
  21. width: 8px;
  22. height: 8px;
  23. }
  24. div.pixel:hover{
  25. opacity: .5;
  26. }
  27. </style>
  28. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
  29. <script src="js/filesaver.js"></script>
  30. <script type="text/javascript">
  31. var decodeBase64 = function(s) {
  32. var e={},i,b=0,c,x,l=0,a,r='',w=String.fromCharCode,L=s.length;
  33. var A="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  34. for(i=0;i<64;i++){e[A.charAt(i)]=i;}
  35. for(x=0;x<L;x++){
  36. c=e[s.charAt(x)];b=(b<<6)+c;l+=6;
  37. while(l>=8){((a=(b>>>(l-=8))&0xff)||(x<(L-2)))&&(r+=w(a));}
  38. }
  39. return r;
  40. };
  41. var app = angular.module("myApp", []);
  42. app.directive('appFilereader', function($q) {
  43. var slice = Array.prototype.slice;
  44. return {
  45. restrict: 'A',
  46. require: '?ngModel',
  47. link: function(scope, element, attrs, ngModel) {
  48. if (!ngModel) return;
  49. ngModel.$render = function() {};
  50. element.bind('change', function(e) {
  51. var element = e.target;
  52. $q.all(slice.call(element.files, 0).map(readFile))
  53. .then(function(values) {
  54. if (element.multiple) ngModel.$setViewValue(values);
  55. else ngModel.$setViewValue(values.length ? values[0] : null);
  56. });
  57. function readFile(file) {
  58. var deferred = $q.defer();
  59. var reader = new FileReader();
  60. reader.onload = function(e) {
  61. deferred.resolve(e.target.result);
  62. };
  63. reader.onerror = function(e) {
  64. deferred.reject(e);
  65. };
  66. reader.readAsDataURL(file);
  67. return deferred.promise;
  68. }
  69. }); //change
  70. } //link
  71. }; //return
  72. })
  73. app.controller("myCtrl", function($scope,$interval) {
  74. $scope.size = 1;
  75. $scope.pixels= [];
  76. $scope.click = false;
  77. $scope.color = "#000";
  78. $scope.mouse = {
  79. x: 0,
  80. y: 0
  81. }
  82. var board = document.getElementById("board");
  83. $(board).css({width:'200px', height:'200px'});
  84. var ctx = board.getContext("2d");
  85. var max = { x: board.width, y: board.height};
  86. var id = 1;
  87. for(var i = 0; i<max.y; i++){
  88. for(var j = 0; j<max.x; j++){
  89. $scope.pixels.push({id: id,position:{x: j, y: i}, color: '#fff'});
  90. id++;
  91. }
  92. }
  93. var painter = {
  94. draw: function(pixel) {
  95. if (board.getContext) {
  96. angular.forEach($scope.pixels, function(value, key) {
  97. ctx.fillStyle = pixel.color;
  98. ctx.fillRect(pixel.position.x, pixel.position.y, $scope.size, $scope.size);
  99. });
  100. }
  101. },
  102. update: function(){
  103. angular.forEach($scope.pixels, function(value, key) {
  104. value.color = 'black';
  105. console.log(22);
  106. });
  107. },
  108. clear: function() {
  109. if (board.getContext) {
  110. ctx.clearRect(0,0, board.width, board.height);
  111. }
  112. }
  113. };
  114. /* $interval(function(){
  115. angular.forEach($scope.pixels, function(value, key) {
  116. squareDrawer.clear();
  117. squareDrawer.draw(value);
  118. });
  119. }, 1000);*/
  120. board.onmousedown = function(e) {
  121. painter.update();
  122. painter.draw();
  123. }
  124. board.onmousemove = function(e) {
  125. var rect = this.getBoundingClientRect();
  126. $scope.mouse = {
  127. x : e.clientX - rect.left,
  128. y : e.clientY - rect.top
  129. }
  130. //ctx.clearRect(0, 0, board.width, board.height); // for demo
  131. /*
  132. while(r = $scope.pixels[i++]) {
  133. // add a single rect to path:
  134. ctx.beginPath();
  135. ctx.rect(r.position.x, r.position.y, $scope.size, $scope.size);
  136. // check if we hover it, fill red, if not fill it blue
  137. ctx.fillStyle = ctx.isPointInPath(x, y) ? "red" : "blue";
  138. ctx.fill();
  139. }
  140. */
  141. };
  142. });
  143. </script>
  144. </head>
  145. <body ng-controller="myCtrl">
  146. <canvas id="board"></canvas>
  147. </body>
  148. </html>