9 Commits

Author SHA1 Message Date
Emma
fbcffcde27 fixed 2023-02-12 20:03:06 -07:00
Emma
c7ff737690 On Drag event 2023-02-12 19:57:12 -07:00
Emma
3d366a1a6c drawImage and drawSprite 2023-02-11 18:39:19 -07:00
Emma
3544b7eae4 unregister 2023-02-10 07:38:57 -07:00
Emma
ff463c6cee more methods return this 2023-02-10 07:24:02 -07:00
Emma
6530b928f5 Clickables 2023-02-10 07:16:47 -07:00
Emma
32365812df I'm dumb 2023-02-08 01:25:29 -07:00
Emma
880c0be4f1 added drag start and end events 2023-02-08 01:20:37 -07:00
Emma
0d6717896e fixed issue of being able to add a point to draggables twice 2023-02-07 16:09:45 -07:00
6 changed files with 167 additions and 50 deletions

BIN
EngineSprites.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

BIN
PurpleEngine.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@@ -59,6 +59,7 @@ class Vector {
this.y += y ?? 0; this.y += y ?? 0;
this.z += z ?? 0; this.z += z ?? 0;
} }
return this;
} }
sub(v, y, z) { sub(v, y, z) {
if (arguments.length === 1 && typeof v !== 'number') { if (arguments.length === 1 && typeof v !== 'number') {
@@ -73,6 +74,7 @@ class Vector {
this.y -= y ?? 0; this.y -= y ?? 0;
this.z -= z ?? 0; this.z -= z ?? 0;
} }
return this;
} }
mult(v) { mult(v) {
if (typeof v === 'number') { if (typeof v === 'number') {
@@ -96,6 +98,7 @@ class Vector {
this.y /= v.y; this.y /= v.y;
this.z /= v.z; this.z /= v.z;
} }
return this;
} }
rotate(angle) { rotate(angle) {
const prev_x = this.x; const prev_x = this.x;
@@ -103,6 +106,7 @@ class Vector {
const s = Math.sin(angle); const s = Math.sin(angle);
this.x = c * this.x - s * this.y; this.x = c * this.x - s * this.y;
this.y = s * prev_x + c * this.y; this.y = s * prev_x + c * this.y;
return this;
} }
dist(v) { dist(v) {
const dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z; const dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z;
@@ -135,6 +139,7 @@ class Vector {
this.x = lerp_val(this.x, x, amt); this.x = lerp_val(this.x, x, amt);
this.y = lerp_val(this.y, y, amt); this.y = lerp_val(this.y, y, amt);
this.z = lerp_val(this.z, z, amt); this.z = lerp_val(this.z, z, amt);
return this;
} }
normalize() { normalize() {
const m = this.mag(); const m = this.mag();
@@ -148,6 +153,7 @@ class Vector {
this.normalize(); this.normalize();
this.mult(high); this.mult(high);
} }
return this;
} }
heading() { heading() {
return -Math.atan2(-this.y, this.x); return -Math.atan2(-this.y, this.x);
@@ -251,6 +257,7 @@ class Doodler {
return this.ctx.canvas.height; return this.ctx.canvas.height;
} }
draggables = []; draggables = [];
clickables = [];
constructor({ width , height , canvas , bg , framerate }){ constructor({ width , height , canvas , bg , framerate }){
if (!canvas) { if (!canvas) {
canvas = document.createElement('canvas'); canvas = document.createElement('canvas');
@@ -381,6 +388,12 @@ class Doodler {
cb(); cb();
this.ctx.restore(); this.ctx.restore();
} }
drawImage(img, at, w, h) {
w && h ? this.ctx.drawImage(img, at.x, at.y, w, h) : this.ctx.drawImage(img, at.x, at.y);
}
drawSprite(img, spritePos, sWidth, sHeight, at, width, height) {
this.ctx.drawImage(img, spritePos.x, spritePos.y, sWidth, sHeight, at.x, at.y, width, height);
}
setStyle(style) { setStyle(style) {
const ctx = this.ctx; const ctx = this.ctx;
ctx.fillStyle = style?.color || style?.fillColor || 'black'; ctx.fillStyle = style?.color || style?.fillColor || 'black';
@@ -390,6 +403,7 @@ class Doodler {
mouseX = 0; mouseX = 0;
mouseY = 0; mouseY = 0;
registerDraggable(point, radius, style) { registerDraggable(point, radius, style) {
if (this.draggables.find((d)=>d.point === point)) return;
const id = this.addUIElement('circle', point, radius, { const id = this.addUIElement('circle', point, radius, {
fillColor: '#5533ff50', fillColor: '#5533ff50',
strokeColor: '#5533ff50' strokeColor: '#5533ff50'
@@ -409,19 +423,44 @@ class Doodler {
} }
this.draggables = this.draggables.filter((d)=>d.point !== point); this.draggables = this.draggables.filter((d)=>d.point !== point);
} }
registerClickable(p1, p2, cb) {
const top = Math.min(p1.y, p2.y);
const left = Math.min(p1.x, p2.x);
const bottom = Math.max(p1.y, p2.y);
const right = Math.max(p1.x, p2.x);
this.clickables.push({
onClick: cb,
checkBound: (p)=>p.y >= top && p.x >= left && p.y <= bottom && p.x <= right
});
}
unregisterClickable(cb) {
this.clickables = this.clickables.filter((c)=>c.onClick !== cb);
}
addDragEvents({ onDragEnd , onDragStart , point }) {
const d = this.draggables.find((d)=>d.point === point);
if (d) {
d.onDragEnd = onDragEnd;
d.onDragStart = onDragStart;
}
}
onClick(e) { onClick(e) {
const rect = this._canvas.getBoundingClientRect(); const mouse = new Vector(this.mouseX, this.mouseY);
e.clientX - rect.left;
e.clientY - rect.top;
for (const d of this.draggables){ for (const d of this.draggables){
if (d.point.dist(new Vector(this.mouseX, this.mouseY)) <= d.radius) { if (d.point.dist(mouse) <= d.radius) {
d.beingDragged = true; d.beingDragged = true;
d.onDragStart?.call(null);
} else d.beingDragged = false; } else d.beingDragged = false;
} }
for (const c of this.clickables){
if (c.checkBound(mouse)) {
c.onClick();
}
}
} }
offClick(e) { offClick(e) {
for (const d of this.draggables){ for (const d of this.draggables){
d.beingDragged = false; d.beingDragged = false;
d.onDragEnd?.call(null);
} }
} }
uiElements = new Map(); uiElements = new Map();
@@ -463,29 +502,31 @@ init({
width: 400, width: 400,
height: 400 height: 400
}); });
const movingVector = new Vector(100, 300); new Vector(100, 300);
let angleMultiplier = 0;
const v = new Vector(30, 30); const v = new Vector(30, 30);
doodler.registerDraggable(v, 20); doodler.registerDraggable(v, 20);
const img = new Image();
img.src = './EngineSprites.png';
img.hidden;
document.body.append(img);
const p = new Vector(200, 200);
doodler.createLayer(()=>{ doodler.createLayer(()=>{
doodler.line(new Vector(100, 100), new Vector(200, 200)); doodler.line(p.copy().add(-8, 10), p.copy().add(8, 10), {
doodler.dot(new Vector(300, 300)); color: 'grey',
doodler.fillCircle(movingVector, 6, { weight: 2
color: 'red'
}); });
doodler.drawRect(new Vector(50, 50), movingVector.x, movingVector.y); doodler.line(p.copy().add(-8, -10), p.copy().add(8, -10), {
doodler.fillRect(new Vector(200, 250), 30, 10); color: 'grey',
doodler.drawCenteredSquare(new Vector(200, 200), 40, { weight: 2
color: 'purple',
weight: 5
}); });
doodler.drawBezier(new Vector(100, 150), movingVector, new Vector(150, 300), new Vector(100, 250)); doodler.line(p, p.copy().add(0, 12), {
let rotatedOrigin = new Vector(200, 200); color: 'brown',
doodler.drawRotated(rotatedOrigin, Math.PI * angleMultiplier, ()=>{ weight: 4
doodler.drawCenteredSquare(rotatedOrigin, 30); });
doodler.line(p, p.copy().add(0, -12), {
color: 'brown',
weight: 4
}); });
movingVector.set((movingVector.x + 1) % 400, movingVector.y);
angleMultiplier += .001;
}); });
document.addEventListener('keyup', (e)=>{ document.addEventListener('keyup', (e)=>{
e.preventDefault(); e.preventDefault();

View File

@@ -37,6 +37,7 @@ export class Doodler {
} }
private draggables: Draggable[] = []; private draggables: Draggable[] = [];
private clickables: Clickable[] = [];
constructor({ constructor({
width, width,
@@ -74,6 +75,7 @@ export class Doodler {
for (const d of this.draggables.filter(d => d.beingDragged)) { for (const d of this.draggables.filter(d => d.beingDragged)) {
d.point.add(e.movementX, e.movementY) d.point.add(e.movementX, e.movementY)
d.onDrag && d.onDrag({x: e.movementX, y: e.movementY});
} }
}) })
this.startDrawLoop(); this.startDrawLoop();
@@ -194,6 +196,15 @@ export class Doodler {
this.ctx.restore(); this.ctx.restore();
} }
drawImage(img: HTMLImageElement, at: Vector): void;
drawImage(img: HTMLImageElement, at: Vector, w: number, h: number): void;
drawImage(img: HTMLImageElement, at: Vector, w?: number, h?: number) {
w && h ? this.ctx.drawImage(img, at.x, at.y, w, h) : this.ctx.drawImage(img, at.x, at.y);
}
drawSprite(img: HTMLImageElement, spritePos: Vector, sWidth: number, sHeight: number, at: Vector, width: number, height: number) {
this.ctx.drawImage(img, spritePos.x, spritePos.y, sWidth, sHeight, at.x, at.y, width, height);
}
setStyle(style?: IStyle) { setStyle(style?: IStyle) {
const ctx = this.ctx; const ctx = this.ctx;
ctx.fillStyle = style?.color || style?.fillColor || 'black'; ctx.fillStyle = style?.color || style?.fillColor || 'black';
@@ -209,6 +220,7 @@ export class Doodler {
registerDraggable(point: Vector, radius: number, style?: IStyle & { shape: 'square' | 'circle' }) { registerDraggable(point: Vector, radius: number, style?: IStyle & { shape: 'square' | 'circle' }) {
if (this.draggables.find(d => d.point === point)) return;
const id = this.addUIElement('circle', point, radius, { fillColor: '#5533ff50', strokeColor: '#5533ff50' }) const id = this.addUIElement('circle', point, radius, { fillColor: '#5533ff50', strokeColor: '#5533ff50' })
this.draggables.push({ point, radius, style, id }); this.draggables.push({ point, radius, style, id });
} }
@@ -221,21 +233,56 @@ export class Doodler {
this.draggables = this.draggables.filter(d => d.point !== point); this.draggables = this.draggables.filter(d => d.point !== point);
} }
onClick(e: MouseEvent) { registerClickable(p1: Vector, p2: Vector, cb: () => void) {
const rect = this._canvas.getBoundingClientRect(); const top = Math.min(p1.y, p2.y);
const mouseX = e.clientX - rect.left; const left = Math.min(p1.x, p2.x);
const mouseY = e.clientY - rect.top; const bottom = Math.max(p1.y, p2.y);
const right = Math.max(p1.x, p2.x);
this.clickables.push({
onClick: cb,
checkBound: (p) => p.y >= top && p.x >= left && p.y <= bottom && p.x <= right
})
}
unregisterClickable(cb: () => void) {
this.clickables = this.clickables.filter(c => c.onClick !== cb);
}
addDragEvents({
onDragEnd,
onDragStart,
onDrag,
point
}: { point: Vector, onDragEnd?: () => void, onDragStart?: () => void, onDrag?: (movement?: {x: number, y: number}) => void }) {
const d = this.draggables.find(d => d.point === point);
if (d) {
d.onDragEnd = onDragEnd;
d.onDragStart = onDragStart;
d.onDrag = onDrag;
}
}
onClick(e: MouseEvent) {
const mouse = new Vector(this.mouseX, this.mouseY)
for (const d of this.draggables) { for (const d of this.draggables) {
if (d.point.dist(new Vector(this.mouseX, this.mouseY)) <= d.radius) { if (d.point.dist(mouse) <= d.radius) {
d.beingDragged = true; d.beingDragged = true;
d.onDragStart?.call(null);
} else d.beingDragged = false; } else d.beingDragged = false;
} }
for (const c of this.clickables) {
if (c.checkBound(mouse)) {
c.onClick();
}
}
} }
offClick(e: MouseEvent) { offClick(e: MouseEvent) {
for (const d of this.draggables) { for (const d of this.draggables) {
d.beingDragged = false; d.beingDragged = false;
d.onDragEnd?.call(null);
} }
} }
@@ -299,6 +346,14 @@ type Draggable = {
style?: IStyle & { shape: 'square' | 'circle' }; style?: IStyle & { shape: 'square' | 'circle' };
beingDragged?: boolean; beingDragged?: boolean;
id: string; id: string;
onDragStart?: () => void;
onDragEnd?: () => void;
onDrag?: (dragDistance?: {x: number, y: number}) => void;
}
type Clickable = {
onClick: () => void;
checkBound: (p: Vector) => boolean;
} }
type uiDrawing = { type uiDrawing = {

View File

@@ -56,9 +56,9 @@ export class Vector {
return v; return v;
} }
} }
add(x: number, y: number, z: number): void; add(x: number, y: number, z: number): Vector;
add(x: number, y: number): void; add(x: number, y: number): Vector;
add(v: Vector): void; add(v: Vector): Vector;
add(v: Vector | number, y?: number, z?: number) { add(v: Vector | number, y?: number, z?: number) {
if (arguments.length === 1 && typeof v !== 'number') { if (arguments.length === 1 && typeof v !== 'number') {
this.x += v.x; this.x += v.x;
@@ -73,10 +73,11 @@ export class Vector {
this.y += y ?? 0; this.y += y ?? 0;
this.z += z ?? 0; this.z += z ?? 0;
} }
return this;
} }
sub(x: number, y: number, z: number): void; sub(x: number, y: number, z: number): Vector;
sub(x: number, y: number): void; sub(x: number, y: number): Vector;
sub(v: Vector): void; sub(v: Vector): Vector;
sub(v: Vector | number, y?: number, z?: number) { sub(v: Vector | number, y?: number, z?: number) {
if (arguments.length === 1 && typeof v !== 'number') { if (arguments.length === 1 && typeof v !== 'number') {
this.x -= v.x; this.x -= v.x;
@@ -91,6 +92,7 @@ export class Vector {
this.y -= y ?? 0; this.y -= y ?? 0;
this.z -= z ?? 0; this.z -= z ?? 0;
} }
return this;
} }
mult(v: number | Vector) { mult(v: number | Vector) {
if (typeof v === 'number') { if (typeof v === 'number') {
@@ -114,6 +116,7 @@ export class Vector {
this.y /= v.y; this.y /= v.y;
this.z /= v.z; this.z /= v.z;
} }
return this;
} }
rotate(angle: number) { rotate(angle: number) {
const prev_x = this.x; const prev_x = this.x;
@@ -121,6 +124,7 @@ export class Vector {
const s = Math.sin(angle); const s = Math.sin(angle);
this.x = c * this.x - s * this.y; this.x = c * this.x - s * this.y;
this.y = s * prev_x + c * this.y; this.y = s * prev_x + c * this.y;
return this;
} }
dist(v: Vector) { dist(v: Vector) {
const dx = this.x - v.x, const dx = this.x - v.x,
@@ -165,6 +169,7 @@ export class Vector {
this.x = lerp_val(this.x, x, amt!); this.x = lerp_val(this.x, x, amt!);
this.y = lerp_val(this.y, y, amt!); this.y = lerp_val(this.y, y, amt!);
this.z = lerp_val(this.z, z!, amt!); this.z = lerp_val(this.z, z!, amt!);
return this;
} }
normalize() { normalize() {
const m = this.mag(); const m = this.mag();
@@ -178,6 +183,7 @@ export class Vector {
this.normalize(); this.normalize();
this.mult(high); this.mult(high);
} }
return this;
} }
heading() { heading() {
return (-Math.atan2(-this.y, this.x)); return (-Math.atan2(-this.y, this.x));

41
main.ts
View File

@@ -11,25 +11,40 @@ const movingVector = new Vector(100, 300);
let angleMultiplier = 0; let angleMultiplier = 0;
const v = new Vector(30, 30); const v = new Vector(30, 30);
doodler.registerDraggable(v, 20) doodler.registerDraggable(v, 20)
const img = new Image();
img.src = './EngineSprites.png'
img.hidden
document.body.append(img)
const p = new Vector(200, 200);
doodler.createLayer(() => { doodler.createLayer(() => {
doodler.line(new Vector(100, 100), new Vector(200, 200)) // doodler.line(new Vector(100, 100), new Vector(200, 200))
doodler.dot(new Vector(300, 300)) // doodler.dot(new Vector(300, 300))
doodler.fillCircle(movingVector, 6, { color: 'red' }); // doodler.fillCircle(movingVector, 6, { color: 'red' });
doodler.drawRect(new Vector(50, 50), movingVector.x, movingVector.y); // doodler.drawRect(new Vector(50, 50), movingVector.x, movingVector.y);
doodler.fillRect(new Vector(200, 250), 30, 10) // doodler.fillRect(new Vector(200, 250), 30, 10)
doodler.drawCenteredSquare(new Vector(200, 200), 40, { color: 'purple', weight: 5 }) // doodler.drawCenteredSquare(new Vector(200, 200), 40, { color: 'purple', weight: 5 })
doodler.drawBezier(new Vector(100, 150), movingVector, new Vector(150, 300), new Vector(100, 250)) // doodler.drawBezier(new Vector(100, 150), movingVector, new Vector(150, 300), new Vector(100, 250))
let rotatedOrigin = new Vector(200,200) // let rotatedOrigin = new Vector(200, 200)
doodler.drawRotated(rotatedOrigin, Math.PI*angleMultiplier, () => { // doodler.drawRotated(rotatedOrigin, Math.PI * angleMultiplier, () => {
doodler.drawCenteredSquare(rotatedOrigin, 30) // doodler.drawCenteredSquare(rotatedOrigin, 30)
}) // doodler.drawSprite(img, new Vector(0, 40), 80, 20, new Vector(160, 300), 80, 20)
// })
movingVector.set((movingVector.x + 1) % 400, movingVector.y); // movingVector.set((movingVector.x + 1) % 400, movingVector.y);
angleMultiplier += .001; // angleMultiplier += .001;
// doodler.drawSprite(img, new Vector(0, 40), 80, 20, new Vector(100, 300), 80, 20)
doodler.line(p.copy().add(-8,10), p.copy().add(8,10), {color: 'grey', weight: 2})
doodler.line(p.copy().add(-8,-10), p.copy().add(8,-10), {color: 'grey', weight: 2})
doodler.line(p, p.copy().add(0,12), {color: 'brown', weight: 4})
doodler.line(p, p.copy().add(0,-12), {color: 'brown', weight: 4})
}); });
document.addEventListener('keyup', e => { document.addEventListener('keyup', e => {