graphico 0.0.1 → 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 ADDED
@@ -0,0 +1,290 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Canvas = void 0;
4
+ /**
5
+ * Represents a canvas for drawing and animating
6
+ */
7
+ var Canvas = /** @class */ (function () {
8
+ /**
9
+ * Create a new canvas with the provided options
10
+ * @param options Configuration options
11
+ */
12
+ function Canvas(options) {
13
+ if (options === void 0) { options = {}; }
14
+ var _this = this;
15
+ /**
16
+ * Contains a list of current keys pressed
17
+ */
18
+ this.keys = [];
19
+ /**
20
+ * Contains a list of current mouse buttons pressed
21
+ */
22
+ this.mouseButtons = [];
23
+ /**
24
+ * Contains the mouse X-coordinate, in pixels
25
+ */
26
+ this.mouseX = 0;
27
+ /**
28
+ * Contains the mouse Y-coordinate, in pixels
29
+ */
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
+ this.config = Canvas.setDefaults(options, Canvas.defaults);
40
+ this.width = this.config.width;
41
+ this.height = this.config.height;
42
+ var canvas = document.createElement('canvas');
43
+ var graphics = canvas.getContext('2d');
44
+ if (graphics) {
45
+ this.graphics = graphics;
46
+ }
47
+ else {
48
+ throw new Error('Could not initialize canvas graphics.');
49
+ }
50
+ // Set static properties
51
+ this.config.parent.appendChild(canvas);
52
+ canvas.tabIndex = 1; // For element focus
53
+ canvas.style.outline = 'none';
54
+ canvas.style.imageRendering = 'pixelated';
55
+ graphics.imageSmoothingEnabled = false;
56
+ // Set custom properties
57
+ canvas.width = this.config.width;
58
+ canvas.height = this.config.height;
59
+ canvas.style.width = "".concat(this.config.width * this.config.scale, "px");
60
+ canvas.style.height = "".concat(this.config.height * this.config.scale, "px");
61
+ canvas.style.background = this.config.background.toString();
62
+ canvas.style.border = "".concat(this.config.scale, "px solid ").concat(this.config.border);
63
+ canvas.style.cursor = this.config.showMouse ? 'default' : 'none';
64
+ // Event listeners
65
+ canvas.addEventListener('mousemove', function (e) {
66
+ if (!_this.isFocused()) {
67
+ return;
68
+ }
69
+ _this.mouseX = (e.offsetX / _this.config.scale) | 0;
70
+ _this.mouseY = (e.offsetY / _this.config.scale) | 0;
71
+ _this.log(e.type, _this.mouseX, _this.mouseY);
72
+ _this.config.mousemove(_this.mouseX, _this.mouseY);
73
+ });
74
+ canvas.addEventListener('keydown', function (e) {
75
+ if (!_this.isFocused()) {
76
+ return;
77
+ }
78
+ e.preventDefault();
79
+ var key = e.key.toLowerCase();
80
+ _this.log(e.type, _this.keys);
81
+ if (!_this.keys.includes(key)) {
82
+ _this.keys.push(key);
83
+ _this.config.keydown(key);
84
+ }
85
+ });
86
+ canvas.addEventListener('keyup', function (e) {
87
+ if (!_this.isFocused()) {
88
+ return;
89
+ }
90
+ var key = e.key.toLowerCase();
91
+ var index = _this.keys.indexOf(key);
92
+ _this.log(e.type, _this.keys);
93
+ if (index >= 0) {
94
+ _this.keys.splice(index, 1);
95
+ _this.config.keyup(key);
96
+ }
97
+ });
98
+ canvas.addEventListener('mousedown', function (e) {
99
+ if (!_this.isFocused()) {
100
+ return;
101
+ }
102
+ var button = e.button;
103
+ _this.log(e.type, _this.mouseButtons);
104
+ if (!_this.mouseButtons.includes(button)) {
105
+ _this.mouseButtons.push(button);
106
+ _this.config.mousedown(button);
107
+ }
108
+ });
109
+ canvas.addEventListener('mouseup', function (e) {
110
+ if (!_this.isFocused()) {
111
+ return;
112
+ }
113
+ var button = e.button;
114
+ var index = _this.mouseButtons.indexOf(button);
115
+ _this.log(e.type, _this.mouseButtons);
116
+ if (index >= 0) {
117
+ _this.mouseButtons.splice(index, 1);
118
+ _this.config.mouseup(button);
119
+ }
120
+ });
121
+ canvas.addEventListener('focusin', function (e) {
122
+ canvas.style.borderColor = _this.config.border;
123
+ _this.log(e.type);
124
+ _this.animation = requestAnimationFrame(function (time) { return _this.startAnimate(time); });
125
+ });
126
+ canvas.addEventListener('focusout', function (e) {
127
+ canvas.style.borderColor = _this.config.borderBlur;
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);
134
+ });
135
+ canvas.addEventListener('contextmenu', function (e) { return e.preventDefault(); });
136
+ // Focus on the canvas
137
+ canvas.focus();
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
+ };
171
+ /**
172
+ * Determine whether a key is currently pressed.
173
+ * @param key The key to check
174
+ * @returns True if `key` is down
175
+ */
176
+ Canvas.prototype.isKeyDown = function (key) {
177
+ return this.keys.includes(key.toLowerCase());
178
+ };
179
+ /**
180
+ * Determine whether a mouse button is currently pressed.
181
+ * @param button The button ID
182
+ * @returns True if `button` is down
183
+ */
184
+ Canvas.prototype.isMouseButtonDown = function (button) {
185
+ return this.mouseButtons.includes(button);
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
+ };
231
+ /**
232
+ * Draw an object onto the canvas.
233
+ * @param drawable Any drawable object
234
+ */
235
+ Canvas.prototype.draw = function (drawable) {
236
+ drawable.draw(this.graphics);
237
+ };
238
+ /**
239
+ * Completely clears the canvas.
240
+ */
241
+ Canvas.prototype.clear = function () {
242
+ this.graphics.clearRect(0, 0, this.config.width, this.config.height);
243
+ };
244
+ /**
245
+ * Log a message to the debug console.
246
+ */
247
+ Canvas.prototype.log = function () {
248
+ var x = [];
249
+ for (var _i = 0; _i < arguments.length; _i++) {
250
+ x[_i] = arguments[_i];
251
+ }
252
+ if (this.config.debug) {
253
+ console.log.apply(console, x);
254
+ }
255
+ };
256
+ /**
257
+ * Set defaults for all undefined options.
258
+ */
259
+ Canvas.setDefaults = function (options, defaults) {
260
+ var _a;
261
+ var copy = {};
262
+ for (var _i = 0, _b = Object.keys(defaults); _i < _b.length; _i++) {
263
+ var key = _b[_i];
264
+ copy[key] = (_a = options[key]) !== null && _a !== void 0 ? _a : defaults[key];
265
+ }
266
+ return copy;
267
+ };
268
+ /**
269
+ * Default canvas options
270
+ */
271
+ Canvas.defaults = {
272
+ debug: false,
273
+ parent: document.body,
274
+ width: 600,
275
+ height: 400,
276
+ scale: 1,
277
+ background: 'transparent',
278
+ border: 'transparent',
279
+ borderBlur: 'transparent',
280
+ showMouse: true,
281
+ keydown: function () { return; },
282
+ keyup: function () { return; },
283
+ mousemove: function () { return; },
284
+ mousedown: function () { return; },
285
+ mouseup: function () { return; },
286
+ loop: function () { return; },
287
+ };
288
+ return Canvas;
289
+ }());
290
+ exports.Canvas = Canvas;
package/dist/index.js CHANGED
@@ -1,6 +1,19 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PKG_NAME = void 0;
4
17
  /**
5
18
  * @packageDocumentation
6
19
  * Canvas 2D rendering toolkit for games and visual projects
@@ -8,7 +21,4 @@ exports.PKG_NAME = void 0;
8
21
  * ![NPM Downloads](https://img.shields.io/npm/d18m/graphico)
9
22
  * ![NPM Last Update](https://img.shields.io/npm/last-update/graphico)
10
23
  */
