A pixel art painter geared specifically at NES pixel art. Includes export for .chr binary file as well as palette and namespace data.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

496 行
14KB

  1. import Utils from "/app/js/common/Utils.js";
  2. import GlobalEvents from "/app/js/common/EventCaller.js";
  3. import Input from "/app/js/ui/Input.js";
  4. import NESPalette from "/app/js/models/NESPalette.js";
  5. import ISurface from "/app/js/ifaces/ISurface.js";
  6. const EL_CANVAS_ID = "painter";
  7. /* --------------------------------------------------------------------
  8. * Univeral data and functions.
  9. ------------------------------------------------------------------- */
  10. var canvas = null;
  11. var context = null;
  12. var ctximg = null;
  13. function OpenCanvasPixels(){
  14. if (context !== null){
  15. if (ctximg === null){
  16. ctximg = context.getImageData(0,0,Math.floor(canvas.clientWidth),Math.floor(canvas.clientHeight));
  17. }
  18. return (ctximg !== null)
  19. }
  20. return false;
  21. }
  22. function PutCanvasPixel(i,j,size,color){
  23. if (ctximg === null)
  24. return;
  25. i = Math.round(i);
  26. j = Math.round(j);
  27. size = Math.ceil(size);
  28. if (size <= 0){return;}
  29. var cw = Math.floor(canvas.clientWidth);
  30. var ch = Math.floor(canvas.clientHeight);
  31. var r = parseInt(color.substring(1, 3), 16);
  32. var g = parseInt(color.substring(3, 5), 16);
  33. var b = parseInt(color.substring(5, 7), 16);
  34. var idat = ctximg.data;
  35. for (var y=j; y < j+size; y++){
  36. for (var x=i; x < i+size; x++){
  37. if (x >= 0 && x < cw && y >= 0 && y < ch){
  38. var index = (y*cw*4) + (x*4);
  39. idat[index] = r;
  40. idat[index+1] = g;
  41. idat[index+2] = b;
  42. }
  43. }
  44. }
  45. }
  46. function CloseCanvasPixels(){
  47. if (ctximg !== null){
  48. context.putImageData(ctximg, 0, 0);
  49. ctximg = null;
  50. }
  51. }
  52. function ResizeCanvasImg(w, h){
  53. if (canvas !== null){
  54. canvas.width = w;
  55. canvas.height = h;
  56. }
  57. };
  58. // Handling window resize events...
  59. var HANDLE_Resize = Utils.debounce(function(e){
  60. if (canvas !== null){
  61. ResizeCanvasImg(
  62. canvas.clientWidth,
  63. canvas.clientHeight
  64. );
  65. GlobalEvents.emit("resize", canvas.clientWidth, canvas.clientHeight);
  66. }
  67. }, 250);
  68. window.addEventListener("resize", HANDLE_Resize);
  69. // Setting-up Input controls.
  70. var input = new Input();
  71. input.enableKeyboardInput(true);
  72. input.enableMouseInput(true);
  73. input.preventDefaults = true;
  74. /* --------------------------------------------------------------------
  75. * CTRLPainter
  76. * Actual controlling class.
  77. ------------------------------------------------------------------- */
  78. // For reference...
  79. // https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/stroke
  80. class CTRLPainter {
  81. constructor(){
  82. this.__scale = 1.0; // This is the scale the painter will display source information.
  83. this.__offset = [0.0, 0.0]; // This is the X,Y offset from origin to display source information.
  84. this.__onePaletteMode = true; // If true, ALL tiles will be drawing using the same palette.
  85. this.__brushSize = 1;
  86. this.__brushLastPos = [0.0, 0.0];
  87. this.__brushPos = [0.0, 0.0];
  88. this.__brushColor = 0;
  89. this.__brushPalette = 0;
  90. this.__gridEnabled = false;
  91. this.__gridSize = 1;
  92. this.__surface = null;
  93. this.__palette = null;
  94. // var self = this;
  95. var RenderD = Utils.throttle((function(){
  96. this.render();
  97. }).bind(this), 20);
  98. var LineToSurface = (function(x0, y0, x1, y1, ci, pi){
  99. var dx = x1 - x0;
  100. var ix = Math.sign(dx);
  101. dx = 2 * Math.abs(dx);
  102. var dy = y1 - y0;
  103. var iy = Math.sign(dy);
  104. dy = 2 * Math.abs(dy);
  105. if (dx > dy){
  106. var err = dy - (dx/2);
  107. var y = y0;
  108. Utils.range(x0, x1, 1).forEach((x) => {
  109. this.__surface.setColorIndex(x, y, ci, pi);
  110. if (err > 0 || (err == 0 && ix > 0)){
  111. err -= dx;
  112. y += iy;
  113. }
  114. err += dy;
  115. });
  116. } else {
  117. var err = dx - (dy/2);
  118. var x = x0;
  119. Utils.range(y0, y1, 1).forEach((y) => {
  120. this.__surface.setColorIndex(x, y, ci, pi);
  121. if (err > 0 || (err == 0 && iy > 0)){
  122. err -= dy;
  123. x += ix;
  124. }
  125. err += dx;
  126. });
  127. }
  128. }).bind(this);
  129. var handle_resize = (function(w,h){
  130. RenderD();
  131. }).bind(this);
  132. GlobalEvents.listen("resize", handle_resize);
  133. var handle_palinfochanged = (function(pdat){
  134. RenderD();
  135. }).bind(this);
  136. var handle_setapppalette = (function(pal){
  137. if (this.__palette !== null)
  138. this.__palette.unlisten("palettes_changed", handle_palinfochanged);
  139. this.__palette = pal;
  140. this.__palette.listen("palettes_changed", handle_palinfochanged);
  141. if (this.__surface !== null){
  142. this.__surface.palette = pal;
  143. if (this.__onePaletteMode === false)
  144. RenderD();
  145. }
  146. }).bind(this);
  147. GlobalEvents.listen("set_app_palette", handle_setapppalette);
  148. var handle_surface_data_changed = (function(){
  149. RenderD();
  150. }).bind(this);
  151. var handle_change_surface = (function(surf){
  152. if (!(surf instanceof ISurface)){
  153. console.log("WARNING: Attempted to set painter to non-surface instance.");
  154. return;
  155. }
  156. if (surf !== this.__surface){
  157. if (this.__surface !== null){
  158. this.__surface.unlisten("data_changed", handle_surface_data_changed);
  159. }
  160. this.__surface = surf;
  161. this.__surface.listen("data_changed", handle_surface_data_changed);
  162. if (this.__palette === null && this.__surface.palette !== null){
  163. this.__palette = this.__surface.palette;
  164. } else if (this.__palette !== null && this.__surface.palette !== this.__palette){
  165. this.__surface.palette = this.__palette;
  166. }
  167. this.scale_to_fit();
  168. this.center_surface();
  169. }
  170. RenderD();
  171. }).bind(this);
  172. GlobalEvents.listen("change_surface", handle_change_surface);
  173. var handle_color_change = (function(pi, ci){
  174. this.__brushPalette = pi;
  175. this.__brushColor = ci;
  176. }).bind(this);
  177. GlobalEvents.listen("active_palette_color", handle_color_change);
  178. var handle_mousemove = (function(e){
  179. this.__brushLastPos[0] = this.__brushPos[0];
  180. this.__brushLastPos[1] = this.__brushPos[1];
  181. this.__brushPos[0] = e.x;
  182. this.__brushPos[1] = e.y;
  183. if (this.__surface !== null){
  184. var x = Math.floor((this.__brushPos[0] - this.__offset[0]) * (1.0 / this.__scale));
  185. var y = Math.floor((this.__brushPos[1] - this.__offset[1]) * (1.0 / this.__scale));
  186. if (x >= 0 && x < this.__surface.width && y >= 0 && y < this.__surface.height){
  187. RenderD();
  188. }
  189. }
  190. }).bind(this);
  191. input.listen("mousemove", handle_mousemove);
  192. input.listen("mouseleft+mousemove", handle_mousemove);
  193. var handle_draw = (function(e){
  194. if (e.isCombo || e.button == 0){
  195. if (this.__surface !== null){
  196. //console.log(this.__brushPos);
  197. //console.log(this.__brushLastPos);
  198. var x = Math.floor((this.__brushPos[0] - this.__offset[0]) * (1.0 / this.__scale));
  199. var y = Math.floor((this.__brushPos[1] - this.__offset[1]) * (1.0 / this.__scale));
  200. var sx = (e.isCombo) ? Math.floor((this.__brushLastPos[0] - this.__offset[0]) * (1.0 / this.__scale)) : x;
  201. var sy = (e.isCombo) ? Math.floor((this.__brushLastPos[1] - this.__offset[1]) * (1.0 / this.__scale)) : y;
  202. if (x >= 0 && x < this.__surface.width && y >= 0 && y < this.__surface.height){
  203. LineToSurface(sx, sy, x, y, this.__brushColor, this.__brushPalette);
  204. //RenderD();
  205. }
  206. }
  207. }
  208. }).bind(this);
  209. input.listen("mouseclick", handle_draw);
  210. input.listen("mouseleft+mousemove", handle_draw);
  211. var handle_offset = (function(e){
  212. this.__offset[0] += e.x - e.lastX;
  213. this.__offset[1] += e.y - e.lastY;
  214. RenderD();
  215. }).bind(this);
  216. input.listen("shift+mouseleft+mousemove", handle_offset);
  217. var handle_scale = (function(e){
  218. if (e.delta < 0){
  219. this.scale_down();
  220. } else if (e.delta > 0){
  221. this.scale_up();
  222. }
  223. if (e.delta !== 0)
  224. RenderD();
  225. }).bind(this);
  226. input.listen("wheel", handle_scale);
  227. var elscale = document.querySelector("#painter_scale");
  228. if (elscale){
  229. var self = this;
  230. elscale.addEventListener("change", function(e){
  231. var val = Number(this.value);
  232. self.scale = val;
  233. RenderD();
  234. });
  235. elscale.value = this.__scale;
  236. }
  237. var handle_fittocanvas = (function(){
  238. this.scale_to_fit();
  239. this.center_surface();
  240. RenderD();
  241. }).bind(this);
  242. GlobalEvents.listen("painter-fit-to-canvas", handle_fittocanvas);
  243. var handle_togglegrid = (function(target, args){
  244. if (args.show){
  245. target.classList.add("pure-button-active");
  246. target.setAttribute("emit-args", JSON.stringify({show:false}));
  247. this.__gridEnabled = true;
  248. } else {
  249. target.classList.remove("pure-button-active");
  250. target.setAttribute("emit-args", JSON.stringify({show:true}));
  251. this.__gridEnabled = false;
  252. }
  253. RenderD();
  254. }).bind(this);
  255. GlobalEvents.listen("painter-togglegrid", handle_togglegrid);
  256. var handle_colormode = (function(target, args){
  257. if (args.onePaletteMode){
  258. target.classList.remove("pure-button-active");
  259. target.setAttribute("emit-args", JSON.stringify({onePaletteMode:false}));
  260. this.__onePaletteMode = true;
  261. } else {
  262. target.classList.add("pure-button-active");
  263. target.setAttribute("emit-args", JSON.stringify({onePaletteMode:true}));
  264. this.__onePaletteMode = false;
  265. }
  266. RenderD();
  267. }).bind(this);
  268. GlobalEvents.listen("painter-colormode", handle_colormode);
  269. }
  270. get onePaletteMode(){return this.__onePaletteMode;}
  271. set onePaletteMode(e){
  272. this.__onePaletteMode = (e === true);
  273. this.render();
  274. }
  275. get scale(){
  276. return this.__scale;
  277. }
  278. set scale(s){
  279. if (typeof(s) !== 'number')
  280. throw new TypeError("Expected number value.");
  281. this.__scale = Math.max(0.1, Math.min(100.0, s));
  282. var elscale = document.querySelector("#painter_scale");
  283. if (elscale){
  284. elscale.value = this.__scale;
  285. }
  286. }
  287. get showGrid(){return this.__gridEnabled;}
  288. set showGrid(e){
  289. this.__gridEnabled = (e === true);
  290. }
  291. initialize(){
  292. if (canvas === null){
  293. canvas = document.getElementById(EL_CANVAS_ID);
  294. if (!canvas)
  295. throw new Error("Failed to obtain the canvas element.");
  296. context = canvas.getContext("2d");
  297. if (!context)
  298. throw new Error("Failed to obtain canvas context.");
  299. context.imageSmoothingEnabled = false;
  300. ResizeCanvasImg(canvas.clientWidth, canvas.clientHeight); // A forced "resize".
  301. input.mouseTargetElement = canvas;
  302. this.scale_to_fit();
  303. this.center_surface();
  304. }
  305. return this;
  306. }
  307. scale_up(amount=1){
  308. this.scale = this.scale + (amount*0.1);
  309. return this;
  310. }
  311. scale_down(amount=1){
  312. this.scale = this.scale - (amount*0.1);
  313. return this;
  314. }
  315. center_surface(){
  316. if (canvas === null || this.__surface === null)
  317. return;
  318. this.__offset[0] = Math.floor((canvas.clientWidth - (this.__surface.width * this.__scale)) * 0.5);
  319. this.__offset[1] = Math.floor((canvas.clientHeight - (this.__surface.height * this.__scale)) * 0.5);
  320. return this;
  321. }
  322. scale_to_fit(){
  323. if (canvas === null || this.__surface === null)
  324. return;
  325. var sw = canvas.clientWidth / this.__surface.width;
  326. var sh = canvas.clientHeight / this.__surface.height;
  327. var s = Math.min(sw, sh).toString();
  328. var di = s.indexOf(".");
  329. if (di >= 0)
  330. s = s.slice(0, di+2);
  331. this.scale = Number(s);
  332. }
  333. render(){
  334. if (context === null || this.__surface === null)
  335. return;
  336. context.save();
  337. // Clearing the context surface...
  338. context.fillStyle = NESPalette.Default[4];
  339. context.fillRect(
  340. 0,0,
  341. Math.floor(canvas.clientWidth),
  342. Math.floor(canvas.clientHeight)
  343. );
  344. OpenCanvasPixels();
  345. for (var j = 0; j < this.__surface.height; j++){
  346. var y = (j*this.__scale) + this.__offset[1];
  347. for (var i = 0; i < this.__surface.width; i++){
  348. var x = (i*this.__scale) + this.__offset[0];
  349. if (x >= 0 && x < canvas.clientWidth && y >= 0 && y < canvas.clientHeight){
  350. var color = NESPalette.Default[4];
  351. if (this.__onePaletteMode){
  352. var pinfo = this.__surface.getColorIndex(i, j);
  353. if (pinfo.ci >= 0)
  354. color = NESPalette.Default[pinfo.ci];
  355. } else {
  356. color = this.__surface.getColor(i, j);
  357. }
  358. PutCanvasPixel(x,y, this.__scale, color);
  359. }
  360. }
  361. }
  362. CloseCanvasPixels();
  363. // Draw the mouse position... if mouse is currently in bounds.
  364. if (input.isMouseInBounds()){
  365. context.fillStyle = "#AA9900";
  366. var x = Math.floor((this.__brushPos[0] - this.__offset[0]) * (1.0/this.__scale));
  367. var y = Math.floor((this.__brushPos[1] - this.__offset[1]) * (1.0/this.__scale));
  368. if (x >= 0 && x < this.__surface.width && y >= 0 && y < this.__surface.height){
  369. context.beginPath();
  370. context.rect(
  371. this.__offset[0] + (x*this.__scale),
  372. this.__offset[1] + (y*this.__scale),
  373. Math.ceil(this.__scale),
  374. Math.ceil(this.__scale)
  375. );
  376. context.fill();
  377. context.closePath();
  378. }
  379. }
  380. // Draw grid.
  381. if (this.__gridEnabled && this.__scale > 0.5){
  382. context.strokeStyle = "#00FF00";
  383. var w = this.__surface.width * this.__scale;
  384. var h = this.__surface.height * this.__scale;
  385. var length = Math.max(this.__surface.width, this.__surface.height);
  386. for (var i=0; i < length; i += 8){
  387. var x = (i*this.__scale) + this.__offset[0];
  388. var y = (i*this.__scale) + this.__offset[1];
  389. if (i < this.__surface.width){
  390. context.beginPath();
  391. context.moveTo(x, this.__offset[1]);
  392. context.lineTo(x, this.__offset[1] + h);
  393. context.stroke();
  394. context.closePath();
  395. }
  396. if (i < this.__surface.height){
  397. context.beginPath();
  398. context.moveTo(this.__offset[0], y);
  399. context.lineTo(this.__offset[0] + w, y);
  400. context.stroke();
  401. context.closePath();
  402. }
  403. }
  404. }
  405. context.restore();
  406. return this;
  407. }
  408. }
  409. const instance = new CTRLPainter();
  410. export default instance;