canvas-based-core 1.0.0

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/.gitattributes ADDED
@@ -0,0 +1,2 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
@@ -0,0 +1,36 @@
1
+ # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2
+ # For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
3
+
4
+ name: Node.js Package
5
+
6
+ on:
7
+ release:
8
+ types: [created]
9
+
10
+ jobs:
11
+ build:
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - uses: actions/checkout@v4
15
+ - uses: actions/setup-node@v4
16
+ with:
17
+ node-version: 20
18
+ - run: npm ci
19
+ - run: npm test
20
+
21
+ publish-gpr:
22
+ needs: build
23
+ runs-on: ubuntu-latest
24
+ permissions:
25
+ contents: read
26
+ packages: write
27
+ steps:
28
+ - uses: actions/checkout@v4
29
+ - uses: actions/setup-node@v4
30
+ with:
31
+ node-version: 20
32
+ registry-url: https://npm.pkg.github.com/
33
+ - run: npm ci
34
+ - run: npm publish
35
+ env:
36
+ NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
@@ -0,0 +1,3 @@
1
+ {
2
+ "liveServer.settings.port": 5501
3
+ }
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # canvas-based-core
2
+ canvas-based-core
@@ -0,0 +1,25 @@
1
+ import {
2
+ CanvasCollisionDetection,
3
+ CanvasCollisionDetection2D
4
+ } from "./collisionDetection/canvas-collisionDetection-module.js";
5
+
6
+ import {
7
+ CanvasArc2D,
8
+ CanvasCircle2D,
9
+ CanvasLine2D,
10
+ CanvasRect2D
11
+ } from "./model/canvas-model-module.js";
12
+
13
+ import {
14
+ StyleType
15
+ } from "./styleType.js";
16
+
17
+ export {
18
+ CanvasCollisionDetection,
19
+ CanvasCollisionDetection2D,
20
+ StyleType,
21
+ CanvasArc2D,
22
+ CanvasCircle2D,
23
+ CanvasLine2D,
24
+ CanvasRect2D
25
+ }
@@ -0,0 +1,7 @@
1
+ import { CanvasCollisionDetection } from "./canvasCollisionDetection.js";
2
+ import { CanvasCollisionDetection2D } from "./canvasCollisionDetection2D.js";
3
+
4
+ export {
5
+ CanvasCollisionDetection,
6
+ CanvasCollisionDetection2D
7
+ }
@@ -0,0 +1,32 @@
1
+ export class CanvasCollisionDetection {
2
+
3
+ constructor(){}
4
+
5
+ static leftCollisionDetected(x, ctx){
6
+ return x <= 0;
7
+ }
8
+
9
+ static rightCollisionDetected(x, ctx){
10
+ return x >= ctx.canvas.width;
11
+ }
12
+
13
+ static horizontalCollisionDetected(x, ctx){
14
+ return this.leftCollisionDetected(x, ctx) || this.rightCollisionDetected(x, ctx);
15
+ }
16
+
17
+ static topCollisionDetected(y, ctx){
18
+ return y <= 0;
19
+ }
20
+
21
+ static bottomCollisionDetected(y, ctx){
22
+ return y >= ctx.canvas.height;
23
+ }
24
+
25
+ static verticalCollisionDetected(y, ctx){
26
+ return this.topCollisionDetected(y, ctx) || this.bottomCollisionDetected(y, ctx);
27
+ }
28
+
29
+ static collisionDetected(x, y, ctx){
30
+ return this.horizontalCollisionDetected(x, ctx) || this.verticalCollisionDetected(y, ctx);
31
+ }
32
+ }
@@ -0,0 +1,40 @@
1
+ import { CanvasCollisionDetection } from "./canvasCollisionDetection.js";
2
+
3
+ export class CanvasCollisionDetection2D extends CanvasCollisionDetection {
4
+
5
+ constructor(){
6
+ super();
7
+ }
8
+
9
+ static leftCollisionDetected(source, ctx){
10
+ const x = (source.x + source.currentXVelocity) + 0.1;
11
+ return super.leftCollisionDetected(x, ctx);
12
+ }
13
+
14
+ static rightCollisionDetected(source, ctx){
15
+ const x = ((source.x + source.width) + source.currentXVelocity) - 1;
16
+ return super.rightCollisionDetected(x, ctx);
17
+ }
18
+
19
+ static horizontalCollisionDetected(source, ctx){
20
+ return this.leftCollisionDetected(source, ctx) || this.rightCollisionDetected(source, ctx);
21
+ }
22
+
23
+ static topCollisionDetected(source, ctx){
24
+ const y = (source.y + source.currentYVelocity) + 0.1;
25
+ return super.topCollisionDetected(y, ctx);
26
+ }
27
+
28
+ static bottomCollisionDetected(source, ctx){
29
+ const y = ((source.y + source.height) + source.currentYVelocity) - 1;
30
+ return super.bottomCollisionDetected(y, ctx);
31
+ }
32
+
33
+ static verticalCollisionDetected(source, ctx){
34
+ return this.topCollisionDetected(source, ctx) || this.bottomCollisionDetected(source, ctx);
35
+ }
36
+
37
+ static collisionDetected(source, ctx){
38
+ return this.horizontalCollisionDetected(source, ctx) || this.verticalCollisionDetected(source, ctx);
39
+ }
40
+ }
@@ -0,0 +1,11 @@
1
+ import { CanvasArc2D } from "./canvasArc2D.js";
2
+ import { CanvasCircle2D } from "./canvasCircle2D.js";
3
+ import { CanvasLine2D } from "./canvasLine2D.js";
4
+ import { CanvasRect2D } from "./canvasRect2D.js";
5
+
6
+ export {
7
+ CanvasArc2D,
8
+ CanvasCircle2D,
9
+ CanvasLine2D,
10
+ CanvasRect2D
11
+ }
@@ -0,0 +1,59 @@
1
+ import { Arc2D } from "../../model/arc2D.js";
2
+ import { StyleType } from "../styleType.js";
3
+
4
+ export class CanvasArc2D extends Arc2D {
5
+
6
+ //props
7
+ ctx;
8
+ styleType;
9
+ style;
10
+
11
+ constructor(ctx, x, y, radius = 0, startAngle = 0, endAngle = 0, styleType = StyleType.FILL, style = "#000000", xVelocity = 0.00, yVelocity = 0.00) {
12
+ super(x, y, height, width, xVelocity, yVelocity);
13
+ this.ctx = ctx;
14
+ this.styleType = styleType;
15
+ this.style = style;
16
+ }
17
+
18
+ setCTX(val){
19
+ this.ctx = val;
20
+ return this;
21
+ }
22
+
23
+ setStyleType(val){
24
+ this.styleType = val;
25
+ return this;
26
+ }
27
+
28
+ setStyle(val){
29
+ this.style = val;
30
+ return this;
31
+ }
32
+
33
+ draw(){
34
+ this.#resolveDraw();
35
+ }
36
+
37
+ #resolveDraw(){
38
+ switch(this.styleType){
39
+ case StyleType.FILL:
40
+ this.ctx.fillStyle = this.style;
41
+ this.ctx.beginPath();
42
+ this.ctx.arc(this.x, this.y, this.radius, this.startAngle, this.endAngle);
43
+ this.ctx.fill();
44
+ break;
45
+ case StyleType.STROKE:
46
+ this.ctx.strokeStyle = this.style;
47
+ this.ctx.beginPath();
48
+ this.ctx.arc(this.x, this.y, this.radius, this.startAngle, this.endAngle);
49
+ this.ctx.stroke();
50
+ }
51
+ }
52
+
53
+ clear(){
54
+ const x = (this.x - this.radius);
55
+ const y = (this.y - this.radius);
56
+ const diameter = (this.radius * 2);
57
+ this.ctx.clearRect(x, y, diameter, diameter);
58
+ }
59
+ }
@@ -0,0 +1,59 @@
1
+ import { Circle2D } from "../../model/circle2D.js";
2
+ import { StyleType } from "../styleType.js";
3
+
4
+ export class CanvasCircle2D extends Circle2D {
5
+
6
+ //props
7
+ ctx;
8
+ styleType;
9
+ style;
10
+
11
+ constructor(ctx, x, y, radius = 0, styleType = StyleType.FILL, style = "#000000", xVelocity = 0.00, yVelocity = 0.00) {
12
+ super(x, y, radius, xVelocity, yVelocity);
13
+ this.ctx = ctx;
14
+ this.styleType = styleType;
15
+ this.style = style;
16
+ }
17
+
18
+ setCTX(val){
19
+ this.ctx = val;
20
+ return this;
21
+ }
22
+
23
+ setStyleType(val){
24
+ this.styleType = val;
25
+ return this;
26
+ }
27
+
28
+ setStyle(val){
29
+ this.style = val;
30
+ return this;
31
+ }
32
+
33
+ draw(){
34
+ this.#resolveDraw();
35
+ }
36
+
37
+ #resolveDraw(){
38
+ switch(this.styleType){
39
+ case StyleType.FILL:
40
+ this.ctx.fillStyle = this.style;
41
+ this.ctx.beginPath();
42
+ this.ctx.arc(this.x, this.y, this.radius, this.startAngle, this.endAngle);
43
+ this.ctx.fill();
44
+ break;
45
+ case StyleType.STROKE:
46
+ this.ctx.strokeStyle = this.style;
47
+ this.ctx.beginPath();
48
+ this.ctx.arc(this.x, this.y, this.radius, this.startAngle, this.endAngle);
49
+ this.ctx.stroke();
50
+ }
51
+ }
52
+
53
+ clear(){
54
+ const x = (this.x - this.radius);
55
+ const y = (this.y - this.radius);
56
+ const diameter = (this.radius * 2);
57
+ this.ctx.clearRect(x, y, diameter, diameter);
58
+ }
59
+ }
@@ -0,0 +1,26 @@
1
+ import { Line2D } from "../../model/line2D.js";
2
+
3
+ export class CanvasLine2D extends Line2D {
4
+
5
+ ctx;
6
+ style;
7
+
8
+ constructor(ctx, originX, originY, targetX, targetY, style = "#000000"){
9
+ super(originX, originY, targetX, targetY);
10
+ this.ctx = ctx;
11
+ this.style = style;
12
+ }
13
+
14
+ setCTX(val){
15
+ this.ctx = val;
16
+ return this;
17
+ }
18
+
19
+ draw(){
20
+ this.ctx.strokeStyle = this.style;
21
+ this.ctx.beginPath();
22
+ this.ctx.moveTo(this.originX, this.originY);
23
+ this.ctx.lineTo(this.targetX, this.targetY);
24
+ this.ctx.stroke();
25
+ }
26
+ }
@@ -0,0 +1,64 @@
1
+ import { Rect2D } from "../../model/rect2D.js";
2
+ import { StyleType } from "../styleType.js";
3
+
4
+ export class CanvasRect2D extends Rect2D {
5
+
6
+ //props
7
+ ctx;
8
+ styleType;
9
+ style;
10
+
11
+ constructor(ctx, x, y, height, width, styleType = StyleType.FILL, style = "#000000", xVelocity = 0.00, yVelocity = 0.00) {
12
+ super(x, y, height, width, xVelocity, yVelocity);
13
+ this.ctx = ctx;
14
+ this.styleType = styleType;
15
+ this.style = style;
16
+ }
17
+
18
+ setCTX(val){
19
+ this.ctx = val;
20
+ return this;
21
+ }
22
+
23
+ setStyleType(val){
24
+ this.styleType = val;
25
+ return this;
26
+ }
27
+
28
+ setStyle(val){
29
+ this.style = val;
30
+ return this;
31
+ }
32
+
33
+ draw(){
34
+ this.#resolveDraw();
35
+ }
36
+
37
+ #resolveDraw(){
38
+ switch(this.styleType){
39
+ case StyleType.FILL:
40
+ this.ctx.fillStyle = this.style;
41
+ this.ctx.fillRect(this.x, this.y, this.width, this.height);
42
+ break;
43
+ case StyleType.STROKE:
44
+ this.ctx.strokeStyle = this.style;
45
+ this.ctx.strokeRect(this.x, this.y, this.width, this.height);
46
+ }
47
+ }
48
+
49
+ clear(){
50
+ this.ctx.clearRect(this.x, this.y, this.width, this.height);
51
+ }
52
+
53
+ rotate(degree){
54
+ this.ctx.save();
55
+ this.degree = (this.degree + degree) % 360;
56
+ const rad = this.degree * (Math.PI / 180);
57
+ this.ctx.translate(this.x, this.y);
58
+ this.ctx.rotate(rad);
59
+ this.ctx.fillRect((-this.width / 2), (-this.height / 2), this.width, this.height);
60
+ this.ctx.resetTransform();
61
+ this.ctx.restore();
62
+ }
63
+
64
+ }
@@ -0,0 +1,4 @@
1
+ export const StyleType = {
2
+ FILL: "FILL",
3
+ STROKE: "STROKE"
4
+ };
@@ -0,0 +1,7 @@
1
+ import { CollisionDetection } from "./collisionDetection.js";
2
+ import { CollisionDetection2D } from "./collisionDetection2D.js";
3
+
4
+ export {
5
+ CollisionDetection,
6
+ CollisionDetection2D
7
+ }
@@ -0,0 +1,12 @@
1
+ export class CollisionDetection {
2
+
3
+ constructor(){}
4
+
5
+ static xAxisCollisionDetected(sourceX, targetX){
6
+ return sourceX === targetX;
7
+ }
8
+
9
+ static yAxisCollisionDetected(sourceY, targetY){
10
+ return sourceY === targetY;
11
+ }
12
+ }
@@ -0,0 +1,118 @@
1
+ import { CollisionDetection } from "./collisionDetection.js";
2
+ import { Point2D } from "../geometry/point2D.js";
3
+
4
+ export class CollisionDetection2D extends CollisionDetection {
5
+
6
+ constructor(){
7
+ super();
8
+ }
9
+
10
+ static leftCollisionDetected(source, target){
11
+ return ((source.x) <= (target.x + target.width)) &&
12
+ ((source.x + source.width) >= (target.x + target.width)) &&
13
+ this.isWithinYRange(source, target);
14
+ }
15
+
16
+ static rightCollisionDetected(source, target){
17
+ return ((source.x + source.width) >= target.x) &&
18
+ ((source.x) <= (target.x)) &&
19
+ this.isWithinYRange(source, target);
20
+ }
21
+
22
+ static horizontalCollisionDetected(source, target){
23
+ return this.leftCollisionDetected(source, target) || this.rightCollisionDetected(source, target);
24
+ }
25
+
26
+ static getLeftCollisionDetected(source, target){
27
+ if(this.leftCollisionDetected(source, target)){
28
+ return {
29
+ source: new Point2D(source.x, source.y),
30
+ target: new Point2D(target.x, target.y)
31
+ }
32
+ }
33
+ }
34
+
35
+ static getRightCollisionDetected(source, target){
36
+ if(this.rightCollisionDetected(source, target)){
37
+ return {
38
+ source: new Point2D(source.x, source.y),
39
+ target: new Point2D(target.x, target.y)
40
+ }
41
+ }
42
+ }
43
+
44
+ static getHorizontalCollisionDetected(source, target){
45
+ if(this.horizontalCollisionDetected(source, target)){
46
+ return {
47
+ source: new Point2D(source.x, source.y),
48
+ target: new Point2D(target.x, target.y)
49
+ }
50
+ }
51
+ }
52
+
53
+ static topCollisionDetected(source, target){
54
+ return ((source.y) <= (target.y + target.height)) &&
55
+ ((source.y + source.height) >= (target.y + target.height)) &&
56
+ this.isWithinXRange(source, target);
57
+ }
58
+
59
+ static bottomCollisionDetected(source, target){
60
+ return ((source.y + source.height) >= target.y) &&
61
+ ((source.y) <= (target.y)) &&
62
+ this.isWithinXRange(source, target);
63
+ }
64
+
65
+ static verticalCollisionDetected(source, target){
66
+ return this.topCollisionDetected(source, target) || this.bottomCollisionDetected(source, target);
67
+ }
68
+
69
+ static getTopCollisionDetected(source, target){
70
+ if(this.topCollisionDetected(source, target)){
71
+ return {
72
+ source: new Point2D(source.x, source.y),
73
+ target: new Point2D(target.x, target.y)
74
+ }
75
+ }
76
+ }
77
+
78
+ static getBottomCollisionDetected(source, target){
79
+ if(this.bottomCollisionDetected(source, target)){
80
+ return {
81
+ source: new Point2D(source.x, source.y),
82
+ target: new Point2D(target.x, target.y)
83
+ }
84
+ }
85
+ }
86
+
87
+ static getVerticalCollisionDetected(source, target){
88
+ if(this.verticalCollisionDetected(source, target)){
89
+ return {
90
+ source: new Point2D(source.x, source.y),
91
+ target: new Point2D(target.x, target.y)
92
+ }
93
+ }
94
+ }
95
+
96
+ static collisionDetected(source, target){
97
+ return this.horizontalCollisionDetected(source, target) || this.verticalCollisionDetected(source, target);
98
+ }
99
+
100
+ static getCollisionDetected(source, target){
101
+ if(this.collisionDetected(source, target)){
102
+ return {
103
+ source: new Point2D(source.x, source.y),
104
+ target: new Point2D(target.x, target.y)
105
+ }
106
+ }
107
+ }
108
+
109
+ static isWithinXRange(source, target){
110
+ return (((source.x + source.width) >= target.x) && (source.x <= target.x)) ||
111
+ ((source.x <= (target.x + target.width)) && (source.x >= target.x));
112
+ }
113
+
114
+ static isWithinYRange(source, target){
115
+ return (((source.y + source.height) >= target.y) && (source.y <= target.y)) ||
116
+ ((source.y <= (target.y + target.height)) && (source.y >= target.y));
117
+ }
118
+ }
@@ -0,0 +1,7 @@
1
+ import { KeyCode } from "./keyCode.js";
2
+ import { KeyboardControlMapBase } from "./keyboardControlMapBase.js";
3
+
4
+ export {
5
+ KeyCode,
6
+ KeyboardControlMapBase
7
+ }
@@ -0,0 +1,55 @@
1
+ export const KeyCode = {
2
+ ArrowUp: "ArrowUp" ,
3
+ ArrowDown: "ArrowDown",
4
+ ArrowLeft: "ArrowLeft" ,
5
+ ArrowRight: "ArrowRight" ,
6
+ Space: "Space",
7
+ KeyQ: "KeyQ",
8
+ KeyW: "KeyW",
9
+ KeyE: "KeyE",
10
+ KeyR: "KeyR",
11
+ KeyT: "KeyT",
12
+ KeyY: "KeyY",
13
+ KeyU: "KeyU",
14
+ KeyI: "KeyI",
15
+ KeyO: "KeyO",
16
+ KeyP: "KeyP",
17
+ KeyA: "KeyA",
18
+ KeyS: "KeyS",
19
+ KeyD: "KeyD",
20
+ KeyF: "KeyF",
21
+ KeyG: "KeyG",
22
+ KeyH: "KeyH",
23
+ KeyJ: "KeyJ",
24
+ KeyK: "KeyK",
25
+ KeyL: "KeyL",
26
+ KeyZ: "KeyZ",
27
+ KeyX: "KeyX",
28
+ KeyC: "KeyC",
29
+ KeyV: "KeyV",
30
+ KeyB: "KeyB",
31
+ KeyN: "KeyN",
32
+ KeyM: "KeyM",
33
+ Digit1: "Digit1",
34
+ Digit2: "Digit2",
35
+ Digit3: "Digit3",
36
+ Digit4: "Digit4",
37
+ Digit5: "Digit5",
38
+ Digit6: "Digit6",
39
+ Digit7: "Digit7",
40
+ Digit8: "Digit8",
41
+ Digit9: "Digit9",
42
+ Digit0: "Digit0",
43
+ Numpad1: "Numpad1",
44
+ Numpad2: "Numpad2",
45
+ Numpad3: "Numpad3",
46
+ Numpad4: "Numpad4",
47
+ Numpad5: "Numpad5",
48
+ Numpad6: "Numpad6",
49
+ Numpad7: "Numpad7",
50
+ Numpad8: "Numpad8",
51
+ Numpad9: "Numpad9",
52
+ Numpad0: "Numpad0",
53
+ ShiftLeft: "ShiftLeft",
54
+ ShiftRight: "ShiftRight"
55
+ }
@@ -0,0 +1,15 @@
1
+ export class KeyboardControlMapBase {
2
+
3
+ up;
4
+ down;
5
+ left;
6
+ right;
7
+
8
+ constructor(up, down, left, right){
9
+ this.up = up;
10
+ this.down = down;
11
+ this.left = left;
12
+ this.right = right;
13
+ }
14
+
15
+ }
@@ -0,0 +1,7 @@
1
+ import { Point2D } from "./point2D.js";
2
+ import { Point3D } from "./point3D.js";
3
+
4
+ export {
5
+ Point2D,
6
+ Point3D
7
+ }
@@ -0,0 +1,20 @@
1
+ export class Point2D {
2
+
3
+ x;
4
+ y;
5
+
6
+ constructor(x, y){
7
+ this.x = x;
8
+ this.y = y;
9
+ }
10
+
11
+ setX(val){
12
+ this.x = val;
13
+ return this;
14
+ }
15
+
16
+ setY(val){
17
+ this.y = val;
18
+ return this;
19
+ }
20
+ }
@@ -0,0 +1,17 @@
1
+ import { Point2D } from "./point2D.js";
2
+
3
+ export class Point3D extends Point2D {
4
+
5
+ z;
6
+
7
+ constructor(x, y){
8
+ super(x, y);
9
+ this.z = z;
10
+ }
11
+
12
+ setZ(val){
13
+ this.z = val;
14
+ return this;
15
+ }
16
+
17
+ }
package/model/arc2D.js ADDED
@@ -0,0 +1,33 @@
1
+ import { ObjectBase2D } from "./ObjectBase2D.js";
2
+
3
+ export class Arc2D extends ObjectBase2D {
4
+
5
+ //props
6
+ //type int
7
+ radius = 0;
8
+ startAngle = 0;
9
+ endAngle = 0;
10
+
11
+ constructor(x, y, radius = 0, startAngle = 0, endAngle = 0, xVelocity = 0.00, yVelocity = 0.00) {
12
+ super(x, y, xVelocity, yVelocity);
13
+ this.radius = radius;
14
+ this.startAngle = startAngle;
15
+ this.endAngle = endAngle;
16
+ }
17
+
18
+ setRadius(val){
19
+ this.radius = val;
20
+ return this;
21
+ }
22
+
23
+ setStartAngle(val){
24
+ this.startAngle = val;
25
+ return this;
26
+ }
27
+
28
+ setEndAngle(val){
29
+ this.endAngle = val;
30
+ return this;
31
+ }
32
+
33
+ }
@@ -0,0 +1,9 @@
1
+ import { Arc2D } from "./arc2D.js";
2
+
3
+ export class Circle2D extends Arc2D {
4
+
5
+ constructor(x, y, radius = 0, xVelocity = 0.00, yVelocity = 0.00) {
6
+ super(x, y, radius, 0, (Math.PI * 2), xVelocity, yVelocity);
7
+ }
8
+
9
+ }
@@ -0,0 +1,34 @@
1
+ export class Line2D {
2
+
3
+ originX;
4
+ originX;
5
+ targetX;
6
+ targetY;
7
+
8
+ constructor(originX, originY, targetX, targetY){
9
+ this.originX = originX;
10
+ this.originY = originY;
11
+ this.targetX = targetX;
12
+ this.targetY = targetY;
13
+ }
14
+
15
+ setOriginX(val){
16
+ this.originX = val;
17
+ return this;
18
+ }
19
+
20
+ setOriginY(val){
21
+ this.originY = val;
22
+ return this;
23
+ }
24
+
25
+ setTargetX(val){
26
+ this.targetX = val;
27
+ return this;
28
+ }
29
+
30
+ setTargetY(val){
31
+ this.targetY = val;
32
+ return this;
33
+ }
34
+ }
@@ -0,0 +1,15 @@
1
+ import { Arc2D } from "./arc2D.js";
2
+ import { Circle2D } from "./circle2D.js";
3
+ import { Line2D } from "./line2D.js";
4
+ import { Rect2D } from "./rect2D.js";
5
+ import { ObjectBase2D } from "./ObjectBase2D.js";
6
+ import { ObjectBase3D } from "./ObjectBase3D.js";
7
+
8
+ export {
9
+ ObjectBase2D,
10
+ ObjectBase3D,
11
+ Arc2D,
12
+ Circle2D,
13
+ Line2D,
14
+ Rect2D
15
+ }
@@ -0,0 +1,48 @@
1
+ export class ObjectBase2D {
2
+
3
+ //props
4
+ //type int
5
+ x;
6
+ y;
7
+ //type float
8
+ xVelocity;
9
+ yVelocity;
10
+
11
+ //ctor
12
+ constructor(x, y, xVelocity = 0.00, yVelocity = 0.00) {
13
+ this.x = x;
14
+ this.y = y;
15
+ this.xVelocity = xVelocity;
16
+ this.yVelocity = yVelocity;
17
+ }
18
+
19
+ //prop methods
20
+ setX(val){
21
+ this.x = val;
22
+ return this;
23
+ }
24
+
25
+ setY(val){
26
+ this.y = val;
27
+ return this;
28
+ }
29
+
30
+ setXVelocity(val){
31
+ this.xVelocity = val;
32
+ return this;
33
+ }
34
+
35
+ setYVelocity(val){
36
+ this.yVelocity = val;
37
+ return this;
38
+ }
39
+ //
40
+
41
+ moveXByXVelocity(){
42
+ this.x += this.xVelocity;
43
+ }
44
+
45
+ moveYByYVelocity(){
46
+ this.y += this.yVelocity;
47
+ }
48
+ }
@@ -0,0 +1,31 @@
1
+ import { ObjectBase2D } from "./ObjectBase2D.js";
2
+
3
+ export class ObjectBase3D extends ObjectBase2D {
4
+
5
+ //props
6
+ //type int
7
+ z;
8
+ //type float
9
+ zVelocity;
10
+
11
+ constructor(x, y, z, xVelocity = 0.00, yVelocity = 0.00, zVelocity = 0.00) {
12
+ super(x, y, xVelocity, yVelocity);
13
+ this.z = z;
14
+ this.zVelocity = zVelocity;
15
+ }
16
+
17
+ setZ(val){
18
+ this.z = val;
19
+ return this;
20
+ }
21
+
22
+ setZVelocity(val){
23
+ this.zVelocity = val;
24
+ return this;
25
+ }
26
+
27
+ moveZByZVelocity(){
28
+ this.z += this.zVelocity;
29
+ }
30
+
31
+ }
@@ -0,0 +1,62 @@
1
+ import { ObjectBase2D } from "./ObjectBase2D.js";
2
+
3
+ export class Rect2D extends ObjectBase2D {
4
+
5
+ //props
6
+ //type int
7
+ height;
8
+ width;
9
+
10
+ constructor(x, y, height, width, xVelocity = 0.00, yVelocity = 0.00) {
11
+ super(x, y, xVelocity, yVelocity);
12
+ this.height = height;
13
+ this.width = width;
14
+ }
15
+
16
+ setHeight(val){
17
+ this.height = val;
18
+ return this;
19
+ }
20
+
21
+ setWidth(val){
22
+ this.width = val;
23
+ return this;
24
+ }
25
+
26
+ getTopLeftCoord(){
27
+ const x = this.x;
28
+ const y = this.y;
29
+ return new Point2D(x, y);
30
+ }
31
+
32
+ getTopRightCoord(){
33
+ const x = (this.x + this.width);
34
+ const y = this.y;
35
+ return new Point2D(x, y);
36
+ }
37
+
38
+ getBottomRightCoord(){
39
+ const x = (this.x + this.width);
40
+ const y = (this.y + this.height);
41
+ return new Point2D(x, y);
42
+ }
43
+
44
+ getBottomLeftCoord(){
45
+ const x = this.x;
46
+ const y = (this.y + this.height);
47
+ return new Point2D(x, y);
48
+ }
49
+
50
+ getCenterCoord(){
51
+ const x = ((this.x + this.width) / 2);
52
+ const y = ((this.y + this.height) / 2);
53
+ return new Point2D(x, y);
54
+ }
55
+
56
+ getCoordinates(){
57
+ //clockwise from origin x, y ie topleft
58
+ //[topleft, topright, bottomright, bottomleft, center]
59
+ return [this.getTopLeftCoord(), this.getTopRightCoord(), this.getBottomRightCoord(), this.getBottomLeftCoord(), this.getCenterCoord()];
60
+ }
61
+
62
+ }
package/module.js ADDED
@@ -0,0 +1,57 @@
1
+
2
+ import {
3
+ KeyCode,
4
+ KeyboardControlMapBase
5
+ } from "./control/control-module.js";
6
+
7
+ import {
8
+ Point2D,
9
+ Point3D
10
+ } from "./geometry/geometry-module.js";
11
+
12
+ import {
13
+ CollisionDetection,
14
+ CollisionDetection2D
15
+ } from "./collisionDetection/collisionDetection-module.js";
16
+
17
+ import {
18
+ ObjectBase2D,
19
+ ObjectBase3D,
20
+ Arc2D,
21
+ Circle2D,
22
+ Line2D,
23
+ Rect2D
24
+ } from "./model/model-module.js";
25
+
26
+ import {
27
+ StyleType,
28
+ CanvasCollisionDetection,
29
+ CanvasCollisionDetection2D,
30
+ CanvasArc2D,
31
+ CanvasCircle2D,
32
+ CanvasLine2D,
33
+ CanvasRect2D
34
+ } from "./canvas/canvas-module.js";
35
+
36
+
37
+ export {
38
+ KeyCode,
39
+ KeyboardControlMapBase,
40
+ Point2D,
41
+ Point3D,
42
+ CollisionDetection,
43
+ CollisionDetection2D,
44
+ StyleType,
45
+ ObjectBase2D,
46
+ ObjectBase3D,
47
+ Arc2D,
48
+ Circle2D,
49
+ Line2D,
50
+ Rect2D,
51
+ CanvasCollisionDetection,
52
+ CanvasCollisionDetection2D,
53
+ CanvasArc2D,
54
+ CanvasCircle2D,
55
+ CanvasLine2D,
56
+ CanvasRect2D
57
+ }
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "canvas-based-core",
3
+ "version": "1.0.0",
4
+ "description": "canvas-based-core",
5
+ "homepage": "https://github.com/pr1v4te4cc0unt/canvas-based-core#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/pr1v4te4cc0unt/canvas-based-core/issues"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/pr1v4te4cc0unt/canvas-based-core.git"
12
+ },
13
+ "license": "ISC",
14
+ "author": "pr1v4te4cc0unt",
15
+ "type": "commonjs",
16
+ "main": "module.js",
17
+ "scripts": {
18
+ "test": "echo \"Error: no test specified\" && exit 1"
19
+ }
20
+ }
package/test.css ADDED
@@ -0,0 +1,29 @@
1
+ header{
2
+ height: 25px;
3
+ width: 800px;
4
+ margin: auto;
5
+ }
6
+
7
+ main{
8
+ height: 500px;
9
+ width: 800px;
10
+ margin: auto;
11
+ font-family: sans-serif;
12
+ display: flex;
13
+ flex-direction: column;
14
+ justify-content: space-around;
15
+ text-align: center;
16
+ }
17
+
18
+ footer{
19
+ height: 25px;
20
+ width: 800px;
21
+ margin: auto;
22
+ }
23
+
24
+ canvas{
25
+ height: 400px;
26
+ width: 800px;
27
+ margin: auto;
28
+ border: 5px solid black;
29
+ }
package/test.html ADDED
@@ -0,0 +1,22 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <link rel="stylesheet" href="./test.css">
7
+ <script src="./test.js" type="module" defer></script>
8
+ <title>Document</title>
9
+ </head>
10
+ <body>
11
+
12
+ <header></header>
13
+
14
+ <main>
15
+ <canvas>
16
+
17
+ </canvas>
18
+ </main>
19
+
20
+ <footer></footer>
21
+ </body>
22
+ </html>
package/test.js ADDED
@@ -0,0 +1,27 @@
1
+ import * as Core from "./moduleExport.js";
2
+
3
+ var canvas = document.querySelector("canvas");
4
+ var ctx = canvas.getContext("2d");
5
+
6
+ var circle = new Core.CanvasCircle2D(ctx, 50, 50, 10);
7
+ console.log(circle);
8
+
9
+ setInterval(() => circle.draw(), 1000);
10
+ setInterval(() => circle.clear(), 2000);
11
+
12
+ var rectOne = new Core.CanvasRect2D(ctx, 100, 50, 10, 10).setStyle("black");
13
+ rectOne.draw();
14
+ // console.log(rectOne);
15
+
16
+ var rectTwo = new Core.CanvasRect2D(ctx, 120, 50, 10, 10).setStyle("green");
17
+ rectTwo.draw();
18
+ // console.log(rectTwo);
19
+
20
+ var rectThree = new Core.CanvasRect2D(ctx, 140, 50, 10, 10).setStyle("blue");
21
+ rectThree.draw();
22
+
23
+ rectOne.draw();
24
+ rectTwo.draw();
25
+ rectThree.draw();
26
+ // console.log(rectOne);
27
+