11
- /**
12
- * Package Name
13
- */
14
- exports.PKG_NAME = 'graphico';
24
+ __exportStar(require("./canvas"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphico",
3
- "version": "0.0.1",
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": "",
@@ -0,0 +1,201 @@
1
+ /**
2
+ * Represents a canvas for drawing and animating
3
+ */
4
+ export declare class Canvas {
5
+ /**
6
+ * Default canvas options
7
+ */
8
+ private static readonly defaults;
9
+ /**
10
+ * Configuration options for this canvas
11
+ */
12
+ private readonly config;
13
+ /**
14
+ * Can be used to render 2D graphics onto the canvas
15
+ */
16
+ private readonly graphics;
17
+ /**
18
+ * Contains a list of current keys pressed
19
+ */
20
+ private readonly keys;
21
+ /**
22
+ * Contains a list of current mouse buttons pressed
23
+ */
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
+ /**
34
+ * Contains the mouse X-coordinate, in pixels
35
+ */
36
+ private mouseX;
37
+ /**
38
+ * Contains the mouse Y-coordinate, in pixels
39
+ */
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;
49
+ /**
50
+ * Create a new canvas with the provided options
51
+ * @param options Configuration options
52
+ */
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;
66
+ /**
67
+ * Determine whether a key is currently pressed.
68
+ * @param key The key to check
69
+ * @returns True if `key` is down
70
+ */
71
+ isKeyDown(key: string): boolean;
72
+ /**
73
+ * Determine whether a mouse button is currently pressed.
74
+ * @param button The button ID
75
+ * @returns True if `button` is down
76
+ */
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;
101
+ /**
102
+ * Draw an object onto the canvas.
103
+ * @param drawable Any drawable object
104
+ */
105
+ draw(drawable: Drawable): void;
106
+ /**
107
+ * Completely clears the canvas.
108
+ */
109
+ clear(): void;
110
+ /**
111
+ * Log a message to the debug console.
112
+ */
113
+ private log;
114
+ /**
115
+ * Set defaults for all undefined options.
116
+ */
117
+ private static setDefaults;
118
+ }
119
+ /**
120
+ * Configuration Options for Canvas
121
+ */
122
+ export interface Options {
123
+ /**
124
+ * Optionally print debug messages to the console
125
+ */
126
+ readonly debug: boolean;
127
+ /**
128
+ * Appends the canvas onto the parent element
129
+ */
130
+ readonly parent: Node;
131
+ /**
132
+ * The width of the drawing area, in pixels
133
+ */
134
+ readonly width: number;
135
+ /**
136
+ * The height of the drawing area, in pixels
137
+ */
138
+ readonly height: number;
139
+ /**
140
+ * The scale of the drawing area to the actual size of the canvas element
141
+ */
142
+ readonly scale: number;
143
+ /**
144
+ * The background color of the canvas
145
+ */
146
+ readonly background: string;
147
+ /**
148
+ * The border color for the canvas (when focused)
149
+ */
150
+ readonly border: string;
151
+ /**
152
+ * The border color for the canvas (when not focused)
153
+ */
154
+ readonly borderBlur: string;
155
+ /**
156
+ * Optionally show or hide the mouse when hovering over the canvas
157
+ */
158
+ readonly showMouse: boolean;
159
+ /**
160
+ * Event listener for when a key is pressed
161
+ * @param key The key that was pressed
162
+ */
163
+ readonly keydown: (key: string) => void;
164
+ /**
165
+ * Event listener for when a key is released
166
+ * @param key The key that was released
167
+ */
168
+ readonly keyup: (key: string) => void;
169
+ /**
170
+ * Event listener for when the mouse is moved
171
+ * @param x Cursor X-coordinate
172
+ * @param y Cursor Y-coordinate
173
+ */
174
+ readonly mousemove: (x: number, y: number) => void;
175
+ /**
176
+ * Event listener for when a button on the mouse is pressed
177
+ * @param button The button that was pressed
178
+ */
179
+ readonly mousedown: (button: number) => void;
180
+ /**
181
+ * Event listener for when a button on the mouse is released
182
+ * @param button The button that was released
183
+ */
184
+ readonly mouseup: (button: number) => void;
185
+ /**
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
189
+ */
190
+ readonly loop: (dt: number) => Drawable[] | (void | never);
191
+ }
192
+ /**
193
+ * Represents an object that can be drawn on the canvas.
194
+ */
195
+ export interface Drawable {
196
+ /**
197
+ * Draw this object onto the canvas.
198
+ * @param graphics Canvas 2D rendering interface
199
+ */
200
+ draw(graphics: CanvasRenderingContext2D): void;
201
+ }
package/types/index.d.ts CHANGED
@@ -5,7 +5,4 @@
5
5
  * ![NPM Downloads](https://img.shields.io/npm/d18m/graphico)
6
6
  * ![NPM Last Update](https://img.shields.io/npm/last-update/graphico)
7
7
  */
8
- /**
9
- * Package Name
10
- */
11
- export declare const PKG_NAME = "graphico";
8
+ export * from './canvas';