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个字符

116 行
2.8KB

  1. import ISurface from "/app/js/ifaces/ISurface.js";
  2. import NESPalette from "/app/js/models/NESPalette.js";
  3. function render(surf, sx, sy, sw, sh, scale, ctx, dx, dy, palcolored){
  4. if (!(surf instanceof ISurface)){
  5. console.log("WARNING: Cannot render non-ISurface object.");
  6. return;
  7. }
  8. if (sx + sw > surf.width || sy + sh > surf.height){
  9. console.log("WARNING: Cannot render. Region is out of bounds.");
  10. return;
  11. }
  12. palcolored = (palcolored === true);
  13. var cw = (Math.floor(ctx.canvas.clientWidth) > 0) ?
  14. Math.floor(ctx.canvas.clientWidth) :
  15. Math.floor(ctx.canvas.width);
  16. var ch = (Math.floor(ctx.canvas.clientHeight) > 0) ?
  17. Math.floor(ctx.canvas.clientHeight) :
  18. Math.floor(ctx.canvas.height);
  19. if (cw <= 0 || ch <= 0){return;}
  20. ctx.save();
  21. ctx.fillStyle = NESPalette.Default[4];
  22. ctx.fillRect(0,0,cw,ch);
  23. var ctximg = ctx.getImageData(0, 0, cw, ch);
  24. var idat = ctximg.data;
  25. var PutPixel = (i,j,s,c) => {
  26. i = Math.round(i);
  27. j = Math.round(j);
  28. s = Math.ceil(s);
  29. var r = parseInt(c.substring(1, 3), 16);
  30. var g = parseInt(c.substring(3, 5), 16);
  31. var b = parseInt(c.substring(5, 7), 16);
  32. for (var y=j; y < j+s; y++){
  33. for (var x=i; x < i+s; x++){
  34. if (x >= 0 && x < cw && y >= 0 && y < ch){
  35. var index = (y*cw*4) + (x*4);
  36. idat[index] = r;
  37. idat[index+1] = g;
  38. idat[index+2] = b;
  39. idat[index+3] = 255;
  40. }
  41. }
  42. }
  43. };
  44. for (let j=sy; j < sy + sh; j++){
  45. var y = (j*scale) + dy;
  46. for (let i=sx; i < sx + sw; i++){
  47. var x = (i*scale) + dx;
  48. if (x >= 0 && x < cw && y >= 0 && y < ch){
  49. var color = NESPalette.Default[4];
  50. if (palcolored){
  51. color = surf.getColor(i, j);
  52. } else {
  53. var pinfo = surf.getColorIndex(i, j);
  54. color = (pinfo.ci >= 0) ? NESPalette.Default[pinfo.ci] : NESPalette.Default[4];
  55. }
  56. PutPixel(x,y,scale,color);
  57. }
  58. }
  59. }
  60. ctx.putImageData(ctximg, 0, 0);
  61. ctx.restore();
  62. }
  63. function renderToFit(surf, ctx, palcolored){
  64. if (!(surf instanceof ISurface)){
  65. console.log("WARNING: Cannot render non-ISurface object.");
  66. return;
  67. }
  68. palcolored = (palcolored === true);
  69. var cw = (Math.floor(ctx.canvas.clientWidth) > 0) ?
  70. Math.floor(ctx.canvas.clientWidth) :
  71. Math.floor(ctx.canvas.width);
  72. var ch = (Math.floor(ctx.canvas.clientHeight) > 0) ?
  73. Math.floor(ctx.canvas.clientHeight) :
  74. Math.floor(ctx.canvas.height);
  75. if (cw <= 0 || ch <= 0){return;}
  76. var scale = Math.min(
  77. cw/surf.width,
  78. ch/surf.height
  79. );
  80. var offX = Math.floor((cw - (surf.width*scale)) * 0.5);
  81. var offY = Math.floor((ch - (surf.height*scale)) * 0.5);
  82. render(surf, 0, 0, surf.width, surf.height, scale, ctx, offX, offY, palcolored);
  83. }
  84. export default {
  85. render: render,
  86. renderToFit: renderToFit
  87. };