graphico 0.0.1 → 0.0.2

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,219 @@
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
+ * 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
+ /**
24
+ * Contains a list of current keys pressed
25
+ */
26
+ this.keys = [];
27
+ /**
28
+ * Contains a list of current mouse buttons pressed
29
+ */
30
+ this.mouseButtons = [];
31
+ /**
32
+ * Contains the mouse X-coordinate, in pixels
33
+ */
34
+ this.mouseX = 0;
35
+ /**
36
+ * Contains the mouse Y-coordinate, in pixels
37
+ */
38
+ this.mouseY = 0;
39
+ this.config = Canvas.setDefaults(options, Canvas.defaults);
40
+ var canvas = document.createElement('canvas');
41
+ var graphics = canvas.getContext('2d');
42
+ if (graphics) {
43
+ this.graphics = graphics;
44
+ }
45
+ else {
46
+ throw new Error('Could not initialize canvas graphics.');
47
+ }
48
+ // Set static properties
49
+ this.config.parent.appendChild(canvas);
50
+ canvas.tabIndex = 1; // For element focus
51
+ canvas.style.outline = 'none';
52
+ canvas.style.imageRendering = 'pixelated';
53
+ // Set custom properties
54
+ canvas.width = this.config.width;
55
+ canvas.height = this.config.height;
56
+ canvas.style.width = "".concat(this.config.width * this.config.scale, "px");
57
+ canvas.style.height = "".concat(this.config.height * this.config.scale, "px");
58
+ canvas.style.background = this.config.background.toString();
59
+ canvas.style.border = "".concat(this.config.scale, "px solid ").concat(this.config.border);
60
+ 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
+ // Event listeners
73
+ canvas.addEventListener('mousemove', function (e) {
74
+ if (!_this.focused) {
75
+ return;
76
+ }
77
+ _this.mouseX = (e.offsetX / _this.config.scale) | 0;
78
+ _this.mouseY = (e.offsetY / _this.config.scale) | 0;
79
+ _this.log(e.type, _this.mouseX, _this.mouseY);
80
+ _this.config.mousemove(_this.mouseX, _this.mouseY);
81
+ });
82
+ canvas.addEventListener('keydown', function (e) {
83
+ if (!_this.focused) {
84
+ return;
85
+ }
86
+ e.preventDefault();
87
+ var key = e.key.toLowerCase();
88
+ _this.log(e.type, _this.keys);
89
+ if (!_this.keys.includes(key)) {
90
+ _this.keys.push(key);
91
+ _this.config.keydown(key);
92
+ }
93
+ });
94
+ canvas.addEventListener('keyup', function (e) {
95
+ if (!_this.focused) {
96
+ return;
97
+ }
98
+ var key = e.key.toLowerCase();
99
+ var index = _this.keys.indexOf(key);
100
+ _this.log(e.type, _this.keys);
101
+ if (index >= 0) {
102
+ _this.keys.splice(index, 1);
103
+ _this.config.keyup(key);
104
+ }
105
+ });
106
+ canvas.addEventListener('mousedown', function (e) {
107
+ if (!_this.focused) {
108
+ return;
109
+ }
110
+ var button = e.button;
111
+ _this.log(e.type, _this.mouseButtons);
112
+ if (!_this.mouseButtons.includes(button)) {
113
+ _this.mouseButtons.push(button);
114
+ _this.config.mousedown(button);
115
+ }
116
+ });
117
+ canvas.addEventListener('mouseup', function (e) {
118
+ if (!_this.focused) {
119
+ return;
120
+ }
121
+ var button = e.button;
122
+ var index = _this.mouseButtons.indexOf(button);
123
+ _this.log(e.type, _this.mouseButtons);
124
+ if (index >= 0) {
125
+ _this.mouseButtons.splice(index, 1);
126
+ _this.config.mouseup(button);
127
+ }
128
+ });
129
+ canvas.addEventListener('focusin', function (e) {
130
+ _this.focused = true;
131
+ canvas.style.borderColor = _this.config.border;
132
+ _this.log(e.type, _this.focused);
133
+ });
134
+ canvas.addEventListener('focusout', function (e) {
135
+ _this.focused = false;
136
+ canvas.style.borderColor = _this.config.borderBlur;
137
+ _this.log(e.type, _this.focused);
138
+ });
139
+ canvas.addEventListener('contextmenu', function (e) { return e.preventDefault(); });
140
+ // Focus on the canvas
141
+ canvas.focus();
142
+ }
143
+ /**
144
+ * Determine whether a key is currently pressed.
145
+ * @param key The key to check
146
+ * @returns True if `key` is down
147
+ */
148
+ Canvas.prototype.isKeyDown = function (key) {
149
+ return this.keys.includes(key.toLowerCase());
150
+ };
151
+ /**
152
+ * Determine whether a mouse button is currently pressed.
153
+ * @param button The button ID
154
+ * @returns True if `button` is down
155
+ */
156
+ Canvas.prototype.isMouseButtonDown = function (button) {
157
+ return this.mouseButtons.includes(button);
158
+ };
159
+ /**
160
+ * Draw an object onto the canvas.
161
+ * @param drawable Any drawable object
162
+ */
163
+ Canvas.prototype.draw = function (drawable) {
164
+ drawable.draw(this.graphics);
165
+ };
166
+ /**
167
+ * Completely clears the canvas.
168
+ */
169
+ Canvas.prototype.clear = function () {
170
+ this.graphics.clearRect(0, 0, this.config.width, this.config.height);
171
+ };
172
+ /**
173
+ * Log a message to the debug console.
174
+ */
175
+ Canvas.prototype.log = function () {
176
+ var x = [];
177
+ for (var _i = 0; _i < arguments.length; _i++) {
178
+ x[_i] = arguments[_i];
179
+ }
180
+ if (this.config.debug) {
181
+ console.log.apply(console, x);
182
+ }
183
+ };
184
+ /**
185
+ * Set defaults for all undefined options.
186
+ */
187
+ Canvas.setDefaults = function (options, defaults) {
188
+ var _a;
189
+ var copy = {};
190
+ for (var _i = 0, _b = Object.keys(defaults); _i < _b.length; _i++) {
191
+ var key = _b[_i];
192
+ copy[key] = (_a = options[key]) !== null && _a !== void 0 ? _a : defaults[key];
193
+ }
194
+ return copy;
195
+ };
196
+ /**
197
+ * Default canvas options
198
+ */
199
+ Canvas.defaults = {
200
+ debug: false,
201
+ parent: document.body,
202
+ width: 600,
203
+ height: 400,
204
+ scale: 1,
205
+ background: 'transparent',
206
+ border: 'transparent',
207
+ borderBlur: 'transparent',
208
+ showMouse: true,
209
+ framesPerSecond: 60,
210
+ keydown: function () { return; },
211
+ keyup: function () { return; },
212
+ mousemove: function () { return; },
213
+ mousedown: function () { return; },
214
+ mouseup: function () { return; },
215
+ loop: function () { return; },
216
+ };
217
+ return Canvas;
218
+ }());
219
+ 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.2",
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,167 @@
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
+ * Whether or not the control is focused
19
+ */
20
+ private focused;
21
+ /**
22
+ * Frame counter, increments every frame
23
+ */
24
+ private frame;
25
+ /**
26
+ * Contains a list of current keys pressed
27
+ */
28
+ private readonly keys;
29
+ /**
30
+ * Contains a list of current mouse buttons pressed
31
+ */
32
+ private readonly mouseButtons;
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
+ * Create a new canvas with the provided options
43
+ * @param options Configuration options
44
+ */
45
+ constructor(options?: Partial<Options>);
46
+ /**
47
+ * Determine whether a key is currently pressed.
48
+ * @param key The key to check
49
+ * @returns True if `key` is down
50
+ */
51
+ isKeyDown(key: string): boolean;
52
+ /**
53
+ * Determine whether a mouse button is currently pressed.
54
+ * @param button The button ID
55
+ * @returns True if `button` is down
56
+ */
57
+ isMouseButtonDown(button: number): boolean;
58
+ /**
59
+ * Draw an object onto the canvas.
60
+ * @param drawable Any drawable object
61
+ */
62
+ draw(drawable: Drawable): void;
63
+ /**
64
+ * Completely clears the canvas.
65
+ */
66
+ clear(): void;
67
+ /**
68
+ * Log a message to the debug console.
69
+ */
70
+ private log;
71
+ /**
72
+ * Set defaults for all undefined options.
73
+ */
74
+ private static setDefaults;
75
+ }
76
+ /**
77
+ * Configuration Options for Canvas
78
+ */
79
+ export interface Options {
80
+ /**
81
+ * Optionally print debug messages to the console
82
+ */
83
+ readonly debug: boolean;
84
+ /**
85
+ * Appends the canvas onto the parent element
86
+ */
87
+ readonly parent: Node;
88
+ /**
89
+ * The width of the drawing area, in pixels
90
+ */
91
+ readonly width: number;
92
+ /**
93
+ * The height of the drawing area, in pixels
94
+ */
95
+ readonly height: number;
96
+ /**
97
+ * The scale of the drawing area to the actual size of the canvas element
98
+ */
99
+ readonly scale: number;
100
+ /**
101
+ * The background color of the canvas
102
+ */
103
+ readonly background: string;
104
+ /**
105
+ * The border color for the canvas (when focused)
106
+ */
107
+ readonly border: string;
108
+ /**
109
+ * The border color for the canvas (when not focused)
110
+ */
111
+ readonly borderBlur: string;
112
+ /**
113
+ * Optionally show or hide the mouse when hovering over the canvas
114
+ */
115
+ readonly showMouse: boolean;
116
+ /**
117
+ * The number of frames to render every second
118
+ */
119
+ readonly framesPerSecond: number;
120
+ /**
121
+ * Event listener for when a key is pressed
122
+ * @param key The key that was pressed
123
+ */
124
+ readonly keydown: (key: string) => void;
125
+ /**
126
+ * Event listener for when a key is released
127
+ * @param key The key that was released
128
+ */
129
+ readonly keyup: (key: string) => void;
130
+ /**
131
+ * Event listener for when the mouse is moved
132
+ * @param x Cursor X-coordinate
133
+ * @param y Cursor Y-coordinate
134
+ */
135
+ readonly mousemove: (x: number, y: number) => void;
136
+ /**
137
+ * Event listener for when a button on the mouse is pressed
138
+ * @param button The button that was pressed
139
+ */
140
+ readonly mousedown: (button: number) => void;
141
+ /**
142
+ * Event listener for when a button on the mouse is released
143
+ * @param button The button that was released
144
+ */
145
+ readonly mouseup: (button: number) => void;
146
+ /**
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
155
+ */
156
+ readonly loop: (frame: number, keys: string[], mouseButtons: number[], x: number, y: number) => void;
157
+ }
158
+ /**
159
+ * Represents an object that can be drawn on the canvas.
160
+ */
161
+ export interface Drawable {
162
+ /**
163
+ * Draw this object onto the canvas.
164
+ * @param graphics Canvas 2D rendering interface
165
+ */
166
+ draw(graphics: CanvasRenderingContext2D): void;
167
+ }
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';