stitchey.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const SMALLEST_SIZE = 4.0;
  2. const LARGEST_SIZE = 48.0;
  3. const DEFAULT_SIZE = 24.0;
  4. class State {
  5. updateWindowSize(w, h) {
  6. this.w = w;
  7. this.h = h;
  8. this.canvas.width = w;
  9. this.canvas.height = h;
  10. this.data = {};
  11. }
  12. constructor(canvas, ctx, w, h) {
  13. this.canvas = canvas;
  14. this.ctx = ctx;
  15. this.updateWindowSize(w, h);
  16. this.size = DEFAULT_SIZE;
  17. this.mode = 'square';
  18. }
  19. scroll(delta) {
  20. this.size += delta / 30.0;
  21. if (this.size < SMALLEST_SIZE) {
  22. this.size = SMALLEST_SIZE;
  23. } else if (this.size > LARGEST_SIZE) {
  24. this.size = LARGEST_SIZE;
  25. }
  26. }
  27. draw(mouseLoc) {
  28. this.ctx.clearRect(0, 0, this.w, this.h);
  29. if (mouseLoc !== null) {
  30. let [fx, fy] = mouseLoc;
  31. this.ctx.fillStyle = '#f00';
  32. if (this.mode === 'square') {
  33. fx = Math.floor(fx / this.size);
  34. fy = Math.floor(fy / this.size);
  35. this.ctx.fillRect(fx * this.size, fy * this.size, this.size, this.size);
  36. } else {
  37. this.ctx.strokeStyle = '#f00';
  38. const half = this.size / 2
  39. fx = Math.floor((fx + half) / this.size);
  40. fy = Math.floor((fy + half) / this.size);
  41. this.ctx.moveTo(fx * this.size, fy * this.size);
  42. this.ctx.arc(fx * this.size, fy * this.size, 5, 0, Math.PI * 2, true);
  43. this.ctx.stroke();
  44. }
  45. }
  46. this.ctx.strokeStyle = '#000';
  47. for (let x = 0; Math.floor(x / this.size < this.w); x++) {
  48. this.ctx.beginPath();
  49. this.ctx.moveTo(x * this.size, 0);
  50. this.ctx.lineTo(x * this.size, this.h);
  51. this.ctx.stroke();
  52. }
  53. for (let y = 0; Math.floor(y / this.size < this.h); y++) {
  54. this.ctx.beginPath();
  55. this.ctx.moveTo(0, y * this.size);
  56. this.ctx.lineTo(this.w, y * this.size);
  57. this.ctx.stroke();
  58. }
  59. }
  60. }
  61. let main = () => {
  62. const canvas = document.getElementById('cv');
  63. const w = window.innerWidth;
  64. const h = window.innerHeight;
  65. const ctx = canvas.getContext('2d');
  66. let state = new State(canvas, ctx, w, h);
  67. let last_mouse = null;
  68. state.draw(last_mouse);
  69. document.addEventListener('mousemove', e => {
  70. last_mouse = [e.clientX, e.clientY];
  71. state.draw(last_mouse);
  72. });
  73. document.addEventListener('wheel', e => {
  74. state.scroll(-e.deltaY);
  75. state.draw(last_mouse);
  76. });
  77. window.addEventListener('resize', () => {
  78. state.updateWindowSize(
  79. window.innerWidth,
  80. window.innerHeight,
  81. );
  82. state.draw(last_mouse);
  83. });
  84. document.addEventListener('keydown', e => {
  85. if (e.key === ' ') {
  86. if (state.mode === 'square')
  87. state.mode = 'dot';
  88. else
  89. state.mode = 'square';
  90. }
  91. state.draw(last_mouse);
  92. });
  93. };
  94. window.onload = main;