graphico 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/canvas.js CHANGED
@@ -12,14 +12,6 @@ var Canvas = /** @class */ (function () {
12
12
  function Canvas(options) {
13
13
  if (options === void 0) { options = {}; }
14
14
  var _this = this;
15
- /**
16
- * Whether or not the control is focused
17
- */
18
- this.focused = false;
19
- /**
20
- * Frame counter, increments every frame
21
- */
22
- this.frame = 0;
23
15
  /**
24
16
  * Contains a list of current keys pressed
25
17
  */
@@ -36,7 +28,17 @@ var Canvas = /** @class */ (function () {
36
28
  * Contains the mouse Y-coordinate, in pixels
37
29
  */
38
30
  this.mouseY = 0;
31
+ /**
32
+ * Represents the animation ID handle to cancel the animation
33
+ */
34
+ this.animation = 0;
35
+ /**
36
+ * The last frame's high resolution timestamp
37
+ */
38
+ this.lastFrame = 0;
39
39
  this.config = Canvas.setDefaults(options, Canvas.defaults);
40
+ this.width = this.config.width;
41
+ this.height = this.config.height;
40
42
  var canvas = document.createElement('canvas');
41
43
  var graphics = canvas.getContext('2d');
42
44
  if (graphics) {
@@ -50,6 +52,7 @@ var Canvas = /** @class */ (function () {
50
52
  canvas.tabIndex = 1; // For element focus
51
53
  canvas.style.outline = 'none';
52
54
  canvas.style.imageRendering = 'pixelated';
55
+ graphics.imageSmoothingEnabled = false;
53
56
  // Set custom properties
54
57
  canvas.width = this.config.width;
55
58
  canvas.height = this.config.height;
@@ -58,20 +61,9 @@ var Canvas = /** @class */ (function () {
58
61
  canvas.style.background = this.config.background.toString();
59
62
  canvas.style.border = "".concat(this.config.scale, "px solid ").concat(this.config.border);
60
63
  canvas.style.cursor = this.config.showMouse ? 'default' : 'none';
61
- // Initialize main sequence
62
- if (this.config.framesPerSecond > 0) {
63
- setInterval(function () {
64
- if (!_this.focused) {
65
- return;
66
- }
67
- _this.frame++;
68
- _this.log(_this.frame);
69
- _this.config.loop(_this.frame, _this.keys, _this.mouseButtons, _this.mouseX, _this.mouseY);
70
- }, 1000 / this.config.framesPerSecond);
71
- }
72
64
  // Event listeners
73
65
  canvas.addEventListener('mousemove', function (e) {
74
- if (!_this.focused) {
66
+ if (!_this.isFocused()) {
75
67
  return;
76
68
  }
77
69
  _this.mouseX = (e.offsetX / _this.config.scale) | 0;
@@ -80,7 +72,7 @@ var Canvas = /** @class */ (function () {
80
72
  _this.config.mousemove(_this.mouseX, _this.mouseY);
81
73
  });
82
74
  canvas.addEventListener('keydown', function (e) {
83
- if (!_this.focused) {
75
+ if (!_this.isFocused()) {
84
76
  return;
85
77
  }
86
78
  e.preventDefault();
@@ -92,7 +84,7 @@ var Canvas = /** @class */ (function () {
92
84
  }
93
85
  });
94
86
  canvas.addEventListener('keyup', function (e) {
95
- if (!_this.focused) {
87
+ if (!_this.isFocused()) {
96
88
  return;
97
89
  }
98
90
  var key = e.key.toLowerCase();
@@ -104,7 +96,7 @@ var Canvas = /** @class */ (function () {
104
96
  }
105
97
  });
106
98
  canvas.addEventListener('mousedown', function (e) {
107
- if (!_this.focused) {
99
+ if (!_this.isFocused()) {
108
100
  return;
109
101
  }
110
102
  var button = e.button;
@@ -115,7 +107,7 @@ var Canvas = /** @class */ (function () {
115
107
  }
116
108
  });
117
109
  canvas.addEventListener('mouseup', function (e) {
118
- if (!_this.focused) {
110
+ if (!_this.isFocused()) {
119
111
  return;
120
112
  }
121
113
  var button = e.button;
@@ -127,19 +119,55 @@ var Canvas = /** @class */ (function () {
127
119
  }
128
120
  });
129
121
  canvas.addEventListener('focusin', function (e) {
130
- _this.focused = true;
131
122
  canvas.style.borderColor = _this.config.border;
132
- _this.log(e.type, _this.focused);
123
+ _this.log(e.type);
124
+ _this.animation = requestAnimationFrame(function (time) { return _this.startAnimate(time); });
133
125
  });
134
126
  canvas.addEventListener('focusout', function (e) {
135
- _this.focused = false;
136
127
  canvas.style.borderColor = _this.config.borderBlur;
137
- _this.log(e.type, _this.focused);
128
+ _this.log(e.type);
129
+ cancelAnimationFrame(_this.animation);
130
+ });
131
+ window.addEventListener('blur', function (e) {
132
+ _this.log(e.type);
133
+ cancelAnimationFrame(_this.animation);
138
134
  });
139
135
  canvas.addEventListener('contextmenu', function (e) { return e.preventDefault(); });
140
136
  // Focus on the canvas
141
137
  canvas.focus();
142
138
  }
139
+ /**
140
+ * Determine if the canvas is currently focused.
141
+ */
142
+ Canvas.prototype.isFocused = function () {
143
+ return this.graphics.canvas === document.activeElement;
144
+ };
145
+ /**
146
+ * Start the animation.
147
+ */
148
+ Canvas.prototype.startAnimate = function (time) {
149
+ var _this = this;
150
+ this.log('startAnimate', time);
151
+ this.lastFrame = time;
152
+ this.animation = requestAnimationFrame(function (time) { return _this.animate(time); });
153
+ };
154
+ /**
155
+ * Run the main animation loop.
156
+ */
157
+ Canvas.prototype.animate = function (time) {
158
+ var _this = this;
159
+ var _a;
160
+ var currentFrame = time;
161
+ var dt = currentFrame - this.lastFrame;
162
+ this.lastFrame = currentFrame;
163
+ this.log('animate', dt, currentFrame);
164
+ var drawables = (_a = this.config.loop(dt)) !== null && _a !== void 0 ? _a : [];
165
+ for (var _i = 0, drawables_1 = drawables; _i < drawables_1.length; _i++) {
166
+ var drawable = drawables_1[_i];
167
+ this.draw(drawable);
168
+ }
169
+ this.animation = requestAnimationFrame(function (time) { return _this.animate(time); });
170
+ };
143
171
  /**
144
172
  * Determine whether a key is currently pressed.
145
173
  * @param key The key to check
@@ -156,6 +184,50 @@ var Canvas = /** @class */ (function () {
156
184
  Canvas.prototype.isMouseButtonDown = function (button) {
157
185
  return this.mouseButtons.includes(button);
158
186
  };
187
+ /**
188
+ * Get the current cursor position.
189
+ * @returns Cursor position as `[x, y]`
190
+ */
191
+ Canvas.prototype.getMousePosition = function () {
192
+ return [this.mouseX, this.mouseY];
193
+ };
194
+ /**
195
+ * Get the color of the selected pixel.
196
+ * @param x The pixel's x-coordinate
197
+ * @param y The pixel's y-coordinate
198
+ * @returns `[red, green, blue, alpha]`
199
+ */
200
+ Canvas.prototype.getPixel = function (x, y) {
201
+ var data = this.graphics.getImageData(x, y, 1, 1).data;
202
+ console.log(this.graphics.getImageData(x, y, 2, 2));
203
+ ;
204
+ return [data[0], data[1], data[2], data[3]];
205
+ };
206
+ /**
207
+ * Set the color of the selected pixel.
208
+ * @param x The pixel's x-coordinate
209
+ * @param y The pixel's y-coordinate
210
+ * @param color `[red, green, blue, alpha]`
211
+ */
212
+ Canvas.prototype.setPixel = function (x, y, color) {
213
+ var data = this.graphics.getImageData(x, y, 1, 1);
214
+ data.data[0] = color[0];
215
+ data.data[1] = color[1];
216
+ data.data[2] = color[2];
217
+ data.data[3] = color[3];
218
+ console.log(data);
219
+ this.graphics.putImageData(data, x, y);
220
+ };
221
+ /**
222
+ * Take a screenshot of the canvas contents and save to a .png file.
223
+ */
224
+ Canvas.prototype.screenshot = function () {
225
+ var dataURL = this.graphics.canvas.toDataURL();
226
+ var downloadLink = document.createElement('a');
227
+ downloadLink.setAttribute('href', dataURL);
228
+ downloadLink.setAttribute('download', 'screenshot');
229
+ downloadLink.click();
230
+ };
159
231
  /**
160
232
  * Draw an object onto the canvas.
161
233
  * @param drawable Any drawable object
@@ -206,7 +278,6 @@ var Canvas = /** @class */ (function () {
206
278
  border: 'transparent',
207
279
  borderBlur: 'transparent',
208
280
  showMouse: true,
209
- framesPerSecond: 60,
210
281
  keydown: function () { return; },
211
282
  keyup: function () { return; },
212
283
  mousemove: function () { return; },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphico",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Canvas 2D rendering toolkit for games and visual projects",
5
5
  "homepage": "https://npm.nicfv.com/",
6
6
  "bin": "",
package/types/canvas.d.ts CHANGED
@@ -14,14 +14,6 @@ export declare class Canvas {
14
14
  * Can be used to render 2D graphics onto the canvas
15
15
  */
16
16
  private readonly graphics;
17
- /**
18
- * Whether or not the control is focused
19
- */
20
- private focused;
21
- /**
22
- * Frame counter, increments every frame
23
- */
24
- private frame;
25
17
  /**
26
18
  * Contains a list of current keys pressed
27
19
  */
@@ -30,6 +22,14 @@ export declare class Canvas {
30
22
  * Contains a list of current mouse buttons pressed
31
23
  */
32
24
  private readonly mouseButtons;
25
+ /**
26
+ * The width of the canvas, in pixels
27
+ */
28
+ readonly width: number;
29
+ /**
30
+ * The height of the canvas, in pixels
31
+ */
32
+ readonly height: number;
33
33
  /**
34
34
  * Contains the mouse X-coordinate, in pixels
35
35
  */
@@ -38,11 +38,31 @@ export declare class Canvas {
38
38
  * Contains the mouse Y-coordinate, in pixels
39
39
  */
40
40
  private mouseY;
41
+ /**
42
+ * Represents the animation ID handle to cancel the animation
43
+ */
44
+ private animation;
45
+ /**
46
+ * The last frame's high resolution timestamp
47
+ */
48
+ private lastFrame;
41
49
  /**
42
50
  * Create a new canvas with the provided options
43
51
  * @param options Configuration options
44
52
  */
45
53
  constructor(options?: Partial<Options>);
54
+ /**
55
+ * Determine if the canvas is currently focused.
56
+ */
57
+ private isFocused;
58
+ /**
59
+ * Start the animation.
60
+ */
61
+ private startAnimate;
62
+ /**
63
+ * Run the main animation loop.
64
+ */
65
+ private animate;
46
66
  /**
47
67
  * Determine whether a key is currently pressed.
48
68
  * @param key The key to check
@@ -55,6 +75,29 @@ export declare class Canvas {
55
75
  * @returns True if `button` is down
56
76
  */
57
77
  isMouseButtonDown(button: number): boolean;
78
+ /**
79
+ * Get the current cursor position.
80
+ * @returns Cursor position as `[x, y]`
81
+ */
82
+ getMousePosition(): [number, number];
83
+ /**
84
+ * Get the color of the selected pixel.
85
+ * @param x The pixel's x-coordinate
86
+ * @param y The pixel's y-coordinate
87
+ * @returns `[red, green, blue, alpha]`
88
+ */
89
+ getPixel(x: number, y: number): [number, number, number, number];
90
+ /**
91
+ * Set the color of the selected pixel.
92
+ * @param x The pixel's x-coordinate
93
+ * @param y The pixel's y-coordinate
94
+ * @param color `[red, green, blue, alpha]`
95
+ */
96
+ setPixel(x: number, y: number, color: [number, number, number, number]): void;
97
+ /**
98
+ * Take a screenshot of the canvas contents and save to a .png file.
99
+ */
100
+ screenshot(): void;
58
101
  /**
59
102
  * Draw an object onto the canvas.
60
103
  * @param drawable Any drawable object
@@ -113,10 +156,6 @@ export interface Options {
113
156
  * Optionally show or hide the mouse when hovering over the canvas
114
157
  */
115
158
  readonly showMouse: boolean;
116
- /**
117
- * The number of frames to render every second
118
- */
119
- readonly framesPerSecond: number;
120
159
  /**
121
160
  * Event listener for when a key is pressed
122
161
  * @param key The key that was pressed
@@ -144,16 +183,11 @@ export interface Options {
144
183
  */
145
184
  readonly mouseup: (button: number) => void;
146
185
  /**
147
- * Event listener for a the main loop, only called when:
148
- * - `framesPerSecond` > 0
149
- * - The canvas is focused
150
- * @param frame The frame sequence number
151
- * @param keys A list of keys that are currently pressed
152
- * @param mouseButtons A list of buttons that are currently pressed
153
- * @param x Cursor X-coordinate
154
- * @param y Cursor Y-coordinate
186
+ * Event listener for a the main animation loop
187
+ * @param dt The number of milliseconds in between frames
188
+ * @returns An array of `Drawable` to render, or void
155
189
  */
156
- readonly loop: (frame: number, keys: string[], mouseButtons: number[], x: number, y: number) => void;
190
+ readonly loop: (dt: number) => Drawable[] | (void | never);
157
191
  }
158
192
  /**
159
193
  * Represents an object that can be drawn on the canvas.