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.

149 lines
5.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. }
  17. div.pixel{
  18. position: absolute;
  19. width: 8px;
  20. height: 8px;
  21. }
  22. div.pixel:hover{
  23. opacity: .5;
  24. }
  25. </style>
  26. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
  27. <script src="js/filesaver.js"></script>
  28. <script type="text/javascript">
  29. var decodeBase64 = function(s) {
  30. var e={},i,b=0,c,x,l=0,a,r='',w=String.fromCharCode,L=s.length;
  31. var A="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  32. for(i=0;i<64;i++){e[A.charAt(i)]=i;}
  33. for(x=0;x<L;x++){
  34. c=e[s.charAt(x)];b=(b<<6)+c;l+=6;
  35. while(l>=8){((a=(b>>>(l-=8))&0xff)||(x<(L-2)))&&(r+=w(a));}
  36. }
  37. return r;
  38. };
  39. var app = angular.module("myApp", []);
  40. app.directive('appFilereader', function($q) {
  41. var slice = Array.prototype.slice;
  42. return {
  43. restrict: 'A',
  44. require: '?ngModel',
  45. link: function(scope, element, attrs, ngModel) {
  46. if (!ngModel) return;
  47. ngModel.$render = function() {};
  48. element.bind('change', function(e) {
  49. var element = e.target;
  50. $q.all(slice.call(element.files, 0).map(readFile))
  51. .then(function(values) {
  52. if (element.multiple) ngModel.$setViewValue(values);
  53. else ngModel.$setViewValue(values.length ? values[0] : null);
  54. });
  55. function readFile(file) {
  56. var deferred = $q.defer();
  57. var reader = new FileReader();
  58. reader.onload = function(e) {
  59. deferred.resolve(e.target.result);
  60. };
  61. reader.onerror = function(e) {
  62. deferred.reject(e);
  63. };
  64. reader.readAsDataURL(file);
  65. return deferred.promise;
  66. }
  67. }); //change
  68. } //link
  69. }; //return
  70. })
  71. app.controller("myCtrl", function($scope) {
  72. $scope.size = 4;
  73. $scope.pixels= [];
  74. $scope.click = false;
  75. $scope.color = "#000"
  76. var max = { x: 100, y: 100};
  77. var id = 1;
  78. for(var i = 1; i<max.y; i++){
  79. for(var j = 1; j<max.x; j++){
  80. $scope.pixels.push({id: id,position:{x: j, y: i}, color: '#fff'});
  81. id++;
  82. }
  83. }
  84. $scope.painter = function(pixel){
  85. if($scope.click){
  86. pixel.color = $scope.color;
  87. }
  88. };
  89. $scope.export = function(){
  90. var text = JSON.stringify($scope.pixels);
  91. var element = document.createElement('a');
  92. element.setAttribute('href', 'data:json/plain;charset=utf-8,' + encodeURIComponent(text));
  93. element.setAttribute('download', 'painter.jgf');
  94. element.style.display = 'none';
  95. document.body.appendChild(element);
  96. element.click();
  97. document.body.removeChild(element);
  98. };
  99. $scope.import = function(file){
  100. $scope.pixels = angular.fromJson(decodeBase64(file.split(',')[1]));
  101. };
  102. });
  103. </script>
  104. </head>
  105. <body ng-controller="myCtrl">
  106. <div id="menu">
  107. <input type="color" ng-model="color"/>
  108. <button ng-click="export()">
  109. Exportar
  110. </button>
  111. <input type="file" ng-model="file" accept=".jgf" app-filereader ng-change="import(file)"/>
  112. </div>
  113. <div id="board" ng-mousedown="click = true;" ng-mouseup="click = false">
  114. <div ng-repeat="pixel in pixels"
  115. ng-mouseover="painter(pixel)"
  116. class="pixel"
  117. style="
  118. left: {{(pixel.position.x * size) + 120 }}px;
  119. top: {{(pixel.position.y * size) + 20}}px;
  120. background: {{pixel.color}};
  121. width: {{size}}px;
  122. height: {{size}}px;"
  123. >
  124. </div>
  125. </div>
  126. </body>
  127. </html>